Error fix · Server errors

MySQL Error 2002: Can't Connect to Local MySQL Server (Fix)

Fix MySQL and MariaDB error 2002 "Can't connect to local MySQL server through socket" by checking the service, socket path, bind address and firewall.

Error messageERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

By SiteDownCheck Editorial · Updated October 5, 2026 · 4 min read

Error 2002 means the MySQL or MariaDB client could not open a connection to the server at all. This is different from a login failure: the client never reached the point of sending a username. Either nothing is listening where the client is looking, or something blocked the path.

You see it in the mysql command line client, in PHP as SQLSTATE[HY000] [2002], and in WordPress as "Error establishing a database connection". The number in brackets at the end is the operating system error: (2) means the socket file does not exist, (111) means connection refused, and (110) means the connection timed out.

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
ERROR 2002 (HY000): Can't connect to MySQL server on '10.0.0.5' (111)
SQLSTATE[HY000] [2002] Connection refused

Causes, most likely first

  1. The database service is not running. It crashed, was killed by the out-of-memory killer, or failed to start after an update.
  2. Wrong socket path. The client looks for a socket file in a different place than the server created it.
  3. Using localhost when you mean TCP. MySQL clients treat localhost as "use the Unix socket", while 127.0.0.1 forces TCP. Each can fail independently.
  4. The server listens on another address or has networking disabled. bind-address = 127.0.0.1 blocks remote clients, and skip-networking disables TCP entirely.
  5. Firewall or security group blocking port 3306 for remote connections.
  6. Disk full or a corrupt InnoDB state preventing startup.

Step 1: is the server running?

sudo systemctl status mysql       # or: mariadb
sudo journalctl -u mysql -n 50 --no-pager
sudo tail -n 50 /var/log/mysql/error.log

If it is stopped, start it and watch the log for the reason it died:

sudo systemctl start mysql

Common reasons visible in the log are "No space left on device" (check with df -h and df -i), InnoDB recovery failures, and a killed process. Confirm the last one with sudo dmesg | grep -i "killed process". If memory is the problem, reduce innodb_buffer_pool_size or add RAM or swap.

Step 2: find the real socket and listening port

mysqladmin variables 2>/dev/null | grep -w socket
sudo ss -ltnp | grep -E '3306|mysqld|mariadbd'
sudo find / -name "mysql*.sock" 2>/dev/null

If the server's socket is, for example, /run/mysqld/mysqld.sock but the client expects /var/lib/mysql/mysql.sock, align them. Set the client side in /etc/mysql/my.cnf (or /etc/my.cnf):

[client]
socket = /run/mysqld/mysqld.sock

For PHP, set pdo_mysql.default_socket and mysqli.default_socket in php.ini, or connect over TCP instead.

Step 3: try TCP explicitly

mysql -h 127.0.0.1 -P 3306 -u root -p

If TCP works and the socket does not, you have a socket path problem; set the host to 127.0.0.1 in your application as a quick workaround. In WordPress that is DB_HOST in wp-config.php.

Step 4: remote connections

For an application server connecting to a separate database host, check these in order:

  1. Bind address. In /etc/mysql/mysql.conf.d/mysqld.cnf (Debian and Ubuntu) set bind-address to the private IP of the database host, or 0.0.0.0 only if a firewall restricts access. Restart the service.
  2. Grants. The account must be allowed from the client's address: CREATE USER 'app'@'10.0.0.%' IDENTIFIED BY '...'; GRANT ALL ON appdb.* TO 'app'@'10.0.0.%';. A missing grant gives error 1045, not 2002, so grants only matter once the port is reachable.
  3. Firewall. Allow 3306 only from the application server:
sudo ufw allow from 10.0.0.4 to any port 3306 proto tcp

Also check cloud security groups. Run the Port Checker against the database host and port 3306 from outside to see whether it is open. Expect it to be closed to the public internet unless you intentionally exposed it, which is rarely a good idea.

How to verify

Run mysql -e "SELECT 1" locally and from the application host; both should return a result. Reload the failing page and confirm it returns 200 using the Website Health Check. For a remote database, Port Checker shows whether the port is reachable at all, and the IP Lookup helps confirm that your DB_HOST name points at the host you think it does. After the fix, an Uptime Monitor keyword check on a page that reads from the database tells you quickly if it dies again.

Next steps

FAQ

What does the (2) at the end of the error mean?

It is the operating system error number. 2 is "no such file or directory", so the socket file is missing, usually because the server is down or the path is wrong. 111 means connection refused and 110 means timeout.

Why does 127.0.0.1 work but localhost fail?

localhost makes MySQL clients use the Unix socket, while 127.0.0.1 uses TCP. If the socket path is wrong or missing, only TCP works.

Is error 2002 the same as access denied?

No. Access denied (1045) means the server was reached and rejected the credentials. Error 2002 means the connection never reached the server.

Can a full disk cause this?

Yes. MySQL may fail to start or crash when it cannot write, and the result is a missing socket. Check df -h, df -i and the error log.

Get notified when this fix updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.