ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run

Fix 'Database connection failed' on MySQL 8.0

Database Errors Beginner 👁 0 views 📅 May 26, 2026

Quick answer: restart the MySQL service. If that fails, check the socket file and grant correct permissions. This error usually means the MySQL daemon isn't running or can't find its socket.

Quick answer

Run sudo systemctl restart mysql and then sudo systemctl status mysql to confirm it's running. If you still see the error, check that /var/run/mysqld/mysqld.sock exists and has the right permissions.

Why this happens

This error pops up when your PHP script, Python app, or command-line client tries to connect to MySQL but can't find the socket file. MySQL uses a Unix socket for local connections — it's faster than TCP on the same machine. When MySQL crashes, gets shut down improperly, or runs out of memory, the socket file gets deleted. The daemon might still be running in background but with a broken socket. I've seen this most often after a server reboot or a failed backup script that left MySQL in a weird state. On shared hosting, it's often because the hosting provider restarted the service without warning.

Here's the thing: the socket path depends on your MySQL config. Default on Ubuntu/Debian is /var/run/mysqld/mysqld.sock. On CentOS/RHEL it's /var/lib/mysql/mysql.sock. If you changed socket in /etc/mysql/mysql.conf.d/mysqld.cnf, you'll need to adjust the paths in the steps below.

Fix steps (in order)

  1. Check if MySQL is running. Run sudo systemctl status mysql (on older systems use sudo service mysql status). If it says inactive (dead) or failed, move to step 2. If it says active (running), the socket might be corrupted — skip to step 3.
  2. Restart the MySQL service. Run sudo systemctl restart mysql. Wait 10 seconds. Then run sudo systemctl status mysql again. You should see active (running). If it fails to start, check the logs: sudo tail -30 /var/log/mysql/error.log. Common reasons: disk full, incorrect config permissions, or a corrupted table.
  3. Verify the socket file exists. Run ls -la /var/run/mysqld/mysqld.sock. If the file is missing, MySQL is running but didn't create the socket — usually a permissions issue. Run sudo chown mysql:mysql /var/run/mysqld and sudo chmod 755 /var/run/mysqld. Then restart MySQL again.
  4. Check the MySQL config for the socket path. Run grep socket /etc/mysql/mysql.conf.d/mysqld.cnf. You should see something like socket = /var/run/mysqld/mysqld.sock. If it's different, use that path in step 3.
  5. Try connecting with explicit socket. If the socket exists but the client still can't find it, specify it manually: mysql -u root -p --socket=/var/run/mysqld/mysqld.sock. If that works, you have a config mismatch — the client expects a different socket. Edit /etc/mysql/my.cnf and add [client] section with socket = /var/run/mysqld/mysqld.sock.

Alternative fixes if the main one fails

  • Check for leftover MySQL processes. Run ps aux | grep mysql. If you see multiple mysqld processes, kill them all with sudo pkill -9 mysqld, then restart the service. Stale processes can block the socket creation.
  • Repair the MySQL installation. On Ubuntu: sudo apt-get install --reinstall mysql-server. This reinstalls the binaries but keeps your data in /var/lib/mysql.
  • Switch to TCP connection. As a workaround, connect using TCP instead of socket: mysql -u root -p -h 127.0.0.1. This bypasses the socket entirely. Not ideal for performance but gets you working.
  • Check disk space. Run df -h. If /var is 100% full, MySQL can't write the socket. Free up space — delete old logs, clear apt cache with sudo apt-get clean.

Prevention tip

Set up a cron job to check MySQL every 5 minutes and restart it if the socket disappears. Here's a one-liner for your crontab:
* * * * * mysqladmin ping --silent || systemctl restart mysql
This runs every minute. If ping fails, it restarts the service. I've used this on dozens of production servers — it catches the error before anyone notices. Also, always use systemctl enable mysql so MySQL starts automatically after a reboot.

Was this solution helpful?