Skip to content
WordPress3 min read

How to fix "Error establishing a database connection" in WordPress

Error establishing a database connection

Short answer

"Error establishing a database connection" appears when WordPress fails to connect to its database. The most common cause is wrong credentials in wp-config.php after a migration or password change; others are a user without privileges on the database, corrupted tables, or a database server under heavy load.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Common causes
  2. How to fix it
  3. Prevention
  4. On ArabyHost

WordPress keeps posts, pages and settings in a MySQL database and connects to it on every request using four values in wp-config.php. If the database refuses the connection or doesn’t answer, WordPress stops and shows this message instead of the site. If /wp-admin instead shows One or more database tables are unavailable. The database may need to be repaired., the connection works but some tables are damaged.

Common causes

  • Wrong credentials in wp-config.php: database name, user, password or host: most often after a migration.
  • The user isn’t attached to the database or lacks privileges on it.
  • The database doesn’t exist: it was deleted, or never imported after a move.
  • Corrupted tables after a crash or a full disk.
  • The concurrent connection limit was reached because of a traffic spike or a heavy plugin. See error 1203.
  • The database server is down or briefly overloaded.

How to fix it

1. Open wp-config.php and check the values

In cPanel open Files » File Manager, go to public_html (or the site’s folder) and click Edit on wp-config.php. Find these lines:

define( 'DB_NAME', 'cpuser_wp123' );
define( 'DB_USER', 'cpuser_wpuser' );
define( 'DB_PASSWORD', 'your-password' );
define( 'DB_HOST', 'localhost' );

Note that cPanel prefixes database and user names with the account username, e.g. cpuser_.

2. Compare them with MySQL Databases

Open Databases » MySQL Databases in cPanel:

  1. Under Current Databases, confirm the database exists with exactly that name and that the user appears in the Privileged Users column.
  2. If not, use Add User To Database: pick the user and database, tick ALL PRIVILEGES and click Make Changes.
  3. If you’re unsure of the password, click Change Password next to the user under Current Users, set a new one and put it in DB_PASSWORD.

3. Test the connection with a temporary file

Create db-test.php in the site folder with the same values:

<?php
try {
    new mysqli('localhost', 'cpuser_wpuser', 'your-password', 'cpuser_wp123');
    echo 'Connected';
} catch (mysqli_sql_exception $e) {
    echo $e->getMessage();
}

Open it in a browser. Access denied means a wrong user or password (error 1045); Unknown database means a wrong database name. Delete the file afterwards.

4. Repair damaged tables

Add this line to wp-config.php above That's all, stop editing!:

define( 'WP_ALLOW_REPAIR', true );

Visit https://example.com/wp-admin/maint/repair.php, click Repair Database, then remove the line straight away, because that page works without logging in. You can also repair from phpMyAdmin: select the tables and choose Repair table.

5. If the credentials are right and the tables are fine

The problem is most likely load or a connection limit. Wait a few minutes, enable page caching to reduce queries, and check Metrics » Errors in cPanel for clues.

Prevention

  • Whenever you change the database user’s password, update wp-config.php at the same time.
  • Take a backup before migrations or major updates; see the WordPress backup guide.
  • Use page caching and drop plugins that run excessive queries.

On ArabyHost

MySQL Databases, File Manager and phpMyAdmin are all in cPanel, which opens with one click from the Araby Panel. Every account has its own resource limits (LVE), and LiteSpeed Cache cuts database queries because cached pages are served without touching the database. If the database is damaged, daily backups (JetBackup) let you restore it, and the Araby AI assistant reads the error logs and explains the cause. To start fresh, see the create a MySQL database guide.

FAQ

Will I lose my content because of this error?

Usually not. The error means WordPress can't reach the database; your posts and settings are still stored in it. Don't delete the database or reinstall WordPress before checking the connection details.

It appeared suddenly without me changing anything. Why?

If the credentials haven't changed, the cause is usually temporary load on the database server, hitting the concurrent connection limit, or corrupted tables. Try again in a few minutes, enable page caching to cut queries, and check the error log.

What should DB_HOST be?

On shared hosting with cPanel it's normally localhost, because the database runs on the same server. Only change it if your host gives you a different address.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.