Question

Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '161.xxx.xxx.xxx' (13)

Posted June 15, 2021 635 views
PHPDatabases

Hi guys ,

I have a problem connecting the database from a database VPS server to another VPS server. My problem is that I am writing a web page that has a connection to a database. When I practice connecting the database from XAMPP ,on my local computer, to the database server VPS, it works fine, and there are no problems. But when I install Apache + PHP on another VPS and then connect to the database VPS server, it throws an error like below:

Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '161.xxx.xxx.xxx' (13)

‘161.xxx.xxx.xxx this is the database server IP that I want to access. I tried to set up mysql remote access, and tried to check if it can control from web VPS to database VPS, it works normally.

Here is the config.php file I used PDO to connect to the database:

<?php
$host = "161.xxx.xxx.xxx";
$username = "username";
$password = "my_pass";
$dbName = "my_db";

try{
    $conn = new PDO("mysql:host=$host; dbname=$dbName", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
}catch(PDOException $e){
    echo "Connection failed: " . $e->getMessage();
}

$conn = null;
?>

Can you help me how to connect the database on the website from 1 VPS to a VPS containing the database. And these 2 VPSs are from the digital Ocean.

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.

×
Submit an Answer
1 answer

Hi there @vin2164382,

In order to be able to access your MySQL service remotely, you would need to do a couple of things:

  • If you have a firewall, first make sure to open the incoming connections to port 3306 on your MySQL server from the remote server
  • Then change the bind address for your MySQL service from 127.0.01 to bind-address = 0.0.0.0 so that MySQL could accept connections from remote servers and not only localhost.
  • Finally alter the MySQL user to allow connections from your remote server IP:
ALTER USER 'sammy'@'remote_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';

Let me know how this goes!
Regards,
Bobby

  • I have reinstalled 2 VPSs. VPS1 for mysql server and VPS2 for web server, but I can’t remote mysql from VPS2 to VPS1, and it shows error like below:

    ERROR 2003 (HY000): Can't connect to MySQL server on '206.189.157.86' (111)
    

    I turned off all firewalls on VPS1, and also opened port 3306 for it. But still can’t solve this problem.

    [root@huyettu3d ~]# systemctl restart mysqld.service
    [root@huyettu3d ~]# systemctl status firewalld
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: inactive (dead)
         Docs: man:firewalld(1)
    [root@huyettu3d ~]# systemctl status iptables
    ● iptables.service - IPv4 firewall with iptables
       Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
       Active: inactive (dead)
    [root@huyettu3d ~]# 
    
    

    Do you have a way to fix it?

    • Hi there,

      I could suggest testing out the network connectivity on port 3306 with telnet, so from your web server run:

      telnet 206.189.157.86 3306
      

      If this does not work, it is possible that port 3306 is closed for outgoing TCP connections on your webserver, I could suggest making sure that you open that too.

      Also you could test the MySQL connections with the following command:

      mysql -u your_user -h 206.189.157.86 -p
      

      I tried this already and the connection from my PC to the server seems to be successful, of course, I don’t have the password so I was not able to authenticate but the network connectivity is all set up well I believe.

      However note that this is a security concern, as you would want to lock down the MySQL service for the rest of the world and only allow your web server to access it.

      Let me know how it goes.
      Regards,
      Bobby