Chapter 18 · 18.6
Error establishing a database connection
The message Error establishing a database connection means that your website can't reach the database, or isn't allowed to log in to it. Your files are still there, and usually your database too. In nine out of ten cases, something's wrong with the details your website uses to log in.
Causes, from common to rare
1. The details in the configuration file are incorrect
This usually happens after a move, a fresh installation, or a password change. Check it like this:
- Open Databases in DirectAdmin. There you'll see the full name of your database and the users linked to it (see Creating a database and Database users, privileges and passwords).
- Open your website's configuration file in File Manager. For WordPress, this is wp-config.php in public_html.
- Compare the four values:
- DB_NAME: the full database name, including your DA username and an underscore before it, such as wtsilefl_shop.
- DB_USER: the full name of the database user, also with that prefix.
- DB_PASSWORD: the password of that database user. This isn't your DA password.
- DB_HOST: localhost.
Not sure about the password? Give the database user a new password in DirectAdmin and enter exactly that password in wp-config.php. Watch out for quotation marks and spaces.
Other systems use a different file name, for example configuration.php for Joomla, app/config/parameters.php for PrestaShop, or .env for Laravel.
2. The user is no longer linked to the database
A database user needs permissions on the database. Check under Databases whether the user is listed with the correct database. If not, link it again (see Database users, privileges and passwords).
3. The database is empty or hasn't been imported
If you've moved your website, check in phpMyAdmin whether the tables are actually there (see Opening and using phpMyAdmin). If there are no tables, import the database again (see Importing a database, including large files).
4. Damaged tables
Do you only get the message occasionally, or does WordPress report that one or more tables aren't available? Check and repair the database (see Checking, repairing and optimising a database).
5. Too many connections
Do you see Too many connections or max_user_connections in the error log? Then your site is making too many simultaneous connections to the database, often caused by a busy or poorly functioning plugin, or by bots. Read My website is slow.
6. Storage is full
Full disk space can also cause the database to stumble. Check Disk Space on the dashboard (see Storage full: where's the space going?).
7. An outage
Very occasionally, it's the database server itself. Check Outages & maintenance to see if anything's going on.
Testing whether the details are correct
If you can see the tables in phpMyAdmin from DirectAdmin, the database exists and is in place. If you still get the message after that, it's almost certainly in your website's configuration file.
Tip: Did it work yesterday and you're not sure what's changed? With WebOké Backups in My WebOké, you can place a copy of the database from an earlier point in time alongside the current one, under a new name. This lets you compare without any risk (see Can I also do this in My WebOké? WebOké Backups).
