How to Fix Common Database Connection & Permission Errors

A reference guide to the database connection and permission errors developers hit most often, across MySQL/MariaDB, PostgreSQL, and MongoDB, with direct fixes.

MySQL/MariaDB: "Access denied for user"

ERROR 1045 (28000): Access denied for user 'appuser'@'localhost'

Verify the password is correct, and confirm the user actually has privileges on the target database:

SHOW GRANTS FOR 'appuser'@'localhost';

If missing, grant them:

GRANT ALL PRIVILEGES ON myapp.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;

MySQL/MariaDB: "Can't connect to MySQL server"

Verify the service is running:

sudo systemctl status mysql

If connecting remotely, confirm bind-address in the config isn't restricted to 127.0.0.1 only, and that the firewall allows the connection.

MySQL/MariaDB: "Too many connections"

SHOW VARIABLES LIKE 'max_connections';

Either the application isn't closing connections properly (fix at the app layer first), or max_connections genuinely needs raising — see How to Tune MySQL/MariaDB Performance for a VPS.

PostgreSQL: "password authentication failed for user"

Verify the password, and check the authentication method configured in:

sudo nano /etc/postgresql/*/main/pg_hba.conf

The method (e.g. scram-sha-256, md5) must match what the client supports.

PostgreSQL: "FATAL: no pg_hba.conf entry for host"

The connecting IP isn't authorized. Add an entry:

host    myapp    appuser    YOUR_IP/32    scram-sha-256
sudo systemctl restart postgresql

PostgreSQL: "database does not exist"

sudo -u postgres psql -l

Confirms the exact database name and whether it was actually created.

MongoDB: "Authentication failed"

Confirm you're specifying the correct authentication database:

mongosh -u admin -p --authenticationDatabase admin

MongoDB: "connect ECONNREFUSED 127.0.0.1:27017"

sudo systemctl status mongod

If MongoDB is running but the app still can't connect, verify it's not bound only to a different interface than expected.

General: Connection Works via CLI But Not from the Application

This usually means the app is using different credentials, host, or port than tested manually. Double-check the exact connection string/environment variables the application is actually using — not what you assume they are.

General: Firewall Blocking a Remote Connection

sudo ufw status

Confirm the specific port and source IP are explicitly allowed.

Diagnostic Checklist

  1. Is the database service actually running? systemctl status
  2. Can you connect locally via the CLI client with the same credentials the app uses?
  3. Does the user have the correct privileges on the specific database?
  4. Is the firewall allowing the connection, if remote?
  5. Does the authentication method/config file allow this specific host/user combination?

Related Articles

  • How to Install and Secure MySQL 8 on Ubuntu & Debian
  • How to Install PostgreSQL on Ubuntu & Debian
  • Database Security Checklist: Protecting MySQL, PostgreSQL & MongoDB
  • database errors, mysql access denied, postgresql connection error, mongodb authentication
  • 0 Los Usuarios han Encontrado Esto Útil
¿Fue útil la respuesta?

Artículos Relacionados

How to Install and Secure MySQL 8 on Ubuntu & Debian

MySQL is one of the world's most widely used relational database systems, powering WordPress,...

How to Install MariaDB on Ubuntu & Debian

MariaDB is a community-developed, fully open-source fork of MySQL, offering strong compatibility...

How to Install PostgreSQL on Ubuntu & Debian

PostgreSQL is an advanced, standards-compliant open-source relational database known for...

How to Install MongoDB on Ubuntu & Debian

MongoDB is a NoSQL, document-oriented database designed for flexibility and horizontal...

How to Install and Secure Redis on Ubuntu & Debian

Redis is a fast, in-memory data store used as a cache, session store, message broker, and queue...