0

I'm new to web development. I bought a server on DigitalOcean, created a droplet and installed LAMP. I have a simple PHP script to connect to MySQL. Here's my PHP script(conn.php)

<?php
$servername = "xxx.xx.xx.xxx";
$username = "root";
$password = "my_password";
try {
    $conn = new PDO("mysql:host=$servername;dbname=mysql", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
}catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>

Now, when I try to run on my local "xxx.xx.xx.xxx/conn.php", it's printing

Connection failed: SQLSTATE[HY000] [2002] Connection refused
amit_vickey
  • 45
  • 1
  • 9
  • https://stackoverflow.com/questions/1676688/php-mysql-connection-not-working-2002-no-such-file-or-directory – ßiansor Å. Ålmerol Oct 22 '17 at 07:18
  • Have you followed https://www.digitalocean.com/community/tutorials/how-to-set-up-a-remote-database-to-optimize-site-performance-with-mysql#configure-mysql-to-allow-remote-access? – Nigel Ren Oct 22 '17 at 07:28

0 Answers0