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

November 21, 2015 5.6k views
MySQL PHP Firewall Ubuntu

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.

1 comment
  • Unfortunately this is unsolvable. When I contacted the support of the web server hosting company again, they told me that neither incoming or outgoing requests to another database was allowed on their servers. Despite that they told me earlier this morning that outgoing requests was allowed.

2 Answers

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:

(http://php.net/manual/en/mysqli.quickstart.connections.php)

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;

        }
  • Thanks, unfortunately I haven't got access to the web server - server, only the web server - webspace (hope that makes sense), aka I haven't got access to the command line.

    I forgot to update this thread, but I found the issue. Unfortunately unsolvable. When I contacted the support of the web server hosting company again, they told me that neither incoming or outgoing requests to another database was allowed on their servers. Despite that they told me earlier this morning that outgoing requests was allowed

@9focuspoints

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.

Have another answer? Share your knowledge.