To open a TCP connection, Node.js sends a request to a host and port. If the machine answers “nothing here” instead of accepting, you get ECONNREFUSED with the address it tried. The two parts of that address tell you what to check: the host (where) and the port (which service).
| Port in the message | Usually |
|---|---|
3306 |
MySQL or MariaDB |
5432 |
PostgreSQL |
6379 |
Redis |
27017 |
MongoDB |
3000, 5000, 8000 |
Another local app or API |
Causes
- The service isn’t running, for example MySQL stopped on your laptop or never installed.
- Wrong host or port in the configuration, often a missing or unread
.envfile, so the code falls back to a default. localhostresolves to::1(IPv6) while the service listens on127.0.0.1only.- The service exists only on your computer. On the server,
localhostmeans the server itself. - The app calls itself on a fixed port like
http://localhost:3000, but in production it runs on a different port. - Startup order: the app starts before the database is ready.
- A remote service refuses your server’s IP or listens on another port.
How to fix it
-
Read the host and port in the message and decide which service it should be.
-
Check that something is listening there. On your own machine:
lsof -i :3306 nc -zv 127.0.0.1 3306If nothing answers, start the service (for example
brew services start mysqlon macOS orsudo systemctl start mysqlon Linux). -
Print the configuration your app actually uses at startup (without passwords). Make sure
.envis loaded before you read it:require('dotenv').config(); console.log('DB host:', process.env.DB_HOST, 'port:', process.env.DB_PORT); -
Replace
localhostwith127.0.0.1if the error shows::1. -
Don’t call your own app through
localhost:3000. From the browser use relative paths (/api/...) or the public URL; on the server, read the port fromprocess.env.PORT. -
For remote databases or APIs, check the exact host and port in the provider’s dashboard and allow your server’s IP if it uses an access list.
-
Add a retry for connections made at startup, so a slow database doesn’t crash the app.
If the same database refuses PHP or a CLI client too, the problem is the database server; see MySQL 2002 Can’t connect. A browser showing ERR_CONNECTION_REFUSED is the same idea seen from the visitor’s side.
Prevention
- Keep every host, port and password in environment variables, never in code.
- Commit an
.env.examplelisting the required variables. - Log a clear message when a required variable is missing.
On ArabyHost
On ArabyHost AI/Developer plans, create the database in the panel (see create a MySQL database) and connect with host localhost and the credentials it shows; use 127.0.0.1 if your driver tries ::1. Add the values as environment variables in cPanel’s Setup Node.js App, which also passes the app’s port in process.env.PORT, so never hard-code 3000. If you’re unsure which setting is wrong, Araby AI in the panel can read the app’s error log and explain the fix.
FAQ
What's the difference between ECONNREFUSED, ETIMEDOUT and ENOTFOUND?
ECONNREFUSED: the machine answered but nothing listens on that port. ETIMEDOUT: no answer at all, often a firewall or wrong IP. ENOTFOUND: the hostname couldn't be resolved in DNS.
Why does the error show ::1 instead of 127.0.0.1?
`localhost` resolved to the IPv6 address `::1`, but the service listens only on IPv4. Use `127.0.0.1` as the host, or make the service listen on both.
It works on my computer but not on the server. Why?
The configuration still points to a service that exists only on your computer, such as a local database. Use the host, port and credentials of a database that exists on the server.