Php/mySQL connection to remote server, fatal error, connection timeout

I’ve got one droplet at DO and one webspace with domain at another hosting provider. I don’t have access to anything other than ftp on the other server. On that other webspace I’ve got a php script which I want to remotely connect to my mySQL db on my droplet. I’ve opened the port 3306 in the firewall, added a user with access to the table from % and changed the row starting with “bind-adress” (also tried commenting out) in the configuration file. Still I get an error like this when trying to connect:

Fatal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[HY000] [2002] Connection timed out’ in {the url to the script that’s trying to connect}:19 Stack trace: #0 {the url to the script that’s trying to connect}(19): PDO->__construct('mysql:host={my droplet public ip}, ‘{username}’, ‘{password}’) #1 {main} thrown in {the url to the script that’s trying to connect} on line 19

And the connection part in php looks like this:

$db = new PDO(‘mysql:host={droplet public ip};port=3306;dbname={database name};charset=utf8’, ‘{username}’, ‘{password}’);

What am I doing wrong?! I’ve contacted the DO support, they couldn’t help me since we don’t know where the problem is. They told me to try the community.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.


Most shared hosting providers block remote access to anything other than SSH, if they provide SSH access to their customers at all.

The primary reason for blocking port access is security. It’s also the primary reason that any VPS or Dedicated Server should be using a firewall configured to block commonly accessed ports and the Private Network IP should be used in place of the public (where possible).

While the web hosting provider could simply block each IP, throttle connections, etc, when you’re a provider that’s hosting 1,000+ clients on a server (not uncommon for the cheap-o providers offering the world $1 - i.e. every single “unlimited” provider on the market), it’d bog service down even more allowing such and they’d spend more time handling issues in this area than elsewhere.

not sure if this qualifies as an answer, but if you haven’t already, you’ll probably want to ensure that port 3306 is open on mysql server ip address. From the webserver with the php pages, you’ll want to issue:

telnet <mysql ip address> 3306

just to make sure there is connectivity. You may wish to install the mysql client on the webserver installation as well, just to check if you are able to login properly. If you are using ubuntu,

apt-get install mysql-client

and then connect with:

mysql -h<mysql public ip> -u<user> -p<password>

Can’t say much about PDO since I’ve never used it, but I had apparently got mysql connectivity some years ago using mysqli. It’s been a while so the information I have below might be incorrect:


function connect() {
                $con = new mysqli ("<host>","<username>","<password>","<database schema>");
                if (mysqli_connect_errno($con)) {
                        echo "Failed to connect to MySQL: " . mysqli_connect_error();

        return $con;