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
sudoaccess. - 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 --versionprints 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 whatmysql -u root -puses 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:
systemctl status mysql --no-pager
systemctl is-active mysqlIf the output says inactive or failed, the socket file will not exist and that alone explains error 2002. Start it:
sudo systemctl start mysqlIf 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:
sudo journalctl -u mysql -n 50 --no-pager
sudo tail -n 50 /var/log/mysql/error.logLook 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:
sudo ss -lx | grep -i mysqlAsk the server binaries what they were compiled and configured to use:
my_print_defaults mysqld | grep -i socket
grep -r "socket" /etc/mysql/ 2>/dev/nullTypical 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
Warning: Restarting MySQL disconnects every open session and can interrupt in-progress writes. Do it during a quiet window, and take a backup first if the data matters.
sudo systemctl restart mysql
sudo systemctl status mysql --no-pagerOn 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:
mysql -u root -p --socket=/run/mysqld/mysqld.sock
mysql -u deploy -p -h 127.0.0.1 -P 3306Note: -h localhost still uses the Unix socket on most builds. Use -h 127.0.0.1 when you specifically want TCP.
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:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnfOn 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:
[client]
socket = /run/mysqld/mysqld.sock
[mysqld]
socket = /run/mysqld/mysqld.sockSave, then restart the service so the server recreates the socket at that path:
sudo systemctl restart mysqlFor 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:
ls -ld /run/mysqld
sudo chown mysql:mysql /run/mysqld
sudo chmod 755 /run/mysqldTip: /run is a temporary filesystem, so anything you create there by hand disappears at reboot. The systemd unit normally recreates /run/mysqld through its RuntimeDirectory setting. If you had to create the directory manually, check the unit file with systemctl cat mysql and confirm that setting exists.
On Ubuntu and Debian, AppArmor can also block access. Check for denials:
sudo dmesg | grep -i apparmor | tail -n 20Check it works
Run a query that reports the socket the server is actually using:
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/mysqldis missing because/runis a tmpfs. Checksystemctl cat mysqlforRuntimeDirectory=mysqldand add it if it is absent, then runsudo systemctl daemon-reload. - Error 2002
(13)Permission denied — the socket directory or file has the wrong owner or mode. Set the directory tomysql:mysqlwith mode755, and checkdmesgfor AppArmor denials. - Error 2003
(HY000)on127.0.0.1— the server is not listening on TCP. Look forbind-addressandskip-networkingin your config;skip-networkingdisables 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 -handdf -i. A full disk or exhausted inodes stops MySQL from creating the socket file.
Next steps
- Lock down the install and decide who may connect remotely: Install and secure MariaDB or MySQL, and allow remote access safely.
- Learn the config file layout so future changes land in the right place: Where is my.cnf? MySQL and MariaDB config files explained.
- Locked out of the account as well as the socket? Reset a forgotten MySQL root password on Linux.
- Connect a desktop client over SSH instead of a local socket: Connect MySQL Workbench, HeidiSQL or DBeaver to your VPS over SSH.
