How to Fix "Error Establishing a Database Connection" in WordPress

"Error establishing a database connection" means WordPress cannot talk to its MySQL database. The files are fine, but every page needs data from the database (posts, settings, users), so the whole site shows only this one line.
In my experience fixing sites, the cause is almost always in wp-config.php: either the file has become completely empty, or the database details inside it are wrong. Less often, the database itself is damaged, missing, or the database server is down. This guide goes through each cause in the order I check them.
Quick checklist: fix the database connection error
- Open
wp-config.phpin File Manager. Is it empty (0 bytes)? Rebuild it (Step 2). - Check DB_NAME, DB_USER, DB_PASSWORD and DB_HOST against cPanel (Step 3).
- Make sure the database user is added to the database with all privileges (Step 4).
- If wp-admin says tables need repair, use WP_ALLOW_REPAIR (Step 5).
- If the database is missing, restore it from a backup or ask your host (Step 6).
- If everything is correct, the MySQL server may be down or overloaded: check with your host (Step 7).
What causes "Error establishing a database connection"?
| Cause | How often I see it | Sign |
|---|---|---|
wp-config.php became empty | Common | File size 0 bytes, or only a few lines |
Wrong database name, user or password in wp-config.php | Very common | After a migration, a password change or a hosting move |
Wrong DB_HOST | Sometimes | On hosts where MySQL is not on localhost |
| Database user not linked to the database | Sometimes | After creating a new database by hand |
| Corrupted database tables | Occasional | wp-admin shows a different message about repairing |
| Database deleted or missing | Rare, serious | The database does not appear in cPanel at all |
| MySQL server down or overloaded | Occasional | Several sites on the same hosting fail at once |
Step 1: Take a backup before you change anything
Even with the site down, download a copy of wp-config.php (whatever state it is in) and, if phpMyAdmin opens, export the database. If something goes wrong while fixing, you can always get back to where you started.
Step 2: Is wp-config.php empty? Rebuild it
This is the case I have run into most. You open public_html/wp-config.php in File Manager and it shows 0 bytes, or it opens blank. Without it, WordPress has no database details at all.
Why does wp-config.php become empty?
- The hosting account ran out of disk space while a plugin was writing to the file (caching and security plugins often add lines to
wp-config.php). The write started, nothing could be saved, and the file was left empty. - A failed edit in File Manager or by FTP, where the upload stopped halfway.
- Malware that rewrote or wiped the file.
So after fixing it, check your disk usage in cPanel and free up space if it is full, or the same thing can happen again.
How to rebuild wp-config.php
Your WordPress folder has a file called wp-config-sample.php. It is a clean template.
- Copy
wp-config-sample.phpand name the copywp-config.php(delete or rename the empty one first). - Open it and fill in the database details:
define( 'DB_NAME', 'youracct_wp123' );
define( 'DB_USER', 'youracct_wp123' );
define( 'DB_PASSWORD', 'the-database-password' );
define( 'DB_HOST', 'localhost' );
- Set the table prefix to match your tables (see below).
- Replace the authentication keys and salts (see below).
- Save, and reload the site.
Where to find the database name and user
In cPanel, open MySQL Databases. Under Current Databases you see each database and which users are linked to it. Database names usually start with your cPanel username, like youracct_wp123.
If the account has several WordPress sites, you need to know which database belongs to this one. Open phpMyAdmin, click a database, open the wp_options table (or xyz_options, with its own prefix), and look at the siteurl row. It shows which site that database belongs to.
What if you don't know the database password?
The password cannot be read back from cPanel. But you do not need the old one:
- In MySQL Databases, scroll to Current Users.
- Click Change Password next to the database user.
- Set a new strong password and save.
- Put the same password in
DB_PASSWORDinwp-config.php.
Changing the database user's password only affects the connection between WordPress and MySQL. It does not change any WordPress login.
Set the right table prefix
The template uses $table_prefix = 'wp_';. Look at your table names in phpMyAdmin. If they start with something else, like wpxy_posts or abc_options, use that:
$table_prefix = 'wpxy_';
If the prefix is wrong, WordPress will not find your tables and will show the installation screen instead of your site. Do not run the installer; just fix the prefix.
Generate new authentication keys and salts
The template has eight lines like define( 'AUTH_KEY', 'put your unique phrase here' );. Replace them with real random values. WordPress gives you a fresh set at:
https://api.wordpress.org/secret-key/1.1/salt/
Open that link, copy all eight lines, and paste them over the placeholder lines in wp-config.php. Every time you open it you get new random keys. Using new keys simply logs everyone out once, which is harmless and even good after a problem like this.
If the old wp-config.php had extra lines (for example memory limits or a caching constant), add them back after the site works. A diff checker helps compare an old copy from a backup with the new file.
Step 3: Check the database details in wp-config.php
If wp-config.php is not empty, the details inside it may simply be wrong. This happens most often after a migration, a hosting move, a database password reset, or when a site was restored from a backup made on another account.
Open wp-config.php and compare each value with cPanel:
- DB_NAME: must exactly match a database in MySQL Databases, including the
cpaneluser_prefix. - DB_USER: must exactly match a user in Current Users.
- DB_PASSWORD: if unsure, reset it (as above) and update the file.
- DB_HOST: usually
localhost. Some hosts use a separate database server with its own address; it is shown in the hosting's database section or help pages.
Look out for small mistakes: an extra space inside the quotes, a missing quote, or a smart quote (โ) pasted from a document instead of a straight quote (').
Step 4: Make sure the user has access to the database
A database and a user can both exist and still not be connected. This happens when you create them by hand.
- In MySQL Databases, find Add User To Database.
- Choose the user and the database, and click Add.
- On the privileges screen, tick ALL PRIVILEGES and click Make Changes.
In Current Databases, the user should now appear next to the database.
Step 5: Repair corrupted database tables
If you can open /wp-admin and it shows a message like "One or more database tables are unavailable. The database may need to be repaired", the connection works but some tables are damaged.
- Add this line to
wp-config.php, above/* That's all, stop editing! */:
define( 'WP_ALLOW_REPAIR', true );
- Open
https://yoursite.com/wp-admin/maint/repair.php. - Click Repair Database (or Repair and Optimize Database).
- When it finishes, remove the line from
wp-config.php.
You can also repair tables in phpMyAdmin: select the database, tick all tables, and choose Repair table from the dropdown.
Step 6: Is the database missing? Restore it
If the database does not appear in MySQL Databases or phpMyAdmin at all, it has been deleted. This can happen when someone uninstalls a site from Softaculous that shared the database, deletes the "wrong" database while cleaning up, or when a hosting account was suspended and cleaned.
Your options, in order:
- Your own backup. A Softaculous, UpdraftPlus or manual
.sqlbackup. Create a new database and user, import the backup in phpMyAdmin, and put the new details inwp-config.php. My UpdraftPlus guide shows how to restore from Google Drive. - The hosting's backups. Many cPanel hosts keep automatic backups with JetBackup or a similar tool. In cPanel, look for JetBackup > Database Backups and restore the database from a date before the problem.
- Ask the hosting provider. If you find no backup tool, open a ticket and ask whether they have a server-level backup of the database. Many hosts keep some days of backups even when you cannot see them, but the sooner you ask, the better: old backups get overwritten.
If no backup exists anywhere, the content cannot be recovered from the files alone, because posts and pages live only in the database. That is the strongest reason to keep your own off-site backups.
Step 7: Is the database server down or overloaded?
If all the details are correct and the user has privileges, the MySQL server itself may be the problem:
- Several sites on the same hosting show the error at the same time;
- the error comes and goes, especially during traffic peaks;
- phpMyAdmin is slow or will not open.
On shared hosting this is often a resource limit (too many connections, CPU or memory) or a MySQL restart on the server. Contact your host and ask whether MySQL is running and whether your account hit its limits. A caching plugin and blocking bad bots reduce database load if it keeps happening.
Test the connection yourself
To be sure whether the details or the server are at fault, create a file called dbtest.php in the site's folder:
<?php
$link = mysqli_connect( 'localhost', 'DB_USER', 'DB_PASSWORD', 'DB_NAME' );
if ( ! $link ) {
die( 'Connection failed: ' . mysqli_connect_error() );
}
echo 'Connected successfully';
Fill in the same values as wp-config.php and open https://yoursite.com/dbtest.php.
- "Connected successfully": the details work, so look at WordPress itself (tables, prefix, plugins).
- "Access denied": the user, password or privileges are wrong (Steps 3 and 4).
- "Unknown database": the database name is wrong or the database is missing (Step 6).
- Cannot connect to server: wrong DB_HOST or the MySQL server is down (Step 7).
Delete dbtest.php right after testing, because it contains your database password.
How to prevent the database connection error
- Keep automatic off-site backups of the database (UpdraftPlus to Google Drive, or your host's backups).
- Watch your disk usage, so files like
wp-config.phpare never cut off by a full disk. - Keep a copy of
wp-config.phpin a safe place after every change. - When you change a database password or move hosting, update
wp-config.phpstraight away. - Before deleting a database or uninstalling a site, check in phpMyAdmin (the siteurl row) which site it really belongs to.
If the site shows a different message, such as "There has been a critical error on this website", see my critical error guide instead.
Frequently asked questions
Can a plugin cause "Error establishing a database connection"?
Rarely directly. But a plugin that writes to wp-config.php can leave it broken or empty if something fails, and a heavy plugin can overload the database. The fix is still in wp-config.php or with the database server.
Will I lose my content because of this error?
Usually not. In most cases the database is intact and only the connection details are wrong. Content is lost only if the database itself was deleted and there is no backup.
Why does the error show only sometimes?
That points to the database server being overloaded or hitting connection limits, not to wrong details. Check with your host, especially during traffic peaks.
Do I need to reinstall WordPress?
No. Reinstalling WordPress does not help, and the installer can even create a new empty database setup. Fix wp-config.php, the user or the database instead.
Where do I get the AUTH_KEY and salt values?
From the official generator at https://api.wordpress.org/secret-key/1.1/salt/. Copy all eight lines into wp-config.php in place of the placeholder lines.