How I solved the database connection error from my WordPress installation which is hosted on the default PHP FPM server in Mac OS X EL Capitan

I was trying to setup a WordPress instance with the built-in PHP-FPM server on my Mac OS X el Capitan. After applying the relevant Nginx configurations for WordPress installations, I had been able to run the php codes from my WordPress instance.

However, when I tried to run the WordPress install script, I was slapped with the following message in my browser:

Error establishing a database connection
This either means that the username and password information in your wp-config.php file is incorrect or we can’t contact the database server at localhost. This could mean your host’s database server is down.

Are you sure you have the correct username and password?
Are you sure that you have typed the correct hostname?
Are you sure that the database server is running?
If you’re unsure what these terms mean you should probably contact your host. If you still need help you can always visit the WordPress Support Forums.

This post documents how I solved the database connection error coming from my WordPress installation which is hosted on the default PHP FPM server that comes with my OS X El Capitan.

Why was my WordPress installation not being able to connect to MySQL database server on my Mac OS X EL Capitan

When my WordPress setup was not able to connect to my MySQL database server, the first thing that I did was to check the relevant MySQL settings that my php binary had taken.

To do so, I ran the following command in my Terminal program:

php -i | grep 'mysql'

From the output of this call, I get to know the following:

  1. MySQL had been enabled.
  2. The default socket that php would use to talk to a MySQL database server was /var/mysql/mysql.sock

With that, I went on to check whether the /var/mysql/mysql.sock file exists on my file system. It turned out that /var/mysql/mysql.sock does not exist on my file system, which was probably why my php binary was not able to connect to my MySQL database server for my WordPress setup.

Figuring out where MySQL database server had placed the mysql.sock file

Since I could use the mysql command to access and create a database instance for my WordPress installation, there should be a mysql.sock file lying somewhere on my computer. To do so, I ran the following command in my Terminal program:

mysql_config --socket

This command tells me that MySQL database server had placed the mysql.sock file in the /tmp folder. Since my php binary was being configured to read the mysql.sock file from another location, it could never been able to connect to the MySQL database server on my Mac.

Finding out where is the php.ini file that my php binary is loading configurations from

To find out the location of the php.ini file that is responsible for my php configurations, I ran the following command in my Terminal program:

php --ini

This command gave me the following output:

Configuration File (php.ini) Path: /etc
Loaded Configuration File:         (none)
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed:      (none)

This tells me that there was no php.ini file that was being processed by my php binary.

Creating the php.ini file and configuring the default mysql socket for my default php binary

To be able to get my php binary to take in my configurations, I will need to supply it with a php.ini file in the /etc folder. Luckily for me, there was a php.ini.default file in the /etc folder which I can reference from.

I went on to create the php.ini file from the php.ini.default file by running the following command in my Terminal program:

sudo cp php.ini.default php.ini

I then fired up my text editor and look for the line containing the mysql.default_socket variable and change it to the following:

mysql.default_socket = /tmp/mysql.sock

After I had saved the changes made to the php.ini file, I ran the following command again to get my php binary to take in the configurations that I had created:

php --ini

This time, the command returned me the following result:

Loaded Configuration File:         /etc/php.ini
Scan for additional .ini files in: /Library/Server/Web/Config/php
Additional .ini files parsed:      (none)

This meant that the php.ini file that I had created was read by the php binary.

I then restart my PHP-FPM server so that it could take the new configurations.

After the PHP-FPM server had restarted, my WordPress installation was then able to allow me to continue on to the famous five-minute WordPress installation process. Database connection error from WordPress solved!

About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.