Linux – ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/thesql/thesql.sock’ (2)

centoslinuxMySQLUbuntuWordpress

I have a Linux server (Centos 7) running WordPress and another Linux server (Ubuntu) running MySQL. Communication between WordPress and the Database was working. However, after a web server reboot, I see the following message in the browser:

This site can't be reached

It was determined that Apache was not running via: ps -ef | grep http

I manually started Apache via: service httpd start

**** I do want Apache to start automatically after a server reboot ****

Now Apache returned the following message in the browser: Error establishing a database connection

After attempting to test the connecting via: mysql

Linux returned the error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

The directory, '/var/lib/mysql/', is empty. I read on the Internet, that when the MySQL service runs, it creates the socket. When exited, the socket file is removed. Not sure this is true or not.

So it appears to be a MySQL socket issue.

Looking forward to everybody's help on this…

Best Answer

If you have Centos/Red Hat/Fedora/Suse, and you have things that seem to work as root or other logged in user but not from an application like a webserver, then check if your instance has selinux on with sestatus.

sestatus

SELinux status: enabled
SELinuxfs mount: /selinux
Current Mode: enforcing

If it is in mode enforcing then you can check audit logs to see if your problem is there. For example to see errors from something running in the httpd program:

ausearch -c "httpd" -m AVC,USER_AVC,SELINUX_ERR,USER_SELINUX_ERR -i

And/or use:

sealert -a /var/log/audit/audit.log > /path/to/mylogfile.txt

For some common problems there exist booleans to tweak the working of the default policies. For a complete listing use getsebool -a

For httpd servers you have an option for accessing specifically some common database ports over network, httpd_can_network_connect_db and more generic full network access, httpd_can_network_connect. This applies if you use php as a module in httpd and it has to access a database over network connection.

If you wan't to change them you use setsebool -P <boolean_name_> {0|1|on|off}. Without the -P the setting will revert on reboot.

An excellent starting point if you do want to read up on selinux: https://wiki.centos.org/HowTos/SELinux