Resolving PHP-MySQL Connection Issues

I ran into an interesting issue when installing WordPress on my re-installed server, I could not get a database connection during installation. I added some debugging and discovered that I had a “Can’t connect to MySQL server on” error returned after the call to mysql_connect() in PHP.

To check the source of the issue I then tried to connect on the command-line using the mysql client, which occurred successfully, confirming that I was using the correct credentials and host address (this was a remote MySQL server).

I next created a test PHP script with a simple mysql_connect() call, and executed it with “php test.php” from the command-line, which was also successful.

Finally I accessed test.php through a browser, where again the connection failed.

So I was dealing with a situation where it was Apache in particular that was unable to connect to the remote MySQL server. Thanks to Wez Furlong‘s ideas I was able to narrow this down to SELinux blocking outgoing communications by Apache.

So, if you’re having issues with Apache and specifically connecting out, you may want to disable SELinux for Apache.