How To Fix WordPress Error Establishing a Database Connection
When WordPress cannot connect to its database, it results in a major error that prevents users from accessing your site. This issue, which can stem from several different causes, may be tricky for beginners to diagnose.
To help you resolve this, we have created a detailed, step-by-step guide to fix the 'Error establishing a database connection' on your WordPress site.
Cause
Below are the common reasons why WordPress Error Establishing a Database Connection occur:
- Incorrect database login details in wp-config.php file
- Database files are corrupted
- Database server is down
Resolution
IMPORTANT. Before doing any of the suggestion resolutions below, we recommend you to backup your files first. Learn how to back up your files in this guide: How to Create Full and Partial Backups of Your Website Files.
1. Check the database login details in the configuration file
The WordPress database settings is kept in the wp-config.php file. It holds important details like the database name, username, and password. Often, if you face a database connection issue right after moving your WordPress site to a new server, it's because of incorrect information in this file. So, it's crucial to double-check and make sure these details are correct.
Here's how you can access your wp-config.php file:
For cPanel (Linux Hosting and Business Hosting)
- Go to your cPanel File Manager.
- Open the root folder of your website. See the directory guide below:
Type of Website Directory Primary Domain Site /public_html Addon Site /public_html/(addondomainfolder) - Locate the wp-config.php file.
- Right click on the file and click View.
For Plesk (Windows Hosting)
- Go to your Plesk File Manager.
- Open the root folder of your website.
- Locate the wp-config.php file.
- Click the file to open.
Once you've accessed your wp-config.php file, locate the lines where database details are listed, like the example below:
The database information in your wp-config.php file should match your actual database information. You can confirm this by accessing your database in your cPanel (for Linux Hosting and Business Hosting) or Plesk panel (for Windows Hosting).
Here's how you can access your existing database:
In cPanel
- Access your cPanel and go to Databases>MySQL Databases>Current Databases.
- Locate the correct database for your WordPress site. Here, you can see your database name and username.
In Plesk
- Access your Plesk panel and go to Databases.
- Locate the correct database for your WordPress site and then click Connection Info.
In most cases the database name and username will change when you migrate a database to our server because of our naming structure. Make sure your wp-config.php database configuration corresponds to the new database name and username.
2. Check your database host in the configuration file
Aside from the database name and username, you also need to check the database host information.
Your previous hosting provider might have a different database host. This information will be carried over when you migrate your website to our server. So, it's important to check and correct the database host information in your configuration file to match your existing database host with Crazy Domains. Below are our database hosts:
Platform | DB_HOST |
---|---|
cPanel (Linux and Business Hosting) | 'localhost' |
Plesk (Windows Hosting) |
Your database host depends on what server your database is hosted. Follow the instructions below to know your database host:
|
3. Repair WordPress database
If you encounter a different error in the wp-admin dashboard, such as ‘One or more database tables are unavailable’ or ‘The database may need to be repaired,’ you will need to repair your database.
To do this, open your wp-config.php file and add the following code before the line that says /* That's all, stop editing! Happy publishing. */ :
define('WP_ALLOW_REPAIR', true);
After adding the code. Open the following link in your browser: http://www.yourdomain.com/wp-admin/maint/repair.php.
IMPORTANT. Make sure to replace yourdomain.com with your actual domain name.
Once the page is launched, click the Repair Database button.
IMPORTANT. This page will be accessible to anyone on the net. Once you are done repairing the database, make sure to remove the code you've added to your wp-config.php file.