Connection Refused on connecting to a remote server

October 22, 2017 2.9k views

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)

$servername = “”;
$username = “root”;
$password = “mypassword”;
try {
$conn = new PDO(“mysql:host=$servername;dbname=mysql”, $username, $password);
// set the PDO error mode to exception
echo “Connected successfully”;
}catch(PDOException $e) {
echo “Connection failed: ” . $e->getMessage();
Now, when I try to run on my browser “”, it’s printing

Connection failed: SQLSTATE[HY000] [2002] Connection refused

1 Answer

Is MySQL running? For remote connections, use a SSH tunnel, MySQL is plaintext.

Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!