What's Actually Happening
When you get a MySQL connection refused error, the client cannot establish a TCP connection to the MySQL server. This means the server isn't listening on the expected host and port, or network issues block the connection.
The Error You'll See
```bash $ mysql -u root -p ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
# Or for TCP connection: ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111) ```
Why This Happens
- 1.MySQL not running - Server stopped or crashed
- 2.Wrong host/port - MySQL listening elsewhere
- 3.Socket file missing - Unix socket deleted
- 4.Bind address - MySQL only on localhost, not remote
- 5.Firewall - Port 3306 blocked
- 6.Permission denied - Socket file permissions
Step 1: Check MySQL Service Status
# Check if MySQL is running
systemctl status mysql
# Or:
systemctl status mysqld
# Or:
systemctl status mariadbIf not running:
systemctl start mysql
systemctl enable mysqlStep 2: Check MySQL Process
ps aux | grep mysqlShould show mysqld process:
mysql 1234 0.5 5.0 1234567 123456 ? Ssl 10:00 0:01 /usr/sbin/mysqldStep 3: Check MySQL Port
# Check if MySQL is listening
netstat -tlnp | grep mysql
# Or:
ss -tlnp | grep mysqlOutput:
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1234/mysqldIf empty, MySQL not listening on any port.
Step 4: Check MySQL Socket
```bash # Find socket location mysqladmin -u root variables | grep socket
# Check socket exists ls -la /var/run/mysqld/mysqld.sock # Or: ls -la /tmp/mysql.sock ```
If missing:
# Restart MySQL to recreate socket
systemctl restart mysqlStep 5: Check MySQL Bind Address
# Check config
grep bind-address /etc/mysql/mysql.conf.d/mysqld.cnfDefault:
bind-address = 127.0.0.1This only accepts localhost connections.
To allow remote connections:
```bash sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# Change to: bind-address = 0.0.0.0
# Restart systemctl restart mysql ```
Step 6: Test Local Connection
```bash # Connect via socket mysql -u root -p --socket=/var/run/mysqld/mysqld.sock
# Connect via TCP mysql -u root -p -h 127.0.0.1 -P 3306 ```
Step 7: Check Firewall
For remote connections:
```bash # Check firewall sudo iptables -L -n | grep 3306
# For ufw sudo ufw status
# Allow MySQL port sudo ufw allow 3306/tcp
# For firewalld sudo firewall-cmd --add-port=3306/tcp --permanent sudo firewall-cmd --reload ```
Step 8: Check MySQL Error Log
tail -f /var/log/mysql/error.logLook for errors:
Can't start server: Bind on TCP/IP port: Address already in use
Can't create IP socket: Permission denied
Fatal error: Can't open and lock privilege tablesStep 9: Check Disk Space
df -h /var/lib/mysqlIf disk full, MySQL can't start:
# Free space
rm /var/log/mysql/old-logs
# Or increase disk sizeStep 10: Check Data Directory Permissions
ls -la /var/lib/mysqlShould be owned by mysql user:
drwxr-xr-x mysql mysql /var/lib/mysqlFix:
chown -R mysql:mysql /var/lib/mysql
chmod 750 /var/lib/mysqlVerify the Fix
```bash # Test connection mysql -u root -p -e "SELECT 1;"
# Check server responds mysqladmin -u root -p ping # mysqld is alive
# Test from remote host mysql -u root -p -h mysql-server-ip -P 3306 ```
Prevention Tips
```bash # Enable MySQL to start on boot systemctl enable mysql
# Monitor MySQL health mysqladmin -u root -p status
# Set up alerts # Prometheus: mysql_up == 0 ```