Skip to content

MySQL "ERROR 2002: Can't connect through socket /var/lib/mysql/mysql.sock" on cPanel

Nothing can reach MySQL — every client returns ERROR 2002 and the socket file is missing. Either mysqld is down or it is listening somewhere the client is not looking.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' means a local client tried to reach MySQL over its Unix socket and found nothing there. The socket file is created by mysqld when it starts and removed when it stops, so a missing socket has exactly two meanings: either the server is not running, or it is running but the socket lives at a path the client is not looking in. This is different from ERROR 1045 (wrong password) or 1040 (too many connections) — those mean MySQL answered and said no. A 2002 means nothing answered at all. The fix is to determine which of the two situations you are in, because they pull in opposite directions.

What’s happening

On a cPanel host, local connections — the cPanel daemon, phpMyAdmin, WordPress with localhost as the DB host — do not use TCP. They use the Unix domain socket, a special file (/var/lib/mysql/mysql.sock by convention) that mysqld creates on startup. The client library reads the socket path from /etc/my.cnf, a per-user ~/.my.cnf, or its compiled-in default, and connects to that path.

A 2002 arises when those two facts disagree, or when the file is simply absent. The most common cause is that mysqld is not running at all — it crashed, was OOM-killed, failed to start after a config change, or /var/lib/mysql filled to 100% and the server refused to come up. The second cause is a path mismatch: mysqld is running and created its socket at one path, but the client is configured to look at another (this happens after migrations, after editing my.cnf, or when a stale ~/.my.cnf points somewhere wrong). A third, sneakier cause is a stale socket file left behind by an unclean shutdown, which can confuse startup. Telling these apart takes about thirty seconds.

How to verify

First and most important — is mysqld actually running?

# Is the daemon alive?
systemctl status mysql 2>/dev/null || systemctl status mariadb
ps aux | grep -i [m]ysqld
/scripts/restartsrv_mysql --status     # cPanel's own view

# Does the socket file exist, and where?
ls -l /var/lib/mysql/mysql.sock
mysql -e "SHOW VARIABLES LIKE 'socket';" 2>/dev/null   # only works if you CAN connect

If the daemon is dead, find out why before restarting it blindly — the error log holds the reason:

# The startup failure or crash reason lives here
tail -60 /var/lib/mysql/$(hostname).err

# Two classic blockers: disk full and a crash on startup
df -h /var/lib/mysql
grep -iE 'crash|corrupt|cannot|denied|error' /var/lib/mysql/$(hostname).err | tail -20

If the daemon is running but you still get 2002, you have a path mismatch. Find where the server actually put the socket versus where the client is looking:

# Where the running server's socket truly is
ps aux | grep -i [m]ysqld    # look for --socket=... in the args, or check the datadir
find /var -name 'mysql.sock' 2>/dev/null

# What the client config says
grep -r socket /etc/my.cnf /etc/my.cnf.d/ ~/.my.cnf 2>/dev/null

The fix

  1. Free the disk if /var/lib/mysql is full — a 100%-full datadir partition stops mysqld from writing and it will not start, producing a 2002 for everyone. Confirm with df -h /var/lib/mysql, then reclaim space: clear old binlogs with PURGE BINARY LOGS BEFORE ... if the server can start in recovery, remove stale dumps, or extend the volume. Do not delete files inside /var/lib/mysql itself. Once there is headroom, start the server.
  2. Start mysqld and read the log if it refuses — systemctl start mysql (or /scripts/restartsrv_mysql). If it fails, the answer is in /var/lib/mysql/$(hostname).err: a corrupt InnoDB log, a bad setting just added to my.cnf, or permission damage on the datadir. Fix the named cause rather than retrying the start. A config typo means revert the edit; an InnoDB crash means an InnoDB recovery, not a socket fix.
  3. Repair a socket path mismatch in my.cnf — if mysqld is running but the client looks in the wrong place, make the two agree. In /etc/my.cnf, set the same socket=/var/lib/mysql/mysql.sock under both [mysqld] and [client] so the server creates it where the client expects it. Restart MySQL after editing so the server recreates the socket at the corrected path.
  4. Clear a stale ~/.my.cnf — a leftover socket path in root’s or a user’s ~/.my.cnf overrides the global config and sends the client to a path that no longer exists. Check cat ~/.my.cnf; remove or correct any socket= line that points somewhere wrong. This is a frequent cause right after a server move.
  5. Remove a stale socket file blocking startup — if mysqld is down and an old mysql.sock (or mysql.sock.lock) lingers from an unclean shutdown, it can interfere with a clean start. With the daemon confirmed stopped, remove the stale socket file, then start MySQL so it creates a fresh one. Only do this when you are certain no mysqld process is running, or you will orphan the live server’s socket.
  6. Fix datadir ownership and permissions — if the error log shows permission denials, mysqld cannot create its socket or read its files. The datadir and socket directory must be owned by the mysql user: chown -R mysql:mysql /var/lib/mysql and confirm the directory is mode 0751 or similar. cPanel’s /scripts/restartsrv_mysql will surface permission problems in its output.
  7. Repoint clients that hard-code the wrong host — applications that connect to 127.0.0.1 get TCP, not the socket, and an app set to localhost gets the socket; a mismatch after a migration shows up as 2002 in one and works in the other. For a WordPress site, localhost in wp-config.php uses the socket — correct if mysqld is fine but the socket path moved, or switch the app to 127.0.0.1 to bypass the socket entirely while you fix the path.

The single decision that drives everything: is mysqld down, or just unreachable at the expected socket path. A dead server needs a cause-specific restart; a running server with a 2002 needs the client and server pointed at the same socket. Stack Harbor brings MySQL back online and fixes socket-path drift under cPanel server administration — start a conversation when nothing can reach the database.