Understanding the Error
MySQL is not accepting connections on the specified port/host:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket
ERROR 2003 (HY000): Can't connect to MySQL server on 'host' (111 "Connection refused")
Check if MySQL is Running
sudo systemctl status mysql
sudo systemctl status mariadb
# If not running
sudo systemctl start mysql
Check Port Listening
sudo netstat -tlnp | grep 3306
sudo ss -tlnp | grep mysql
If nothing listening, MySQL isn't bound to any interface.
Check MySQL Bind Address
sudo grep bind-address /etc/mysql/mysql.conf.d/mysqld.cnf
# Or
sudo grep bind-address /etc/my.cnf
If set to 127.0.0.1, MySQL only accepts local connections. For remote access:
bind-address = 0.0.0.0 # Or your server IP
Restart MySQL:
sudo systemctl restart mysql
Firewall Blocking Port 3306
# Check firewall
sudo ufw status
sudo firewall-cmd --list-all
# Allow MySQL port
sudo ufw allow 3306/tcp
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
Check MySQL Error Log
sudo tail -f /var/log/mysql/error.log
sudo journalctl -u mysql -f
Socket Connection Issues
If using socket (localhost):
# Find socket location
sudo grep socket /etc/mysql/my.cnf
# Check socket exists
ls -la /var/run/mysqld/mysqld.sock
# Specify socket in connection
mysql -S /var/run/mysqld/mysqld.sock -u root -p
Resource Exhaustion
Disk full or memory issues:
df -h
free -h
dmesg | tail -20