Skip to content

ERROR 2003: Can't Connect to MySQL Server on 'localhost:3306'

ERROR 2003 means the client's TCP connect never reached a MySQL server. The number in brackets tells you whether it was refused, dropped or unroutable, and that points straight at bind-address, the port, a firewall or Docker.

mysql-error-2003 connection-refused bind-address mysql-connections docker mysql mysql-errors
Gopi Gorantala
Reading Progress

On This Page

# /etc/mysql/mysql.conf.d/mysqld.cnf  (Ubuntu's mysql-server package)
bind-address        = 127.0.0.1
mysqlx-bind-address = 127.0.0.1

Those two lines ship in Ubuntu's MySQL server config, and they're behind a big share of the "Can't connect to MySQL server on" errors I get asked about. The server is up. It's healthy. It answers every connection from its own machine. And every other machine on earth gets this:

What the client printed

ERROR 2003 (HY000): Can't connect to MySQL server on '10.0.4.12:3306' (111)

That's the format from the MySQL 26.7 client error reference: error 2003, symbol CR_CONN_HOST_ERROR, text Can't connect to MySQL server on '%s:%u' (%d). Every client-side error carries SQLSTATE HY000, so the (HY000) tells you nothing. I couldn't install a MySQL server or the mysql CLI in my lab workspace this time (no access to the package mirrors or Docker Hub), so that line comes from the reference, filled in with a private address. Everything else below I ran.

The variants you'll actually see:

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (111)
ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1:3306' (10061)
ERROR 2003 (HY000): Can't connect to MySQL server on 'db.internal:3306' (110)
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)")
Error: connect ECONNREFUSED ::1:3306

The first three are the mysql CLI; no port in the quotes means an old 5.7-era client. Then PyMySQL's usual wording (quoted, not from my run), then Node's mysql2, which hands you the raw OS code. Java's Connector/J wraps all of it in Communications link failure, its own article.

It shows up at connect time, before any query. That's the useful bit. Nothing about your password, your database name or your grants has been looked at yet.

Refused, timed out, or unreachable?

The client library raised 2003 because the operating system's connect() call on a TCP socket failed. The number in brackets is that call's error. Your whole triage hangs on it:

Bracket OS meaning Who said no
(111) Linux, (61) macOS, (10061) Windows Connection refused Something on the path answered with a TCP reset: nothing listening on that address and port, or a firewall rejecting on its behalf
(110) Linux, (60) macOS, (10060) Windows Timed out Nobody answered at all. Packets dropped, usually by a firewall or security group, or the host is down
(113) Linux No route to host A router or firewall sent back "host unreachable"

The MySQL manual's page on this error only spells out 10061; the rest are the standard OS codes.

Then check from both ends:

# from the client: does anything accept TCP on that host and port?
nc -vz -w 5 10.0.4.12 3306

# on the database host: who listens on 3306 and 33060, and on which address?
sudo ss -ltnp | grep -E ':3306|:33060'

# on the database host, over the local socket (works even when TCP doesn't)
sudo mysql -e "SELECT @@bind_address, @@port, @@skip_networking, @@version"

If ss shows 127.0.0.1:3306, you've found it, and the rest of this post is detail. If it shows *:3306 or 0.0.0.0:3306 and you still get (111) from outside, something between you and the box is rejecting. If it shows nothing, the server isn't listening on TCP at all.

One discriminator before going further. If you get ERROR 1130 or ERROR 1045 instead, congratulations, you got further: TCP worked and the server turned you away at the account check. That's the 1130 article or the 1045 one, and the comparison of 2002, 2003, 1045 and 1130 maps how far each one got. And if it says 2002 and mentions a socket path, the client never tried TCP; that's ERROR 2002.

Break it on purpose, three ways

Since 2003 is decided by the OS before MySQL speaks a single byte, you don't need MySQL to reproduce it. A fake listener and a plain TCP probe show exactly what the client sees. I did this on Ubuntu 24.04 (kernel 6.18), Python 3.13.18, PHP 8.3.6 with mysqlnd 8.3.6, as root in a throwaway container. The example is an FX rates loader that connects to a rates schema as app; the credentials never get used.

The probe does the same connect() the mysql client does, then waits to see whether a MySQL server says hello (a real one sends its greeting first):

# tcpprobe.py HOST PORT
import socket, sys, time, errno
host, port = sys.argv[1], int(sys.argv[2])
t = time.time()
try:
    s = socket.create_connection((host, port), timeout=5)
    s.settimeout(3)
    try:
        print(f"{host}:{port} connected, first bytes: {s.recv(64)[:20]!r}")
    except socket.timeout:
        print(f"{host}:{port} connected, but nothing said hello in 3s")
except OSError as e:
    print(f"{host}:{port} -> ({e.errno}) {errno.errorcode.get(e.errno, e)}  {time.time()-t:.2f}s")
  1. bind-address = 127.0.0.1, simulated. Start a listener on loopback only, the way Ubuntu's config makes mysqld listen:
python3 -c 'import socket,time; s=socket.socket(); s.bind(("127.0.0.1",3306)); s.listen(5); time.sleep(60)' &
python3 tcpprobe.py 127.0.0.1 3306      # from the box itself
python3 tcpprobe.py 172.17.0.1 3306     # same box, a different interface (the docker0 bridge address)
127.0.0.1:3306 connected, but nothing said hello in 3s
172.17.0.1:3306 -> (111) ECONNREFUSED  0.00s

Same listener. Through loopback you're in; through any other address the kernel refuses instantly, because nothing is bound there. A remote client gets exactly that (111).

  1. localhost inside a container. A container has its own network namespace with its own loopback. unshare --net gives you one without Docker (loup.py is a five-line ioctl that brings lo up, since this image has no ip command):
sudo unshare --net sh -c "python3 loup.py && python3 tcpprobe.py 127.0.0.1 3306"
127.0.0.1:3306 -> (111) ECONNREFUSED  0.00s

The listener is still running on the host. From inside the namespace, 127.0.0.1 is the namespace's own loopback, and nothing listens there. That's your app container dialling localhost in Compose.

  1. The firewall versions. Two lab-only iptables rules, one that drops and one that rejects:
sudo iptables -A INPUT -p tcp --dport 3309 -j DROP
sudo iptables -A INPUT -p tcp --dport 3310 -j REJECT --reject-with icmp-host-unreachable
python3 tcpprobe.py 127.0.0.1 3310
python3 -c 'import socket; s=socket.socket(); s.setsockopt(socket.IPPROTO_TCP, socket.TCP_SYNCNT, 2); s.connect(("127.0.0.1",3309))'
127.0.0.1:3310 -> (113) EHOSTUNREACH  0.00s
OSError: [Errno 110] Connection timed out     (after 7.14s)

I capped the SYN retries at 2 to get (110) in seconds. This kernel's default is 6 (/proc/sys/net/ipv4/tcp_syn_retries), which means a dropped connect hangs for a couple of minutes unless the client's own connect timeout fires first.

How you know it worked: the bracket numbers match the table. Same probe through PHP's mysqlnd, for comparison:

host=172.17.0.1 port=3306 -> [2002] Connection refused  (0.00s)
host=127.0.0.1 port=3309 -> [2002] Connection timed out  (5.01s)
host=127.0.0.1 port=3310 -> [2002] No route to host  (0.00s)

PHP calls all of these 2002. The mysql CLI calls them 2003. In PHP, read the text, not the code.

Teardown: sudo iptables -D INPUT -p tcp --dport 3309 -j DROP, the same -D for the 3310 rule, and kill the background listener. Don't leave a DROP rule behind on a shared host; the next person to hit that port will lose an afternoon.

The short answer

Your client opened a TCP connection to a host and port, and the OS couldn't complete it. Either no MySQL process listens on that address and port, or something in between refused or swallowed the packets. MySQL never saw you.

Why the server can be up and still refuse you

mysqld listens for classic-protocol TCP connections on port (3306 by default) at the addresses in bind_address. In the 8.4 system variables reference the default is *, meaning every IPv4 interface and, where the host supports it, every IPv6 one. Current releases also accept a comma-separated list of addresses. bind_address isn't dynamic: change it in the option file and restart.

So upstream MySQL listens everywhere. Packagers change that. Ubuntu's package pins both bind-address and mysqlx-bind-address to 127.0.0.1. The second one is for the X Protocol on port 33060 (MySQL Shell and the X DevAPI), and the X Plugin options page gives it the same * default. That's why "it works on my laptop" falls apart the minute the app moves to another VM: on the laptop, the app was the box itself.

The other way to get (111) from a running server is skip_networking. With it on, the server doesn't listen on TCP at all. Socket clients like sudo mysql still work, which is why they prove nothing here, and everything that dials an IP fails. Docker Desktop on a Mac or Windows adds a third: if you didn't publish the port with -p 3306:3306 (or ports: in Compose), nothing on the host listens, whatever mysqld does inside.

Now the dead end, and it's mine from this lab. To get a timeout, I first pointed the PHP probe at 203.0.113.10, an address reserved for documentation, expecting it to hang. It came back (111) in zero seconds. The sandbox's egress rules were rejecting outbound connections on the remote host's behalf. Which matters more than it sounds: (111) doesn't prove the database host said no. A firewall configured to REJECT rather than DROP sends the same reset. If ss on the server shows a listener on the right address and the client still gets (111), look for a REJECT rule or a cloud network ACL between them.

And the surprise. A listener that accepts the connection but never speaks MySQL doesn't produce 2003 at all. My fake loopback listener got PHP to [2006] MySQL server has gone away after the 5-second read timeout I'd set in the probe. 2003 is strictly about TCP. Once the three-way handshake completes, you're in a different family of errors, the handshake ones covered under ERROR 2013. A port-forward to the wrong service, or a load balancer with no healthy backend that still accepts, lands you there instead.

Fixing "Can't connect to MySQL server on" by bracket number

(111) against localhost or 127.0.0.1. Nothing listens locally on that port. Check the process and the port it really uses:

systemctl status mysql            # mysqld on RPM-based distros
sudo tail -n 50 /var/log/mysql/error.log
sudo ss -ltnp | grep mysqld       # is it on 3306, or did someone set port=3307?

If the server won't start, fix that; 2003 is only the symptom. If it runs on another port, use that port. If @@skip_networking is 1, remove skip-networking from the option file and restart, or connect over the socket on purpose.

(111) from another machine, and ss shows 127.0.0.1:3306. Change bind-address to the server's private address, the one your apps reach it on:

[mysqld]
bind-address = 10.0.4.12

Restart (sudo systemctl restart mysql; it drops every current connection, so pick your moment). Binding to * or 0.0.0.0 also works, but only do that on a host where a firewall or security group already limits who can reach 3306. After this, expect the next error to be 1130, because your app's account probably only exists as 'app'@'localhost'. Create the account for the app's subnet, with the grants it needs, not 'root'@'%':

CREATE USER 'app'@'10.0.4.%' IDENTIFIED BY 'use-a-real-secret';
GRANT SELECT, INSERT, UPDATE, DELETE ON rates.* TO 'app'@'10.0.4.%';

Why 'app'@'localhost' and 'app'@'10.0.4.%' are two separate accounts is a whole post: MySQL accounts are user@host.

(111) and the server listens on the right address. Something is rejecting. Check the host firewall (sudo ufw status, sudo iptables -S, firewall-cmd --list-all) and anything in the network path.

(110) or (10060). Packets are dropped silently. Almost always a cloud security group, a network ACL or a host firewall with DROP. nc -vz -w 5 host 3306 from the client's own network, not from your laptop, tells you whether 3306 is open from where the app actually runs.

From a container. In Compose, connect to the service name (db:3306), never localhost. From the host to a container, publish the port and use 127.0.0.1 with the published port.

Node and ECONNREFUSED ::1:3306. Since Node 17, localhost can resolve to ::1 first while MySQL listens only on 127.0.0.1. Node 22.23.5 reports net.getDefaultAutoSelectFamily() as true and tries both families; on older Node, put 127.0.0.1 in the config. (No IPv6 in my workspace, so I didn't reproduce this one.)

What isn't a fix: opening 3306 to 0.0.0.0/0 in a security group, or GRANT ALL ON *.* TO 'root'@'%' "to rule out permissions". Permissions aren't involved yet, and the open port is how scanners find you.

What I'd do instead

Decide where MySQL listens when you build the host, and write it into config management, not into a ticket. One private interface in bind-address, a security group that allows 3306 only from the app subnets, one app account per service scoped to that subnet, and the X Protocol bound to loopback or off if nothing uses it. Then apps get one connection string per environment with an explicit host and port, never localhost:

# fx-rates-loader, staging
spring.datasource.url=jdbc:mysql://mysql-staging.internal:3306/rates

The test that would have caught this

Check from where the app runs, not from the database host. A deploy step or CI job that runs nc -vz -w 5 mysql-staging.internal 3306 from the app's network, then mysqladmin ping -h mysql-staging.internal -P 3306 --protocol=TCP, catches a loopback-only bind before the app does. In Compose, give the database a healthcheck over TCP (mysqladmin ping -h 127.0.0.1) so depends_on with condition: service_healthy means something. In monitoring, probe 3306 from the app subnet; a probe from the database host itself will happily report green on a server bound to 127.0.0.1.

On versions: the 2003 number, CR_CONN_HOST_ERROR and its '%s:%u' (%d) format come from the 26.7 client error reference, and the * defaults for both bind variables from the 8.4 reference; 9.7.4 (2026-10-20) is the newest LTS patch I could confirm. What changes between machines is packaging, so re-check @@bind_address after moving between apt, Homebrew, Docker and a managed service. For this post I ran every OS outcome and the namespace case through a raw TCP probe and mysqlnd, and read the reference entries for 2002, 2003, 2005 and 2013 plus the manual's "can't connect" page.

The short list

  • 2003 is a TCP failure. MySQL never saw you, so passwords and grants are irrelevant until it's gone.
  • Read the bracket: (111)/(10061) refused, (110)/(10060) dropped, (113) no route.
  • A healthy server bound to 127.0.0.1 refuses every other machine. SELECT @@bind_address over the local socket settles it in a second.
  • (111) can come from a REJECT firewall rule too, not only the database host.
  • Inside a container, localhost is the container. Use the service name.

People also ask about ERROR 2003

How do I fix ERROR 2003 (111) on Ubuntu when connecting remotely?

Ubuntu's MySQL package sets bind-address = 127.0.0.1 in /etc/mysql/mysql.conf.d/mysqld.cnf. Change it to the server's private IP, restart MySQL, allow port 3306 from your app's network in the firewall, and create an account for the app's host or subnet. Expect ERROR 1130 until that account exists.

What does (10061) mean in "Can't connect to MySQL server"?

It's the Windows code for "connection refused": nothing accepted a TCP connection on that host and port. Check that the MySQL service is running, which port it uses, and that you published the port if MySQL runs in Docker. (10060) is the Windows timeout, which points at a firewall instead.

Why does MySQL work on localhost but not from another computer?

Local tools use 127.0.0.1 or the Unix socket, and both work when the server listens only on loopback. Remote clients need the server bound to an address they can reach and a firewall that lets 3306 through; after that, an account whose host part matches them.

mysql-error-2003connection-refusedbind-addressmysql-connectionsdockermysqlmysql-errors

Staff engineer architecting Kafka and Flink platforms for banking, where a dropped event is a reconciliation break, not a retry. Through javahandbook.com, I share production-tested guidance on building reliable event-driven systems.

Comments