I know this error is infuriating—you're trying to connect to MySQL and it just says "Can't connect through socket." But the fix is usually simple: the MySQL service isn't running.
The Quick Fix: Restart MySQL
Most of the time, the socket file /var/run/mysqld/mysqld.sock doesn't exist because the MySQL daemon (mysqld) hasn't started. Maybe it crashed, or the server rebooted and MySQL didn't come back.
First, check if the service is running:
sudo systemctl status mysql
If it shows inactive (dead) or failed, start it:
sudo systemctl start mysql
sudo systemctl status mysql
That's it. Nine times out of ten, this resolves the error instantly. If you're on an older system using service, run sudo service mysql start instead.
Why This Works
When you connect via localhost or mysql -u root, the MySQL client uses a Unix socket file to talk to the server. That socket file is created by mysqld when it starts. If the server is down, the file doesn't exist, and you get error 2002.
Restarting the service makes mysqld create the socket file at the expected location. Simple as that.
If the service was already running and you still get this error, the socket path might be different. Check your MySQL config:
mysql_config --socket
# or look in /etc/mysql/my.cnf or /etc/mysql/mysql.conf.d/mysqld.cnf
Look for a line like socket = /var/run/mysqld/mysqld.sock. If it's different, either change the config or use that socket explicitly in your client command.
Less Common Variations
Permission Issues on the Socket Directory
Sometimes the /var/run/mysqld directory is missing or has wrong permissions. This happened to me once after a system update. The fix is to create the directory and give MySQL ownership:
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
Then restart MySQL.
AppArmor Blocking the Socket
On Ubuntu, AppArmor can block MySQL from creating the socket if the path isn't allowed. Check the audit logs (sudo tail /var/log/syslog) for AppArmor messages. If you're in a dev environment, you can disable AppArmor for MySQL, but that's not a great idea for production. Better to tweak the profile if needed.
MySQL Installed but Not Initialized
If you just installed MySQL and get this error, the data directory might be empty. Run sudo mysqld --initialize (as mysql user) and then start the service. On some distros, you need to run sudo mysql_secure_installation first.
Prevention
Most socket errors stem from the service not running. So the real prevention is ensuring MySQL starts automatically and stays healthy.
- Enable MySQL to start on boot:
sudo systemctl enable mysql - Set up a cron job to check and restart MySQL if it stops, or use systemd restart settings.
- Watch disk space—if the partition fills up, MySQL can crash and fail to restart.
One more thing: if you're using a hosting provider or a container, the socket path might be different. Check your environment's docs.
That's the whole story. Start the service, check the socket path, and you'll be back in business.