# MySQL Connection Refused: Cannot Connect to Server

You attempt to connect to your MySQL server and receive a frustrating error:

bash
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

Or perhaps:

bash
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (111)

These connection refused errors indicate that your MySQL client cannot establish a connection to the server. Let me walk you through diagnosing and resolving this common issue.

Understanding the Error

The "connection refused" message means one of several things:

  1. 1.MySQL server is not running
  2. 2.MySQL is running but not on the expected port or socket
  3. 3.MySQL is not listening for network connections
  4. 4.A firewall is blocking the connection
  5. 5.The client is connecting to the wrong host or port

Step 1: Check if MySQL is Running

First, verify that MySQL is actually running on your system.

On Linux (systemd):

bash
systemctl status mysql

Or for older systems:

bash
service mysql status

On macOS with Homebrew:

bash
brew services list | grep mysql

On Windows:

cmd
sc query MySQL

If MySQL is not running, start it:

```bash # Linux sudo systemctl start mysql

# macOS brew services start mysql

# Windows net start MySQL ```

Step 2: Verify the MySQL Port

MySQL typically runs on port 3306. Check if MySQL is listening on this port:

```bash # Linux/macOS sudo netstat -tlnp | grep mysql # or sudo ss -tlnp | grep 3306

# Check the actual port MySQL is using sudo lsof -i :3306 ```

Inside MySQL, check the port configuration:

sql
SHOW VARIABLES LIKE 'port';

If MySQL is running on a non-standard port, you need to specify it when connecting:

bash
mysql -u root -p --port=3307

Step 3: Check the Socket File

For local connections, MySQL uses a socket file. Verify it exists:

bash
ls -la /var/run/mysqld/mysqld.sock
# or check alternative locations
ls -la /tmp/mysql.sock

Find the actual socket location from MySQL configuration:

sql
SHOW VARIABLES LIKE 'socket';

If the socket file doesn't exist, MySQL may not be running, or it may be using a different location.

Connect with the correct socket path:

bash
mysql -u root -p --socket=/tmp/mysql.sock

Step 4: Enable Network Connections

If you're connecting remotely and getting connection refused, MySQL might not be configured to accept network connections.

Check the bind address in your MySQL configuration:

bash
# Find your config file location
mysql --help | grep "Default options" -A 1

Edit the configuration file (typically /etc/mysql/mysql.conf.d/mysqld.cnf or /etc/my.cnf):

ini
[mysqld]
# Change from 127.0.0.1 to allow remote connections
bind-address = 0.0.0.0

Restart MySQL after making changes:

bash
sudo systemctl restart mysql

Step 5: Check Firewall Rules

Firewalls can block MySQL connections. Check your firewall status:

On Linux (ufw):

bash
sudo ufw status
sudo ufw allow 3306/tcp

On Linux (firewalld):

bash
sudo firewall-cmd --list-all
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload

On Linux (iptables):

bash
sudo iptables -L -n | grep 3306
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT

Step 6: Verify Connection Parameters

Double-check your connection parameters. A typo in host or port causes connection refused:

```bash # Connect with explicit parameters mysql -h 127.0.0.1 -P 3306 -u root -p

# For remote connections mysql -h your-server-ip -P 3306 -u username -p ```

Step 7: Check MySQL Error Logs

If problems persist, examine MySQL's error log for clues:

bash
# Common log locations
sudo tail -100 /var/log/mysql/error.log
# or
sudo tail -100 /var/log/mysqld.log
# or
sudo journalctl -u mysql -n 100

The error log often reveals startup failures, permission issues, or configuration problems.

Verification

After applying fixes, verify the connection works:

```bash # Test local connection mysql -u root -p -e "SELECT 1;"

# Test with verbose output mysql -u root -p --verbose -e "SHOW STATUS LIKE 'Connections';" ```

Quick Reference Checklist

  • [ ] MySQL service is running
  • [ ] Correct port (default 3306)
  • [ ] Socket file exists for local connections
  • [ ] bind-address allows your connection type
  • [ ] Firewall permits MySQL port
  • [ ] Connection parameters are correct
  • [ ] No errors in MySQL log files

Common Scenarios

Scenario: Docker container

```bash # Check if MySQL container is running docker ps | grep mysql

# Connect to MySQL in Docker docker exec -it mysql-container mysql -u root -p ```

Scenario: MySQL installed but not initialized

```bash # Initialize MySQL data directory (MySQL 5.7+) sudo mysqld --initialize sudo systemctl start mysql

# Get temporary root password sudo grep 'temporary password' /var/log/mysql/error.log ```

With these steps, you should be able to identify and resolve most MySQL connection refused errors. The key is methodically checking each potential cause starting with whether MySQL is actually running.