Why is my Node.js app getting ECONNREFUSED when connecting to MySQL?

0
2
Asked By MellowCactus42 On

I'm following a Node.js and MySQL connection example, but running the script produces an ECONNREFUSED error for 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 QuietHarbor7 On

ECONNREFUSED means your Node.js process reached the local machine, but nothing accepted the connection on port 3306. Check that MySQL or MariaDB is actually running and configured to listen on that port. You can also test it directly with `mysql -h 127.0.0.1 -P 3306 -u -p`. If the database uses a different port or host, update your Node.js connection settings accordingly.

Answered By SilverMaple_8 On

The addresses in the error are both loopback addresses: `::1` is IPv6 localhost and `127.0.0.1` is IPv4 localhost. This usually isn’t a username or password problem—the connection fails before authentication. If MySQL is running but still refuses the connection, check whether it is listening for TCP connections rather than only using a local socket. Also, if MySQL is in Docker, `localhost` from a Node.js container refers to the Node container itself; use the database service name instead.

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.