Error Establishing a Database Connection

Error Establishing a Database Connection

Posted by

The “Error Establishing a Database Connection” is a critical WordPress error that means WordPress cannot connect to your database.

Here’s a step-by-step guide to fix it:


Error Establishing a Database Connection
Error Establishing a Database Connection

1. Check wp-config.php File

Make sure your database details are correct:

  • Open your website files via FTP or File Manager.
  • Locate the file: /public_html/wp-config.php
  • Check these lines:
define( 'DB_NAME', 'your_db_name' );
define( 'DB_USER', 'your_db_user' );
define( 'DB_PASSWORD', 'your_db_password' );
define( 'DB_HOST', 'localhost' ); // Usually "localhost"

Ensure values are correct. Ask your hosting provider if you’re not sure.


2. Test Database Access

Use phpMyAdmin or a simple script to test DB login:

<?php
$connection = mysqli_connect('localhost','your_db_user','your_db_password','your_db_name');
if ($connection) {
    echo 'Connected successfully!';
} else {
    die('Database connection failed: ' . mysqli_connect_error());
}
?>

If this fails → the issue is with credentials or the DB server.


3. Check with Hosting Provider

Ask them:

  • Is Mysql server up and running?
  • Is the DB username allowed access to that DB?

Sometimes the database server is temporarily down.


4. Repair WordPress Database

Add this line to wp-config.php:

define('WP_ALLOW_REPAIR', true);

Then go to:

https://yourdomain.com/wp-admin/maint/repair.php

Click: “Repair and Optimise Database”

After that, remove the line from wp-config.php.


5. Check for Corrupt Files

Re-upload a fresh copy of:

  • /wp-includes/
  • /wp-admin/

Don’t delete wp-content/ or wp-config.php.


6. Check Website Traffic (Too High)

If your site is under heavy traffic, your DB may crash.

  • Check with your host
  • Upgrade the hosting plan if needed

7. Check DB User Privileges

In phpMyAdmin, make sure:

  • The user has ALL PRIVILEGES on the database.

8. Restart Mysql Server

(For VPS or Dedicated Server)

Use SSH and type:

sudo service mysql restart

Error Establishing a Database Connection If You Still See the Error:

  • Double-check all credentials
  • Check the hosting provider’s status page
  • Check .htaccess file for corruption
  • Look into error logs via cPanel or FTP

How to add domain to Cloudflare