Fix MySQL Socket Error: Can't Connect to Local Server

ERROR 2002 means the MySQL client could not reach the server over its Unix socket. Check the service, find the real socket path, and fix the client config.

  • Beginner
  • Linux VPS
  • 6 min read

By RunRacks Team · Published

If your MySQL client prints ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2), the server is either not running or is listening on a different socket path than the client expects. This guide shows you how to fix the MySQL socket error on a Linux VPS: check the service, find the real socket, correct the client configuration, and confirm the connection works.

Before you start

You need:

  • A Linux VPS running MySQL or MariaDB, with root or sudo access.
  • An SSH session. If you have not connected yet, see Connect to your Linux VPS with SSH.
  • The exact error text from your client, including the path in quotes and the number in brackets.
  • To know which server you run: mysql --version prints either MySQL or MariaDB and the version.

The steps below work on Ubuntu 22.04 and 24.04, Debian 12, and AlmaLinux or Rocky 8–9. Package names differ slightly (mysql-server versus mariadb-server), but the socket logic is the same.

What the socket error actually means

MySQL and MariaDB accept two kinds of local connections:

  • A Unix socket, a file on disk such as /run/mysqld/mysqld.sock. This is what mysql -u root -p uses by default.
  • TCP, usually 127.0.0.1:3306. This is what you get when you pass -h 127.0.0.1.

The number in brackets tells you why the socket connection failed:

  • (2) — No such file or directory. The socket file is missing, so the server is not running or uses another path.
  • (13) — Permission denied. The file exists but your user cannot open it.
  • (111) — Connection refused. You asked for TCP and nothing is listening on that port.

Step 1: Check whether the server is running

Start with the service state. On Ubuntu and Debian the unit is usually mysql for MySQL and mariadb for MariaDB:

Terminal
systemctl status mysql --no-pager
systemctl is-active mysql

If the output says inactive or failed, the socket file will not exist and that alone explains error 2002. Start it:

Terminal
sudo systemctl start mysql

If it starts and immediately stops again, go to the next step before retrying.

Step 2: Read the error log

The reason a server refuses to start is always in the log:

Terminal
sudo journalctl -u mysql -n 50 --no-pager
sudo tail -n 50 /var/log/mysql/error.log

Look for lines such as Can't create/write to file '/run/mysqld/mysqld.sock', No space left on device, or a message about another process holding the data directory. Fix that cause first; the socket error is only a symptom.

Step 3: Find the socket path the server uses

If the service is running but the client still fails, the two sides disagree about the path. List the sockets that exist right now:

Terminal
sudo ss -lx | grep -i mysql

Ask the server binaries what they were compiled and configured to use:

Terminal
my_print_defaults mysqld | grep -i socket
grep -r "socket" /etc/mysql/ 2>/dev/null

Typical defaults:

  • Ubuntu and Debian (MySQL and MariaDB): /run/mysqld/mysqld.sock
  • AlmaLinux, Rocky and other RHEL-family systems: /var/lib/mysql/mysql.sock

If ss -lx shows a socket at a path your client is not using, that is your bug.

Step 4: Start or restart the service

Terminal
sudo systemctl restart mysql
sudo systemctl status mysql --no-pager

On MariaDB, replace mysql with mariadb. After a clean start, ss -lx | grep mysql should show the socket file again.

Step 5: Connect with an explicit socket or over TCP

Test both paths directly so you know which one works:

Terminal
mysql -u root -p --socket=/run/mysqld/mysqld.sock
mysql -u deploy -p -h 127.0.0.1 -P 3306

If the explicit socket works, the server is healthy and only the client default is wrong — fix it in the next step.

Step 6: Fix the socket path in the client config

Client defaults come from the option files. On Ubuntu and Debian, edit the server config:

Terminal
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

On MariaDB the file is usually /etc/mysql/mariadb.conf.d/50-server.cnf, and on RHEL-family systems /etc/my.cnf. Add or correct both sections:

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

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

Save, then restart the service so the server recreates the socket at that path:

Terminal
sudo systemctl restart mysql

For a full map of which file wins on your distribution, see Where is my.cnf? MySQL and MariaDB config files explained.

Step 7: Check permissions on the socket directory

Error (13) is a permissions problem. The socket lives in a directory owned by the mysql user:

Terminal
ls -ld /run/mysqld
sudo chown mysql:mysql /run/mysqld
sudo chmod 755 /run/mysqld

On Ubuntu and Debian, AppArmor can also block access. Check for denials:

Terminal
sudo dmesg | grep -i apparmor | tail -n 20

Check it works

Run a query that reports the socket the server is actually using:

Terminal
mysqladmin -u root -p status
mysql -u deploy -p -e "SELECT VERSION(), @@socket;"

You should see the version and a socket path that matches the file shown by ss -lx | grep mysql. If both commands return output without error 2002, the problem is fixed. Confirm your application connects too — restart PHP-FPM, your app server or the container that talks to the database.

Troubleshooting

  • Error 2002 `(2)` returns after every reboot — /run/mysqld is missing because /run is a tmpfs. Check systemctl cat mysql for RuntimeDirectory=mysqld and add it if it is absent, then run sudo systemctl daemon-reload.
  • Error 2002 `(13)` Permission denied — the socket directory or file has the wrong owner or mode. Set the directory to mysql:mysql with mode 755, and check dmesg for AppArmor denials.
  • Error 2003 `(HY000)` on `127.0.0.1` — the server is not listening on TCP. Look for bind-address and skip-networking in your config; skip-networking disables TCP entirely.
  • Works as root, fails as `deploy` — the user exists but lacks privileges on the database, which is error 1045 rather than a socket problem. See Fix MySQL or MariaDB error 1045: Access denied for user.
  • Server starts then dies with "No space left on device" — check df -h and df -i. A full disk or exhausted inodes stops MySQL from creating the socket file.

Next steps

Was this guide helpful?

Still stuck?

Ask RunRacks AI for a quick answer, or open a ticket and our team will take a look at your server.

Databases

Reset a Forgotten MySQL Root Password on Linux

Recover access to MySQL or MariaDB on a Linux VPS by starting the server with skip-grant-tables, setting a new root password and restarting normally.

  • Intermediate
  • Linux VPS
  • 5 min read

Databases

MySQL Point-in-Time Recovery Using Binary Logs

Recover a MySQL or MariaDB database to an exact moment after a bad write or dropped table by restoring a full backup and replaying binary logs up to the incident.

  • Advanced
  • Linux VPS
  • 7 min read