4 Ways to Fix Error Establishing a Database Connection in WordPress
Error establishing a database connection is one of the common errors that WordPress users at least seen it few times. If your website encounter this error than your WordPress website is unable to connect to your database.
WordPress execute with PHP programming language and SQL database to run your blog smoothly. This error can cause by many reasons and in this tutorial we have put all the ways to fix and bring your website back to the operation.
Time needed: 45 minutes.
Error Establishing a Database Connection
- Corrupted Database:
WordPress database can be corrupted by installing faulty plugins in your site. If you have update or install poorly coded plugins or themes that corrupts the database you need to revert it back to default or remove entirely from your website.
- Repair Database
If you are also seeing error in your login page wp-admin then you need to repair your database adding one line of code to the wp-config.php file in your web server.
- Database Login Credentials are Incorrect
If your login credential, username or password is incorrect it couldn’t fetch anything from the database. It could happen even if you didn’t change it yourself.
- The Database Server is Down
Because of high volume of traffic your database could be down. If you are using Google Analytics, check your web traffic, if you see majority of visitor comparing to other day you probably have to speak with your hosting provider to upgrade.
Step-1: Check Corrupted Database
If you see this error after recent update or installation of the plugins or themes, than you have to deactivate all the plugins and the themes to ensure the faulty plugin or themes from you website.
How to deactivate plugins?
You need FTP client such as FileZilla to access the files and folder in your web server. Once you connect FileZilla to your web server, Double click on public_html then wp-content where you will see plugins folder. You simply need to rename the plugins folder name to any name. For more details guide you can check how to deactivate WordPress plugins.
Reload your website back again to see this error, If error still exist deactivate the themes as same process above. Double Click on public_html then wp-content. Inside wp-content you will themes folder double click themes folder to see installed themes. Right click on the current themes and rename it.
If this step didn’t fix the issue you can jump to the next step to fix the error.
Step-2: Repair Database
Check if you can access WordPress admin login (wp-admin) page. If you are seeing the same error (error establishment database connection) in your WordPress login page e.g. yourwebsite.com/wp-admin then skip this step.
If your login page is showing different error as below then you need to repair your database adding a line of code to the wp-config.php file in the server.
Connect FTP client FileZilla to the web server. From the right side, Go to the root directory of your blog e.g. public_html folder where you will find wp-config.php file. Right-Click on the file then click on View/Edit to add the code.
Scroll down until you find That’s All, Stop editing! Happy publishing, paste the code before the line and Save it.
Once you added the line, open your web browser such as Chrome and paste the below URL, make sure to change the yourblog to your domain name and hit enter.
You will see the options to repair the database. Click on Repair Database button, WordPress will start repairing your database.
Upon completion go back to your site and see if the error still exists. If not, this step has successfully fixed the error. You have to go back to edit the wp-config.php file again to removing the line of code you have added. If still the error occurs, go to the next step.
Step-3: Database Login Credentials are Incorrect:
Check wp-config.php file to ensure correct database credentials. WordPress connects to the database using username and password. Sometimes your login credentials get change which breaks the database connection to your site.
When you access wp-config.php file you will see below lines that holds your Database Name, User and password. If any of these are wrong than you will see error establishing database connection.
define('DB_NAME', 'site_db'); define('DB_USER', 'site_username'); define('DB_PASSWORD', 'site_pass'); define('DB_HOST', 'localhost');
In-order to get your database credential you need to contact your hosting provider once you note down your database credential you can enter the correct details in those lines.
Again, connect FTP client, FileZilla and edit wp-config.php files. Enter the correct Database name, user and password provided by your hosting provider. Database host will be ‘localhost’ which will remain same and save the changes.
Step-4: The Database Server is Down:
Often when the blog flooded with traffic your server couldn’t handle it resulting to crash your database. If you are not sure that your blog received massive traffic you can check your Google Analytics report to compare the web traffic. If so, contact your hosting provider to fix the issue.
To keep your blog secure and hassle free you probable want to choose the best hosting companies that provide 99.9% server up-time and 24/7 customer service. Here is my recommendations for best WordPress hosting companies that can take your small business blog to the next level.
Best web host will secure your blog from the hacks and optimize the blog performance which helps your blog to climb the top of search engine. One important thing to install UpdraftPlus plugin to backup your blog easily incase of any incident.