First, make sure MySql is actually running. It will not create a socket file if the process is not running.
netstat -tulpn | grep mysql
or
ps -e | grep mysql
If MySql is running, changing the database host from localhost to 127.0.0.1 in wp-config.php works, but this is only a workaround.
When you specify localhost , the mysqli_real_connect() function tries to connect to your database via a Unix socket that it cannot find (hence the error "there is no such file.") When you specify 127.0.0.1 , it tries to connect to its database using The default TCP port (usually 3306), which works.
This does not fix the problem that PHP does not know where to find your MySql socket. You need to configure the following parameters in php.ini . The location in the socket will depend on your OS, but you can usually find it by running locate mysql.sock . Here are the settings that work for me on CentOS 6.8.
php.ini
pdo_mysql.default_socket = /var/lib/mysql/mysql.sock mysqli.default_socket = /var/lib/mysql/mysql.sock
Other systems typically use /tmp/mysqld.sock . You can check the configured location by checking the MySql my.cfg configuration file.
After you have configured PHP to indicate the correct location of the sockets, restart Apache / nginx so that it picks up the new settings.
Now you can use localhost as intended.
source share