Fix a database that will not connect
We'll check the service is running, the port is listening, credentials and firewall, and test with mysql or psql—or tell you when to escalate.
What you'll need
- Console or SSH access to the database server
- sudo on the server
- Client credentials (username, password)
Step-by-step diagnostic
Quick triage — pick your path
Get started
Choose the option that matches what you see. You can jump straight to that section.
Show full guide
Steps
Goal: Check the database service, port, credentials, and firewall.
- Run
systemctl status mysqlorsystemctl status postgresqlto confirm the service is running. - Good: Service is active. Proceed to Check service and port.
- Bad: Service inactive—start it or see fix-linux-server-will-not-start-service.
Check service and port
Goal: Confirm the database is running and listening on the correct port.
- Run
netstat -tlnp | grep 3306(MySQL) ornetstat -tlnp | grep 5432(PostgreSQL). Orss -tlnp. - MySQL uses 3306, PostgreSQL uses 5432.
- Good: Port is in LISTEN state. Proceed to Test locally.
- Bad: Port not listening—check bind-address (MySQL) or listen_addresses (PostgreSQL).
Test locally
Goal: Verify the database accepts connections from the server.
- MySQL: run
mysql -u root -p(or your user). - PostgreSQL: run
psql -U postgres -h localhost(or your user). - Good: You get a prompt. The issue is firewall or client config. Proceed to Check firewall.
- Bad: Connection refused or auth failed—check credentials, pg_hba.conf, or MySQL grants.
Check firewall
Goal: Confirm the database port is open for remote clients.
- Add rule:
ufw allow 3306/tcp(or 5432) andufw reload, orfirewall-cmd --add-port=3306/tcp --permanentandfirewall-cmd --reload. - From the client:
nc -zv host 3306(or 5432) to test reachability. - Good: Port is reachable. Check client host, port, and credentials.
- Bad: Firewall blocks—add the rule and reload.
When to escalate
Escalate if:
- The service will not start (see fix-linux-server-will-not-start-service).
- You cannot access the server console.
- The fix requires security policy or config management changes.
Provide systemctl status, netstat/ss output, and the exact connection error.
Verification
mysql -u root -porpsql -U postgres -h localhostconnects from the server.netstat -tlnporss -tlnpshows the database port in LISTEN state.- The client connects with the correct host, port, and credentials.
Escalation ladder
Work from the device outward. Stop when the problem is fixed.
- Service status systemctl status mysql or postgresql.
- Port listening netstat -tlnp or ss -tlnp | grep 3306 or 5432.
- Test locally mysql -u root -p or psql -U postgres -h localhost.
- Firewall and credentials ufw/firewall-cmd; check host, port, user, password.
- Escalate Provide systemctl status, netstat/ss, and connection error.
What to capture if you need help
Before calling support or posting for help, have these ready. It speeds everything up.
- systemctl status mysql or postgresql
- netstat -tlnp or ss -tlnp output
- Exact connection error from client
- Steps already tried
Is the database service running?
systemctl status mysql or postgresql.
You can change your answer later.
Start the service
Is the port listening?
netstat -tlnp | grep 3306 or 5432; ss -tlnp.
You can change your answer later.
Does mysql or psql connect locally?
mysql -u root -p or psql -U postgres -h localhost.
You can change your answer later.
Does the firewall allow the port?
ufw allow 3306/tcp or firewall-cmd --add-port=5432/tcp.
Check credentials and server config
MySQL grants; PostgreSQL pg_hba.conf.
Check bind/listen config
Reviewed by Blackbox Atlas
Frequently asked questions
- Why would a database not connect?
- The service is stopped, the port is blocked by a firewall, the host or port is wrong, credentials are invalid, or the server is unreachable. Check systemctl status, netstat/ss, and test with mysql or psql.
- Can I fix database connection issues myself?
- Yes. Start the service, open the firewall port, verify host and credentials. Test with mysql -u or psql -U locally first.
- When should I escalate database connection issues?
- If the service will not start, you cannot access the server console, or the fix requires config or security policy changes. Provide systemctl status, netstat/ss output, and the exact error.
Rate this guide
Was this helpful?
Thanks for your feedback.