MySQL 2002 Can't connect through socket: fix in order
MySQL server isn't running or the socket path is wrong. Here's how to fix it from quickest to most thorough.
What's actually happening here
Error 2002 (HY000) means the MySQL client can't find the mysql.sock file. That file only exists while the MySQL server (mysqld) is running. So either:
- The server isn't running at all
- The server started but crashed right after
- Your client is looking in the wrong directory for the socket
This usually hits you after a reboot, a failed update, or when you've just installed MySQL and haven't started it yet. I see it most often on Ubuntu 20.04+ and CentOS 7 boxes.
30-second fix: start the MySQL service
Run this immediately:
sudo systemctl start mysql
On some distros it's mariadb instead of mysql:
sudo systemctl start mariadb
Check if it's running:
sudo systemctl status mysql
If you see "active (running)" in green, you're done. The socket file appears at /var/run/mysqld/mysql.sock (or /var/lib/mysql/mysql.sock on older systems) the moment the process starts.
Why this works: systemd creates the socket file as part of the service start-up. If MySQL ran normally before a reboot, this single command brings it back.
If systemctl start fails with "Unit mysql.service not found" — you probably have MariaDB instead. Install it with sudo apt install mariadb-server or sudo yum install mariadb-server, then start it.
5-minute fix: check why MySQL won't start
If the server won't start, systemctl status will show "failed" or "inactive (dead)". Now we dig.
1. Look at the logs
sudo journalctl -u mysql --no-pager | tail -50
Or find the error log directly:
sudo tail -100 /var/log/mysql/error.log
Common causes I've hit:
- Disk full — MySQL refuses to start.
df -h /var/lib/mysql - Corrupted ibdata1 — InnoDB data file got messed up. You'll see "Corrupted page" or "innodb_force_recovery" references.
- Port conflict — Another MySQL instance or process on port 3306.
sudo ss -tlnp | grep 3306 - Permission issue — The
mysqluser doesn't own/var/lib/mysql.sudo chown -R mysql:mysql /var/lib/mysql
2. Try safe mode
If the log mentions InnoDB corruption, start MySQL with recovery:
sudo mysqld_safe --skip-grant-tables &
Then in another terminal:
mysql -u root
If that works, you know the issue is in the data, not the config. Dump what you can with mysqldump and restore fresh.
3. Verify the socket path in your client
Sometimes MySQL is running, but your client looks in the wrong place. Check with:
mysqladmin variables | grep socket
That shows the actual socket path the server is using. Then connect explicitly:
mysql -S /var/run/mysqld/mysql.sock
If that works, your client's config (usually /etc/mysql/my.cnf or ~/.my.cnf) has a bad socket line. Fix it.
15+ minute fix: rebuild the socket file and fix the root cause
If the socket file is missing and starting the service doesn't create it, something is fundamentally broken. Here's the nuclear option.
1. Kill any stuck MySQL processes
sudo pkill -9 mysqld
sudo pkill -9 mysqld_safe
sleep 2
sudo rm -f /var/run/mysqld/mysql.sock /var/lib/mysql/mysql.sock
The rm -f is safe — if the socket is there, it's from a dead or orphaned process. A fresh start will recreate it.
2. Recreate the socket directory
sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql /var/run/mysqld
On some systems (like Arch or manual installs), this directory doesn't get created at boot. MySQL expects it to exist and fails silently.
3. Start MySQL with full debug output
sudo mysqld --user=mysql --console
This runs in the foreground and prints every error to your terminal. You'll see things like:
[ERROR] Can't start server: Bind on TCP/IP port: Address already in use— kill whatever's on port 3306[ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist— you need to reinitialize the database
4. Reinitialize the database (last resort)
If the error log says privilege tables are missing or corrupted, back up /var/lib/mysql (if you care about the data) and reinitialize:
sudo mv /var/lib/mysql /var/lib/mysql.bak
sudo mkdir -p /var/lib/mysql
sudo chown mysql:mysql /var/lib/mysql
sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
For MySQL 8.0+, the command is mysqld --initialize instead:
sudo mysqld --initialize --user=mysql
That creates a temporary root password printed to the log. Grab it with sudo grep 'temporary password' /var/log/mysql/error.log and log in immediately to change it.
The reason step 3 works: mysqld --initialize creates the mysql system tables (including the socket path config) from scratch. After this, systemctl start mysql will succeed and the socket file appears.
One last thing: check your my.cnf for typos
A common footgun I've seen — someone edits /etc/my.cnf and puts a stray character in the [mysqld] section. MySQL silently ignores the whole section if the syntax is wrong. Check for:
- Missing semicolons on
socketandpid-filelines - Blank lines with spaces
- Uppercase
MYSQLinstead ofmysqldin the section header
If you're not sure, rename the file and try a default start:
sudo mv /etc/my.cnf /etc/my.cnf.bak
sudo systemctl start mysql
If that works, your config was the problem. Restore it line by line.
Was this solution helpful?