Database connection failed
Fix ‘Access denied for user’, ‘Unknown database’ and connection errors in PHP apps by checking prefixes, passwords, privileges and host.
On this page
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
| Error | Meaning |
|---|---|
1045 Access denied for user 'x'@'localhost' | Wrong username or password, or user not allowed on the database |
1044 Access denied ... to database | User exists but lacks privileges on that database |
1049 Unknown database | Database name wrong or doesn’t exist |
2002 No such file or directory / Connection refused | Wrong host setting |
1. Prefixed names
Database and user names include your account prefix, such as acme_shop. Compare exactly with Account Manager › 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 › 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
Prefer to follow along?
- Create a database and connect an application to it
Beginner · 15 min - Move a website and email to Traxio with no downtime
Advanced · 120 min
Common questions
What does “Error establishing a database connection” mean?
Your site’s configuration file has the wrong database name, username, password or host. Check all four against what DirectAdmin shows, remembering the account prefix on the database name, and use localhost as the host.
Spotted something out of date or unclear? Tell us and we’ll fix it.