How to Fix MySQL Connection Refused Error
In this tutorial, you'll learn about How to Fix MySQL Connection Refused Error. We cover key concepts, practical examples, and best practices.
The Problem
Your application or client tries to connect to MySQL and gets:
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)
MySQL is not accepting connections. The server may be stopped, bound to the wrong interface, or blocked by a firewall.
Quick Fix
1. Check if MySQL is running
sudo systemctl status mysql
If inactive or failed, start it:
sudo systemctl start mysql
sudo systemctl enable mysql
Verify the port is listening:
sudo ss -tlnp | grep 3306
Expected output:
LISTEN 0 70 127.0.0.1:3306 0.0.0.0:*
2. Check the bind address
MySQL may be bound only to 127.0.0.1 but your client connects from a different host. Edit /etc/mysql/mysql.conf.d/mysqld.cnf:
# Wrong — only listens on localhost
bind-address = 127.0.0.1
# Right — listen on all interfaces (configure firewall)
bind-address = 0.0.0.0
Or bind to specific IPs:
bind-address = 127.0.0.1,192.168.1.100
Restart MySQL after changing:
sudo systemctl restart mysql
3. Check the port configuration
MySQL may be running on a non-default port:
# Check the configured port
grep port /etc/mysql/mysql.conf.d/mysqld.cnf
# Expected
port = 3306
Connect with explicit port if non-standard:
mysql -u user -p -h hostname -P 3307
4. Check firewall rules
A firewall on the MySQL host may block port 3306:
# UFW
sudo ufw status
sudo ufw allow 3306/tcp
# firewalld
sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload
# iptables
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
For cloud hosts, check the security group or network ACL.
5. Verify the socket file
When connecting via localhost, MySQL uses a Unix socket. If the socket path is wrong:
# Check socket path
grep socket /etc/mysql/mysql.conf.d/mysqld.cnf
# Expected
socket = /var/run/mysqld/mysqld.sock
Connect using the socket explicitly:
mysql -u root -p -S /var/run/mysqld/mysqld.sock
Or force TCP:
mysql -u root -p -h 127.0.0.1 -P 3306
6. Check disk space
MySQL refuses new connections if the disk is full:
df -h
If the data partition is at 100%, free space or move the data directory.
Prevention
- Enable MySQL to start on boot with
systemctl enable mysql. - Monitor MySQL with
mysqladmin pingor a health check script. - Use a monitoring tool to alert on disk usage and MySQL connectivity.
- Document the bind address and port in your deployment runbook.
Common Mistakes with connection refused
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
These mistakes appear frequently in real-world MYSQL code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro