Report this

What is the reason for this report?

How do I connect to a mysql database through PHP, running LAMP Ubuntu 14.04

Posted on February 12, 2015

I am trying to connect using this code. $username = ‘root’; $password = ‘password’; $database = ‘localhost’; $mysqli = new mysqli(“104.236.80.149”, $username, $password, $database); if ($mysqli->connect_errno) { echo “Failed to connect to MySQL: (” . $mysqli->connect_errno . ") " . $mysqli->connect_error; } ?> I get this error, and I assume that it isn’t due to my password being incorrect. Failed to connect to MySQL: (2002) No connection could be made because the target machine actively refused it. Is there something I am missing? Same error occurs if I try connecting with new users I make also. I should mention this is a remote connection.



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!

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.

This question was answered by @ryanpq:

Is your database named localhost? I would double-check this value. The reason you are unable to connect to your database is because by default MySQL does not enable remote connections. While your client is on the same server, by using the public IP address the requests are being routed through the public network interface. Instead of:

$mysqli = new mysqli("104.236.80.149", $username, $password, $database);

You should try:

$mysqli = new mysqli("localhost", $username, $password, $database);

View the original comment

The developer cloud

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

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.