Question
Why do I receive a database error when specifying "localhost" in the WordPress database connection information? (Specifying 127.0.0.1 allows for a successful connection).
Answer
MySQL will try to connect to the unix socket if you tell it to connect to "localhost". If you tell it to connect to 127.0.0.1 you are forcing it to connect to the network socket.
If you have already confirmed that the database connection information is valid and a connection is able to be made when using 127.0.0.1.
If using CloudLinux, please try remounting and updating CageFS with the following command:
cagefsctl -M
cagefsctl --force-update
The issue may be that the /etc/my.cnf doesn't have a datadir / socket specified.
Default settings for the datadir and socket are noted below:
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
You may also experience issues on CloudLinux if the directory containing the MySQL socket file is not present in the CageFS map file. You can add the directory to the CageFS map file like so:
1) Open the /etc/cagefs/cagefs.mp file in you preferred editor. For example:
vim /etc/cagefs/cagefs.mp
nano /etc/cagefs/cagefs.mp
2) Add the directory containing the MySQL socket file to the bottom of the file. If the MySQL socket is located at /var/lib/mysql/mysql.sock, you would add the following.
/var/lib/mysql
3) Remount CageFS for all users with the following command.
cagefsctl -M