NestJS connects to MySQL at ::1 and is refused
Error: connect ECONNREFUSED ::1:3306Short answer
::1 is the IPv6 loopback address. Your app resolved localhost to it, but MySQL is listening only on IPv4 127.0.0.1, so the connection is refused. Set the host to 127.0.0.1, or make Node prefer IPv4 addresses. You can also change MySQL's bind_address so it listens on IPv6 too.
Fix it fast
1Find out which cause you have
Ask the OS which addresses MySQL is listening on. If you see 127.0.0.1:3306 but no [::1]:3306, the cause is the address family.
sudo ss -ltnp | grep 33062Match what you see, then jump to its fix
What does the error look like?
Error: connect ECONNREFUSED ::1:3306
Cause 1 of 2
The client resolves localhost to ::1 while MySQL listens on IPv4 only
1Check
Node's dns.lookup returns addresses in the order the operating system gives them (the default order is verbatim), so localhost can come back as ::1 first.
node -e "require('dns').lookup('localhost',{all:true},(e,a)=>console.log(a))"
mysql -h 127.0.0.1 -P 3306 -u root -p -e "select 1"2Fix
Use the IPv4 address as the host in the TypeORM options. Alternatively tell Node to put IPv4 first for the whole process.
TypeOrmModule.forRoot({
type: 'mysql',
host: '127.0.0.1',
port: 3306,
// ...
})
// main.ts, before NestFactory.create
import dns from 'node:dns';
dns.setDefaultResultOrder('ipv4first');3Verify
Start the app; the connection is made and the error is gone.
npm run start:devConfirmed by nodejs.org
Cause 2 of 2
MySQL is bound to an address that excludes the one the client uses
1Check
MySQL accepts connections only on the exact address when bind_address is a regular IPv4 or IPv6 address.
mysql -u root -p -e "SHOW VARIABLES LIKE 'bind_address'"2Fix
Set bind_address to * (all IPv4 and IPv6 interfaces if the host supports IPv6) or :: in the MySQL option file, then restart the server. Only open it beyond loopback if you also restrict access with a firewall.
# my.cnf, [mysqld] section
bind_address = *3Verify
The listening sockets now include an IPv6 one.
sudo ss -ltnp | grep 3306Confirmed by dev.mysql.com
How do I stop it coming back?
- Write 127.0.0.1 instead of localhost in database settings.
- Take the host from an environment variable so containers and laptops can differ.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixDatabases
NestJS cannot open an SSL connection to a managed Postgres database
SELF_SIGNED_CERT_IN_CHAIN
TypeORM passes its ssl option to node-postgres, which hands it to Node's TLS layer.
4 causes, each with a fixDatabases
Node.js MySQL: Cannot enqueue Query after a fatal error
code: 'PROTOCOL_ENQUEUE_AFTER_FATAL_ERROR'
This error is a symptom, not the problem.
2 causes, each with a fixDatabases
Node.js cannot reach MySQL: connect ETIMEDOUT
code: 'ETIMEDOUT'
ETIMEDOUT on connect means packets to the database port got no answer at all.
4 causes, each with a fixDatabases
MySQL says Too many connections to a Node.js app
Too many connections
MySQL refuses new clients once max_connections slots are in use.
3 causes, each with a fixDatabases
Node.js says the MySQL server closed the connection
code: 'PROTOCOL_CONNECTION_LOST'
The error means the MySQL server, or something between it and your app, closed a connection your code still held.
3 causes, each with a fix