PHP Database Connection Errors (mysqli and PDO): Access Denied, Unknown Database, Connection Refused
Read the exact message and match it to the cause: credentials, host, database name or server down.
Reference guide. This guide is written from established practice and the official documentation of the tools involved. We have not yet tested every step on a live demo site. Back up your site before you change anything.
Print or log the exact error. Each message points to a different cause.
Common messages
- Access denied for user: wrong user name or password, or the user is not allowed to use that database.
- Unknown database: the database name is wrong. Names often have your account prefix.
- Connection refused or cannot connect: wrong host, or the database server is down.
- Too many connections: your account hit its limit. See our limits guide.
Steps
- In your hosting panel, check the database name, user and that the user is assigned to the database with privileges.
- Use the host name your provider documents. It is often
localhost, but some hosts use a different name. - Reset the database user's password and update your code.
- Test with a tiny script using the same details.
- Do not show errors to visitors on a live site. See our PHP errors guide.
WordPress
See our WordPress database connection guide.
Security
Keep database credentials out of public files, and use prepared statements for queries.
Free tool for this problem: Database Checker
Related guides
- Writing Clear Error Reports: Screenshots, URLs and Steps
- WordPress Tag and Date Archives Duplicate Content: Settings
- WordPress Permalinks Change: Redirect Old URLs and Avoid 404s
- Error Establishing a Database Connection in WordPress: Full Checklist
Did this solve it? If not, email hello@sitefixlab.org with the exact error text and we will look into it. Try the problem finder.