Error establishing a database connection on a fresh Wordpress installation

I've installed a fresh copy of Wordpress on CentOS 7/Apache but am getting the dreaded "Error establishing a database connection" error message when navigating to the site to complete the setup. I've created a new database for Wordpress to use, along with a user which has all privileges on it.

My MySQL (MariaDB) installation is on a separate host, however I can connect to it fine with the command line client:

mysqlshow -u <wordpress_db_user> -p -h <host ip>

shows information_schema and <wordpress_db_name> in the list of Databases, and

mysql -u <wordpress_db_user> -p -h <host_ip> <wordpress_db_name>

opens a session as expected - however there's nothing to query as WP has not set up its tables yet. I've even tried

sudo -H -u apache bash -c 'mysql -u <wordpress_db_user> -p -h <host_ip> <wordpress_db_name>'

to make sure that SELinux isn't blocking apache from connecting.

Is there anything else I can check to get this working? Most of the guides out there stop at "Have you got the right credentials" and "Have you tried repairing the database"...


The answer, as with many things CentOS, is found in SELinux! While the apache user is able to open network connections, the httpd process (regardless of which user it's running as) isn't by default. The setting that controls it is httpd_can_network_connect, which can be set with

setsebool -P httpd_can_network_connect 1

Thanks to this question for helping me to the solution using audit2why!