Why is my Node.js app getting ECONNREFUSED when connecting to MySQL?
Asked by
MellowCactus42
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?
Answers
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.
Reply to this answer
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.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions