Connection Refused on connecting to a remote server

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 = “”; $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 browser “”, it’s printing

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

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

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