error: ‘Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)’ — Missing /var/run/mysqld/mysqld.sock

To find all socket files on your system run:

sudo find / -type s

My Mysql server system had the socket open at /var/lib/mysql/mysql.sock

Once you find where the socket is being opened, add or edit the line to your /etc/my.cnf file with the path to the socket file:

socket=/var/lib/mysql/mysql.sock

Sometimes the system startup script that launched the command line executable specifies a flag --socket=path. This flag could override the my.cnf location, and that would result in a socket not being found where the my.cnf file indicates it should be. Then when you try to run the mysql command line client, it will read my.cnf to find the socket, but it will not find it since it deviates from where the server created one. So, Unless you care where the socket resides, just changing the my.cnf to match should work.

Then, stop the mysqld process. How you do this will vary by system.

If you’re super user in the linux system, try one of the following if you don’t know the specific method your Mysql setup uses:

  • service mysqld stop
  • /etc/init.d/mysqld stop
  • mysqladmin -u root -p shutdown
  • Some systems aren’t setup to have an elegant way to stop mysql (or for some reason mysql doesn’t respond) and you can force terminate mysql with either:
    • One step: pkill -9 mysqld
    • Two step (least preferred):
      • Find the process id of mysql with either pgrep mysql or ps aux | grep mysql | grep -v grep
      • Assuming the process id is 4969 terminate with kill -9 4969

After you do this you might want to look for a pid file in /var/run/mysqld/ and delete it

Make sure the permissions on your socket is such that whatever user mysqld is running as can read/write to it. An easy test is to open it up to full read/write and see if it still works:

chmod 777 /var/run/mysqld/mysqld.sock

If that fixes the issue, you can tailor the permissions and ownership of the socket as needed based on your security settings.

Also, the directory the socket resides in has to be reachable by the user running the mysqld process.

Leave a Comment