Skip to content

How to Fix MySQL Connection Refused Error

DodaTech Updated 2026-06-24 3 min read

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 ping or 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

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. 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

### What does "Connection refused" mean in MySQL?

It means the MySQL server is not listening on the target IP and port. Either the service is not running, it is bound to a different interface, or a firewall is blocking the connection.

How do I check if MySQL is listening on all interfaces?

Run sudo ss -tlnp | grep 3306. If it shows 127.0.0.1:3306, MySQL only accepts local connections. If it shows 0.0.0.0:3306, it accepts connections from all interfaces.

Can Docker cause connection refused errors?

Yes. When MySQL runs in a Docker container, it may not be exposed on the host's localhost. Verify port mapping with docker ps and connect using the host IP or host.docker.internal instead of localhost.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro