Question

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

  • Posted 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.

Subscribe
Share

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);

The code you have shared thus far does not include anything that would write content to a browser. If you are getting a blank page and no errors in your log files the script is likely running as written so I would double-check what in your code appears to be executing vs what is not to track down the source of the problem.

Im getting a blank page… Any solutions for that


Submit an 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