Report this

What is the reason for this report?

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

Posted on June 15, 2021

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.