Node.js MySQL Connection Fails with ECONNREFUSED on Port 3306

0
3
Asked By MellowBirch42 On

I'm trying to connect a Node.js application to MySQL by following a setup example, but running the script from the terminal produces an ECONNREFUSED error. The connection attempts are being made to both ::1:3306 and 127.0.0.1:3306. What does this error mean, and what should I check to get the connection working?

2 Answers

Answered By CedarMoon18 On

The addresses in the error are both loopback addresses: `::1` is IPv6 localhost and `127.0.0.1` is IPv4 localhost. If MySQL is configured to use only a Unix socket, or is bound to a different address or port, TCP connections like these will be refused. Check the database configuration and confirm its bind address and port. If the database is running in Docker, don’t use `localhost` from a separate Node.js container; use the MySQL service or container name instead.

MellowBirch42 -

So the first step is to confirm that the database service is running and actually listening on TCP port 3306, rather than changing the Node.js code or credentials.

Answered By QuartzPanda7 On

ECONNREFUSED means Node.js reached the local machine, but nothing accepted the connection on port 3306. Check that MySQL or MariaDB is running and configured to listen on that port. You can test it directly with `mysql -h 127.0.0.1 -P 3306 -u -p`, then verify whether anything is listening on the port with a command such as `ss -lntp | grep 3306`. This is happening before authentication, so it’s probably not a username or password problem yet.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.