MySQL Error 2002 HY000 - Cannot Connect to MySQL Server - Complete Fix Guide
Mahesh Waghmare MySQL Error 2002 (HY000) “Can’t connect to MySQL server” is a common connection error that occurs when your application or command-line tool cannot establish a connection to the MySQL server. This comprehensive guide covers all possible causes and solutions.
Understanding Error 2002
Error 2002 typically means:
- MySQL server is not running
- Connection settings are incorrect
- Socket file is missing or inaccessible
- Port is blocked or in use
- Network/firewall issues
- Configuration problems
Common Error Messages:
ERROR 2002 (HY000): Can't connect to local MySQL server through socketERROR 2002 (HY000): Can't connect to MySQL server on 'localhost' (10061)ERROR 2002 (HY000): Can't connect to MySQL server on '127.0.0.1' (10061)
Error Code Breakdown:
- 2002: Connection error code
- HY000: General SQL state
- 10061: Windows-specific “Connection refused” error
Check MySQL Service Status
The most common cause is MySQL service not running.
Windows
Check Service Status:
- Press Windows + R, type
services.msc - Find MySQL service
- Check status (should be “Running”)
- If stopped, right-click → Start
Command Line:
net start MySQL
Check if running:
sc query MySQL
Linux/macOS
Check Service Status:
sudo systemctl status mysql
# or
sudo service mysql status
Start Service:
sudo systemctl start mysql
# or
sudo service mysql start
Enable Auto-start:
sudo systemctl enable mysql
XAMPP/WAMP
XAMPP:
- Open XAMPP Control Panel
- Start MySQL service
- Verify green indicator
WAMP:
- Check WAMP icon in system tray
- Should be green (all services running)
- If not, click icon → Start All Services
Verify Connection Settings
Connection Parameters
Verify your connection settings match MySQL configuration:
Host:
localhost- Uses socket file (Unix) or named pipes (Windows)127.0.0.1- Uses TCP/IP connection::1- IPv6 localhost
Port:
- Default:
3306 - Check if MySQL uses different port
Username/Password:
- Verify credentials are correct
- Check user has proper permissions
Test Connection
Command Line:
mysql -u root -p -h localhost
With specific port:
mysql -u root -p -h 127.0.0.1 -P 3306
Using TCP/IP explicitly:
mysql -u root -p --protocol=TCP -h 127.0.0.1
Connection String Examples
PHP:
$conn = new mysqli('127.0.0.1', 'username', 'password', 'database', 3306);
Python:
import mysql.connector
conn = mysql.connector.connect(
host='127.0.0.1',
port=3306,
user='username',
password='password'
)
Node.js:
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: '127.0.0.1',
port: 3306,
user: 'username',
password: 'password'
});
Socket File Issues
Unix/Linux/macOS Socket Files
MySQL uses socket files for local connections on Unix systems.
Find Socket File Location:
mysql_config --socket
# or
mysql -u root -p -e "SHOW VARIABLES LIKE 'socket';"
Common Locations:
/tmp/mysql.sock/var/run/mysqld/mysqld.sock/var/lib/mysql/mysql.sock
Check Socket File Exists:
ls -la /tmp/mysql.sock
Fix Missing Socket:
- Check MySQL is running
- Verify socket path in
my.cnf - Check file permissions
- Create symlink if needed
Specify Socket in Connection:
mysql -u root -p --socket=/tmp/mysql.sock
Windows Named Pipes
Windows uses named pipes instead of socket files.
Check Named Pipe:
SHOW VARIABLES LIKE 'named_pipe';
Use Named Pipe Connection:
mysql -u root -p --pipe
Port Conflicts
Check if Port is in Use
Windows:
netstat -ano | findstr :3306
Linux/macOS:
sudo lsof -i :3306
# or
sudo netstat -tulpn | grep 3306
Find Process Using Port
Windows:
netstat -ano | findstr :3306
# Note PID, then:
tasklist | findstr <PID>
Linux/macOS:
sudo lsof -i :3306
Change MySQL Port
If port 3306 is in use, change MySQL port:
Edit my.cnf (Linux/macOS):
[mysqld]
port = 3307
Edit my.ini (Windows):
[mysqld]
port = 3307
Restart MySQL and connect with new port:
mysql -u root -p -P 3307
Firewall Settings
Windows Firewall
Allow MySQL Through Firewall:
- Open Windows Defender Firewall
- Click Advanced Settings
- Inbound Rules → New Rule
- Select Port → TCP → 3306
- Allow connection
- Apply to all profiles
Command Line:
New-NetFirewallRule -DisplayName "MySQL" -Direction Inbound -LocalPort 3306 -Protocol TCP -Action Allow
Linux Firewall
UFW (Ubuntu):
sudo ufw allow 3306/tcp
firewalld (CentOS/RHEL):
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
iptables:
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
macOS Firewall
- System Preferences → Security & Privacy → Firewall
- Click Firewall Options
- Add MySQL or allow incoming connections
Configuration Files
Locate Configuration File
Find Config File:
mysql --help | grep "Default options"
Common Locations:
- Windows:
C:\ProgramData\MySQL\MySQL Server X.X\my.ini - Linux:
/etc/mysql/my.cnfor/etc/my.cnf - macOS:
/usr/local/mysql/my.cnfor/etc/my.cnf - XAMPP:
C:\xampp\mysql\bin\my.ini - WAMP:
C:\wamp64\bin\mysql\mysqlX.X\my.ini
Key Configuration Settings
Bind Address:
[mysqld]
bind-address = 127.0.0.1
For remote connections, use:
bind-address = 0.0.0.0
Port:
[mysqld]
port = 3306
Socket File (Unix):
[mysqld]
socket = /tmp/mysql.sock
After Changes:
- Save configuration file
- Restart MySQL service
- Test connection
Network Issues
Test Network Connectivity
Ping MySQL Host:
ping localhost
ping 127.0.0.1
Test Port Connectivity:
telnet 127.0.0.1 3306
# or
nc -zv 127.0.0.1 3306
DNS Resolution Issues
If using hostname instead of IP:
Check Hosts File:
- Windows:
C:\Windows\System32\drivers\etc\hosts - Linux/macOS:
/etc/hosts
Add Entry:
127.0.0.1 localhost
IPv6 vs IPv4
Force IPv4:
mysql -u root -p -h 127.0.0.1 --protocol=TCP
Force IPv6:
mysql -u root -p -h ::1 --protocol=TCP
Advanced Troubleshooting
Check MySQL Error Logs
Find Log Location:
SHOW VARIABLES LIKE 'log_error';
Common Locations:
- Windows:
C:\ProgramData\MySQL\MySQL Server X.X\Data\*.err - Linux:
/var/log/mysql/error.log - macOS:
/usr/local/var/mysql/*.err
View Recent Errors:
tail -f /var/log/mysql/error.log
Verify MySQL User Permissions
Check User Exists:
SELECT User, Host FROM mysql.user WHERE User='your_username';
Grant Permissions:
GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
Test with Different Connection Methods
Method 1: Socket (Unix):
mysql -u root -p --socket=/tmp/mysql.sock
Method 2: TCP/IP:
mysql -u root -p -h 127.0.0.1 --protocol=TCP
Method 3: Named Pipe (Windows):
mysql -u root -p --pipe
Reset MySQL Root Password
If authentication is the issue:
Windows:
- Stop MySQL service
- Start MySQL with
--skip-grant-tables - Connect and reset password
- Restart normally
Linux/macOS:
sudo mysqld_safe --skip-grant-tables &
mysql -u root
ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
FLUSH PRIVILEGES;
Check MySQL Process
Verify MySQL is Running:
ps aux | grep mysql
# or
pgrep -l mysql
Check Process Details:
ps aux | grep mysqld
Reinstall/Repair MySQL
If all else fails:
Windows:
- Uninstall MySQL
- Clean registry and files
- Reinstall fresh
Linux:
sudo apt-get remove --purge mysql-server
sudo apt-get install mysql-server
Quick Fix Checklist
- ✅ Check MySQL service is running
- ✅ Verify connection settings (host, port, credentials)
- ✅ Check socket file exists (Unix) or named pipe (Windows)
- ✅ Verify port 3306 is not blocked
- ✅ Check firewall settings
- ✅ Review MySQL error logs
- ✅ Test with different connection methods
- ✅ Verify user permissions
Conclusion
Error 2002 is usually caused by:
- MySQL service not running (most common)
- Incorrect connection settings
- Socket/named pipe issues
- Port conflicts or firewall blocks
Most Common Fix:
- Start MySQL service
- Verify it’s running
- Test connection with correct credentials
If Still Failing:
- Check error logs for specific messages
- Verify configuration files
- Test with different connection methods
- Review network and firewall settings
With systematic troubleshooting, Error 2002 can be resolved quickly. Start with service status, then work through connection settings, and finally check advanced configuration if needed.
Written by Mahesh Waghmare
I bridge the gap between WordPress architecture and modern React frontends. Currently building tools for the AI era.
Follow on Twitter →