MySQL Error 2002 HY000 - Cannot Connect to MySQL Server - Complete Fix Guide

Mahesh Mahesh Waghmare
7 min read

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 socket
  • ERROR 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:

  1. Press Windows + R, type services.msc
  2. Find MySQL service
  3. Check status (should be “Running”)
  4. 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:

  1. Open XAMPP Control Panel
  2. Start MySQL service
  3. Verify green indicator

WAMP:

  1. Check WAMP icon in system tray
  2. Should be green (all services running)
  3. If not, click icon → Start All Services
Advertisement

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:

  1. Check MySQL is running
  2. Verify socket path in my.cnf
  3. Check file permissions
  4. 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:

  1. Open Windows Defender Firewall
  2. Click Advanced Settings
  3. Inbound RulesNew Rule
  4. Select PortTCP3306
  5. Allow connection
  6. 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

  1. System PreferencesSecurity & PrivacyFirewall
  2. Click Firewall Options
  3. 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.cnf or /etc/my.cnf
  • macOS: /usr/local/mysql/my.cnf or /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:

  1. Save configuration file
  2. Restart MySQL service
  3. Test connection
Advertisement

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:

  1. Stop MySQL service
  2. Start MySQL with --skip-grant-tables
  3. Connect and reset password
  4. 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:

  1. Uninstall MySQL
  2. Clean registry and files
  3. Reinstall fresh

Linux:

sudo apt-get remove --purge mysql-server
sudo apt-get install mysql-server

Quick Fix Checklist

  1. Check MySQL service is running
  2. Verify connection settings (host, port, credentials)
  3. Check socket file exists (Unix) or named pipe (Windows)
  4. Verify port 3306 is not blocked
  5. Check firewall settings
  6. Review MySQL error logs
  7. Test with different connection methods
  8. 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:

  1. Start MySQL service
  2. Verify it’s running
  3. Test connection with correct credentials

If Still Failing:

  1. Check error logs for specific messages
  2. Verify configuration files
  3. Test with different connection methods
  4. 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.

Advertisement
Mahesh Waghmare

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

Read Next