Error Establishing Database Connection #
The “Error Establishing Database Connection” error is one of the most common issues on WordPress sites. This error indicates that the connection between your website files and the database has dropped. Since the database is queried for most of your WordPress site’s content, this drop stops any site content from displaying until the connection is re-established. So it’s important to address this error as soon as possible.
There are several reasons why the connection to the database may drop. The connection details may be incorrect, the database itself may not be loading correctly, or it may have been corrupted. This guide will break down the core areas to check to re-establish the connection and get your site loading.
Note: Take a backup of your website database before attempting any of the following solutions. You can do so using our Timeline Backups service.
1. Checking wp-config.php #
In a WordPress site, the database connection details are held within a file called wp-config.php, generally located in the root folder of your WordPress site. If you navigate to this file using our File Manager or via FTP, you’ll see code such as the following:
// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress / define( 'DB_NAME', 'database_name_here' ); /* Database username / define( 'DB_USER', 'username_here' ); /* Database password / define( 'DB_PASSWORD', 'password_here' ); /* Database hostname / define( 'DB_HOST', 'hostname_here' ); /* Database charset to use in creating database tables. / define( 'DB_CHARSET', 'utf8' ); /* The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
Each section is labelled via comments that show what each area is used for. The main details you’ll need to check are the Database name, Database hostname, Database username, and Database password. If any of these details are wrong, the connection to the database won’t be possible and you’ll need to change the details accordingly.
Finding Database Details #
Your site’s database details can be accessed and updated via your hosting package:
- Log into StackCP and head to your Manage Hosting area.
- Select Options > Manage on the hosting package you wish to edit.
- Head to the Web Tools section and select MySQL Databases.
Under Manage MySQL Databases, you’ll see your databases and their current Server, Database/Username, and an area where you can update the password. Use these details to fill out and update your connection details in the wp-config.php file.
- Server is your Database hostname.
- Database/Username is the Database name and Database username.
- Database password is the password you’ve added to the Change Password area.
Once these have been updated, recheck your site, and the database connection error should no longer be displaying.
2. Checking the Database Connection #
You can also test your database connection using the above details. This can help determine if the problem is with the details being used or if there’s something wrong with the database itself.
- Head to your site’s File Manager or use FTP to create a new file in the root folder of your site. Name this file something like
test.php. - Add the following code to the file:
$host = 'hostname';
$user = 'username';
$pass = 'pass';
$test = mysqli_connect($host, $user, $pass);
if (!$test) {
die('MySQL Error: ' . mysqli_error($test));
}
echo 'Database connection is working properly!';
mysqli_close($test);
Replace hostname, username, and pass with the database hostname, database username, and database password, respectively.
Load the script via your site by visiting the URL, for example, domain.com/test.php.
If the script loads and displays an error, there’s a problem with the details being used. If it displays “Database connection is working correctly!”, the credentials being used for the database are correct. If the site is still showing a database error, the problem may be with the database itself, and it will need to be repaired.
3. Repairing a Corrupt Database #
The issue can sometimes be that your database itself has become corrupt. This isn’t a common occurrence but can happen due to the amount of data, rows, and tables created and removed by various third-party plugins and functions.
Accessing wp-admin #
One way to test if the database is corrupt is to try and connect via wp-admin. If you receive a corrupted database error message, it means that the database has been corrupted. Fortunately, there are tools that can be used to repair this, using both WordPress and phpMyAdmin.
4. Using WordPress’ Database Repair Tool #
To access the repair tool provided by WordPress, you’ll need to edit your wp-config.php file and add the following line of code before the line /* That’s all, stop editing! Happy publishing. */:
Replace hostname, username, and pass with the database hostname, database username, and database password, respectively.
Load the script via your site by visiting the URL, for example, domain.com/test.php.
If the script loads and displays an error, there’s a problem with the details being used. If it displays “Database connection is working correctly!”, the credentials being used for the database are correct. If the site is still showing a database error, the problem may be with the database itself, and it will need to be repaired.
Repairing a Corrupt Database #
The issue can sometimes be that your database itself has become corrupt. This isn’t a common occurrence but can happen due to the amount of data, rows, and tables created and removed by various third-party plugins and functions.
Accessing wp-admin #
One way to test if the database is corrupt is to try and connect via wp-admin. If you receive a corrupted database error message, it means that the database has been corrupted. Fortunately, there are tools that can be used to repair this, using both WordPress and phpMyAdmin.
5. Using WordPress’ Database Repair Tool #
To access the repair tool provided by WordPress, you’ll need to edit your wp-config.php file and add the following line of code before the line /* That’s all, stop editing! Happy publishing. */:
define('WP_ALLOW_REPAIR', true);
Once this code has been added, head to the following URL:
https://domain.com/wp-admin/maint/repair.php
Replace ‘domain.com’ with your domain. This will open a repair wizard built into WordPress that will attempt to repair your database. Follow the steps in the wizard, and when it’s finished repairing, your site should come back online. Once you’ve finished with the repair tool, remove the above line of code from your wp-config.php file.
WordPress CLI #
WordPress CLI also provides a quick tool to repair the database. To access this, connect to your site via SSH and navigate to the root folder of your WordPress installation. Run the command:
wp db repair
- This will start a full repair of the WordPress database.
- Repairing a Database with phpMyAdmin
- You can also repair the database using phpMyAdmin.
- Log into StackCP and head to your Manage Hosting area.
- Select Options > Manage on the hosting package you wish to edit.
- Head to the Web Tools section and select phpMyAdmin.
- Select Sign-In on the database you need to repair.
- You’ll be signed into phpMyAdmin. Select your database on the left, and you’ll see a selection of your database’s current tables.
- Select all of your tables with the checkbox next to each.
- From the drop-down at the bottom of the screen, select Repair table.
- This will run the SQL REPAIR TABLE command on all of your selected tables, which will attempt to repair and remove any corruption or broken data from these tables.
6. Repairing Corrupted WordPress Files #
In some rare instances, the issue may lie with the files of the site rather than the database itself. This can happen when the connection during an FTP upload is interrupted, or malware infects the WordPress site and modifies the core files. If this is the case, you’ll need to replace the core files of the site with a fresh copy directly from WordPress.
Replacing Core Files #
- Download the latest version of WordPress from WordPress.org.
- Unzip the files on your local device and remove the wp-content and wp-config.php files.
- Connect to your site via FTP and upload the folders and files to your site’s root folder, overwriting your current core files with the fresh upload.
- Note: Provided that you remove the wp-content files, doing this won’t affect the content of your website – text, images, etc.
Alternatively, you can use the following WP CLI command after connecting via SSH to achieve this as well:
wp core download --force
By following these steps, you should be able to pinpoint and fix the “Error Establishing a Database Connection” error, bringing your site back online as soon as possible. Always be sure to take a backup before attempting any form of manual change to your site files and database.