PHP can't connect to MySQL Database (timeout)

I started experimenting with PHP and now I wanted to connect with a MySQL Database and have tried every way you can find on the Internet (mysqli and pdo), but every time the connection times out. What i also need to say is that the Database is running on a external server and not on localhost

Answer

Solution:

I guess you're running your php code on your own machine and MySQL on a hosting service.

Many hosting services simply don't allow connections to MySQl from outside their private networks. Because cybercreeps. If that's your situation you need a different hosting service.

Others require some special configuration setting. It sometimes appears on cpanel as something like "Remote database access". Ask their tech support. And make sure you use the correct hostname when connecting.

If you are new to this stuff you may get the best setup for your learning by installing MySql locally. Trying to learn both database programming and the necessary network engineering for a remote connection at the same time? Huuuge pain in the xxs neck. Can you install WAMP? Amps?

Source