All posts

How to Fix the Error Establishing a Database Connection in WordPress?

Aug 18, 2024 3 min read Cyberison Team Updated Dec 4, 2026
In this guide
  1. Causes of the Error
  2. 1. Verify Your Database Credentials
  3. 2. Check the Database Host
  4. 3. Repair Your Database
  5. 4. Check the Database Server
  6. 5. Contact Your Hosting Provider

If your WordPress site is showing the “Error establishing a database connection” message, it means WordPress cannot connect to your database. This error can make your site inaccessible to users. Here’s how to troubleshoot and fix this issue.

Error db connection

Causes of the Error

This error usually occurs due to:

  • Incorrect database credentials
  • A corrupt database
  • An unresponsive database server

WordPress requires the following details to connect to your database:

  • Database name
  • Database username
  • Database password
  • Database host

1. Verify Your Database Credentials

Incorrect credentials are a common cause of this error. Check your wp-config.php file for the following lines:

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

Error db connection db file

Ensure these values match the details provided by your hosting provider. You can verify this information in your hosting account dashboard under MySQL databases.

2. Check the Database Host

Confirm that you’re using the correct database host. Most hosting providers use localhost, but some may use a different server. If you’re unsure, check with your hosting provider.

Localhost

3. Repair Your Database

If you encounter messages like “One or more database tables are unavailable,” your database may need repair. Add the following line to your wp-config.php file just before ‘That’s all, stop editing! Happy blogging’ line.

define('WP_ALLOW_REPAIR', true);

Visit http://www.yoursite.com/wp-admin/maint/repair.php to access the repair page. Once repairs are complete, remove this line from wp-config.php.

4. Check the Database Server

If the issue persists, your MySQL server may be down. Contact your hosting provider to check if the server is responsive. Alternatively, you can create a test connection file:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysqli_connect_error());
}
echo 'Connected successfully';
mysqli_close($link);

Save this code as wptestdb.php, upload it to your site’s root directory, and open it in your browser. If the test connects successfully, then your database credentials are correct.

5. Contact Your Hosting Provider

If none of the above solutions resolve the issue, contact your hosting provider for assistance. Provide them with details about the steps you’ve taken to help them diagnose and fix the problem.

By following these steps, you should be able to resolve the “Error establishing a database connection” issue and restore access to your WordPress site.

Share this guide