MySQL isn't accessible with error 'Can't connect to local MySQL server through socket'. What's wrong with MySQL?

Linode Staff

My website suddenly doesn't work and I notice this error in my logs:

SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

What's going on?

1 Reply

This error is pretty straight forward -- whatever you're trying to connect to MySQL with (like PHP) isn't able to find the socket file.

For reference, a socket file is just a connection endpoint that Linux uses that allows services (Like PHP and MySQL) to communicate with each other. So, it will show up like a regular file, but it won't actually contain data.

There's a few steps you can take to see what's going on:

1- Make sure your connection settings are right. Check your code, username, and password.

2- Check to make sure that MySQL is running. You can check with these commands, or you can run ps aux | grep mysql

$ systemctl status mysql
$ service mysql status

3- Check for any recent out of memory error messages on your console, in dmesg, or in /var/log/syslog

If you see an error message like this, you might want to consider upgrading your Linode or checking your memory usage with top.

Out of memory: Kill process 12531 (mysql) score 52 or sacrifice child
Killed process 12531 (mysql) total-vm:96884kB, anon-rss:12003kB, file-rss:0kB, shmem-rss:8k

4- Check your disk usage to make sure you have enough disk space available.

5- Check to make sure that the socket file is present (in case it's in a different location):

$ sudo find / -type s | grep mysql

If it's not present, check to see if you have a different socket file defined in your (/etc/mysql/) my.cnf file. If it's not defined in your my.cnf file, you can try to define it.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct