By vin2164382
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.
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!
Hi there @vin2164382,
In order to be able to access your MySQL service remotely, you would need to do a couple of things:
127.0.01 to bind-address = 0.0.0.0 so that MySQL could accept connections from remote servers and not only localhost.ALTER USER 'sammy'@'remote_server_ip' IDENTIFIED WITH mysql_native_password BY 'password';
Let me know how this goes! Regards, Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.