MySQL Connection

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

Fix MySQL Error 2002 can't connect to local server through socket. Covers MySQL not running, wrong socket path, and permission issues.

Mar 12, 2026 4 min read

The Error Message

Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

What Causes MySQL 2002?

MySQL Error 2002 means the MySQL client can't find or connect to the MySQL server's socket file. This usually means MySQL isn't running or the socket path is wrong.

Common Causes

MySQL server is not running

The most common cause — the MySQL service has stopped or failed to start.

Wrong socket file path

Your client is looking for the socket in a different location than where MySQL created it.

Permission issues

The socket file exists but your user doesn't have permission to access it.

Disk full

MySQL cannot create the socket file because the disk is full.

How to Fix It

Step 1: Check if MySQL is running

First verify that the MySQL server process is actually running.

-- Linux:
sudo systemctl status mysql

-- macOS:
brew services list | grep mysql

-- Or check the process:
ps aux | grep mysql

Step 2: Start MySQL if stopped

Start the MySQL service and check the logs if it fails to start.

-- Linux:
sudo systemctl start mysql

-- macOS:
brew services start mysql

-- Docker:
docker start mysql-container

Step 3: Find the actual socket location

Find where MySQL actually creates the socket file.

-- Check MySQL config:
mysql --help | grep socket

-- Or check the config file:
cat /etc/mysql/mysql.conf.d/mysqld.cnf | grep socket

-- Common locations:
-- /var/run/mysqld/mysqld.sock
-- /tmp/mysql.sock
-- /var/lib/mysql/mysql.sock

Step 4: Connect using the correct socket

Either specify the correct socket path or bypass sockets entirely with TCP connection.

-- Specify socket path directly:
mysql -u root -p --socket=/var/run/mysqld/mysqld.sock

-- Or use TCP instead:
mysql -u root -p --host=127.0.0.1 --port=3306

How to Prevent This Error

Set up MySQL to auto-start on boot. Monitor MySQL with a health check script. Use TCP connections (127.0.0.1:3306) in application code instead of socket connections for better portability.

Fix MySQL Errors with AI2SQL

Instead of debugging SQL syntax manually, describe what you need in plain English and let AI2SQL generate the correct query for MySQL.

Try AI2SQL Free

No credit card required

Frequently Asked Questions

What causes MySQL Error 2002?

Error 2002 means the MySQL client can't connect to the server's socket file. The server is either not running, the socket path is wrong, or there are permission issues.

How do I fix 'can\'t connect to local MySQL server through socket'?

First check if MySQL is running (systemctl status mysql). If stopped, start it. If running, find the socket path in the MySQL config and specify it in your connection command.

Should I use socket or TCP to connect to MySQL?

Sockets are faster for local connections but TCP (127.0.0.1:3306) is more portable and works across containers and cloud environments. Use TCP in application code.

Stop Debugging SQL Errors Manually

Describe what you need in plain English. AI2SQL generates correct MySQL queries instantly.

Try AI2SQL Free

No credit card required