Database connection failed

Fix ‘Access denied for user’, ‘Unknown database’ and connection errors in PHP apps by checking prefixes, passwords, privileges and host.

TroubleshootingIntermediate2 min readUpdated

Does this match what you’re seeing?

  • SQLSTATE[HY000] [1045] Access denied for user
  • Unknown database
  • The application says it can’t connect to the database

WordPress? Go to “Error establishing a database connection” in WordPress.

Read the exact error

ErrorMeaning
1045 Access denied for user 'x'@'localhost'Wrong username or password, or user not allowed on the database
1044 Access denied ... to databaseUser exists but lacks privileges on that database
1049 Unknown databaseDatabase name wrong or doesn’t exist
2002 No such file or directory / Connection refusedWrong host setting

1. Prefixed names

Database and user names include your account prefix, such as acme_shop. Compare exactly with Account Manager then MySQL Management. How to create a MySQL database in DirectAdmin

2. Password

Reset the user’s password in DirectAdmin and paste the same value into the application’s config. How to change a database user’s password

3. Privileges

Open the database in Account Manager then MySQL Management and check the user is listed. Add it with all privileges if not. Managing database users and privileges

4. Host

Use localhost. If localhost gives error 2002 in PDO, try 127.0.0.1.

5. Config file actually in use

Frameworks cache configuration. Clear cached config (for Laravel, delete bootstrap/cache/config.php), and check you edited the production .env, not a local copy.

6. Moved from another host?

The old host’s database details won’t work here. Create a new database and user, import the data, update the config. How to import a database with phpMyAdmin

Popular

Tip: press / to search from any pageSee all results