How to fix “error establishing a database connection” in wordpress


As a WordPress site developer or webmaster, a terrifying situation is when your site is down. You not only thua thảm incoming traffic but your site’s ranking and reputation are also negatively affected.

Bạn đang xem: How to fix “error establishing a database connection” in wordpress

One of the errors that cause your site khổng lồ temporarily be inaccessible is the database connection error. You might have run inlớn this error when you find yourself entering the URL of your site & nothing appears other than this error message: “Error establishing a database connection.”

If you’ve ever encountered this error or are in the midst of finding a solution for it then don’t fret! In this tutorial, we will show you how to fix this error very easily – the right way. By following the steps carefully, you’ll be able to lớn retrieve sầu your site and get back all of your traffic and subscribers in no time.

Sound good? Let’s get started!

What Is the Error Establishing a Database Connection Error?

Before we jump into lớn the tutorial let’s take a brief moment khổng lồ understvà what this error actually means. The error establishing a database connection error basically means that for some reason or another the PHP code was unable khổng lồ connect lớn your MySQL database to retrieve the information it needs to lớn fully build that page. That’s why the error is always shown on a blank page because there is no information about your site as it is not connected to lớn your database.

Why Does the Error Establishing a Database Connection Error Occur?

Now that we have sầu understood the meaning of the error, we need to find out what is causing the error when we try lớn open our WordPress site. It could be because of many reasons because errors in database connectivity may have several different underlying causes.

01. Incorrect Login Credentials

For instance, it can be because of the wrong login username and password being used lớn access the admin panel. If the login credentials have sầu changed recently, you will not be able khổng lồ access your database with the old ones. So your first order of business should be lớn confirm that the login credentials you’re entering are accurate.

02. Corrupt Database

Another possibility is that the database might be corrupted. The WordPress database can be corrupted by a number of things like installing a faulty or incompatible plugin. Another likely reason might be that the hệ thống that hosts your database may temporarily be down.

These are some of the possible reasons that may have sầu caused the error. Now that you know what the error is & how it was probably caused, let’s try fixing that error và getting your site baông chồng up online.

How to Fix Error Establishing a DB Connection Error

Before fixing the error, we need lớn determine exactly what is causing the error.

Method 1: Repair the Database

First we need to lớn kiểm tra if our database is okay. If you can see your site normally and get an error when trying lớn access the wp-admin page or get a different error saying “One or more database tables are unavailable. The database may need to be repaired.” when you try khổng lồ access your site, these are clear signs that your database is corrupt. Thankfully, your database can easily be repaired using a built-in WordPress feature.

By mặc định the feature is disabled, so you need to enable it yourself in order khổng lồ repair your database by going into lớn the wp-admin.php file & adding this code:

define( "WP_ALLOW_REPAIR", true );This simple code of line allows you to repair và optimize your database. After adding this line, simply navigate to A page with two similar options will appear – the first option would be the Repair option which will repair your database và the second would be the Repair & Optimize option which will repair and optimize your database for future errors và keep it stable.

The Repair and Optimize error takes more time than the simple Repair option so if you’re short on time then simply choose the first option.

Another important thing lớn keep in mind is that the repair page you just opened after putting the line of code into lớn the wp-admin.php file is not secure; as anyone can access that URL without having khổng lồ login. So make sure you remove that little line of code from the wp-admin.php tệp tin you put earlier after you have sầu successfully repaired your site. Doing this guarantees that no one will mess with your database options in the future.

Method 2: Change Login Credentials

Still no luông chồng on opening your website successfully even after repairing the database? You might want lớn take a look at the database login settings in the wp-config tệp tin. Your database login credentials may have sầu stopped working because you might have sầu changed your hosting company or some useful information about your database which wasn’t manually updated in the wp-config file.

xuất hiện the wp-config file again just like before & find the database login information; it will probably be near the top of the file.

Xem thêm: Phần Mềm Google Drive Cho Pc, Android, Iphone, Google Drive


It shows the name of the database as “DB_NAME”, the login username as “DB_USER”, the login password as “DB_PASSWORD”, và the database host as “DB_HOST”. After going through all of this information, bởi vì make sure everything is correct as WordPress won’t be able lớn connect khổng lồ the database if any of these values are incorrect.

You can kiểm tra your database outside of WordPress by using PHPMyAdmin which is basically a tool for accessing và editing MySquốc lộ databases. But be careful when using this tool as it giao dịch directly with your site’s database – there’s a great chance of messing things up.

After logging in from your PHPMyAdmin trương mục, clichồng on your database from a danh mục of databases on your hệ thống. Make sure to lớn cliông xã on the one which matches the name in the wp-config tệp tin which you just checked.After clicking on the database name, you’ll be seeing a lot of names of the tables in your database. To make sure you’ve sầu got the database name right, find the table named wp_options & cliông xã on the Browse button next lớn it.This will take you lớn a page where you will be able to lớn see the name, URL, and other general settings of your WordPress trang web. Make sure it’s the same as it was in the wp-config tệp tin you saw earlier. If, however, it isn’t, go fix it in the wp-config file before proceeding.

Now that’s it for the database name. Moving on to the password và the username, there are a few ways lớn check if both of those are correct. One way is to check the already existing login username và password are correct is by creating a simple .php tệp tin which will demo lớn see whether or not you are able khổng lồ connect to lớn the database using the login credentials from the wp-config tệp tin or the existing login username và password. To thử nghiệm, just simply create a .php file, name it anything you lượt thích & add this code to it:

This code allows you khổng lồ kiểm tra the connection by going lớn the URL of the file inkhổng lồ your browser. For example, if I mix the name of the .php file to sample-file.php then I’ll use the URL You’ll either get a successful connection or in the worst case, another error with more details, which means your previous login credentials are not working và you need new ones.

To create a new database user và password we require using a different tool available in cPanel called MySquốc lộ Databases. Simply go clichồng on that and under the heading of MySquốc lộ Users cliông xã Add New User. This will take you to lớn a new page which asks you for the new login credentials.

Once you’ve chosen a username và a password, make note of them in the wp-config file. Click Create User to lớn continue. On the following screen, cliông xã on Add User to Database, choose your new username và your WordPress database and cliông chồng Add.

Once you’re done adding the new user khổng lồ the database, simply update your wp-config file with the new username và password. Doing this ensures your database name, password, & username is 100% correct.

Method 3: Fix Corrupted Files

If completely troubleshooting your database doesn’t even work, the last thing you can try is to fix any corrupted files in WordPress. Many users who failed lớn identify the underlying problem completely wiped all of their files and the error went away. So that’s exactly what we are going lớn vì chưng now as the error could be because of corrupted WordPress files.

That said, since we are talking about the main system files, you need khổng lồ be very careful. The tiniest of mistakes can wipe away your entire site. Before we proceed, it’s best khổng lồ backup all of your data either manually or by using a plugin. The next thing we need is new, fresh WordPress files lớn replace the old corrupted files that are causing problems for us.

Download a fresh copy of the WordPress CMS from it in your drive and delete the whole wp-contents folder and the wp-config tệp tin so there’s no chance of overwriting your current wp-config file. Plus all the hard work you’ve sầu put into setting up themes và plugins up till now won’t be lost.After deleting the infected or corrupt files, paste the rest of the files into your WordPress root folder using the File Manager or an FTPhường. client.

This will replace the corrupted files or the files which are causing the error. By following these simple steps you’ll be able to lớn easily fix the Error Establishing a Database Connection error. Reload the page once you’re done và you are all set to lớn go.

If you’ve followed the entire tutorial in order (và correctly) và the problem still persists, the next logical step would be lớn contact your website hosting service’s customer support representatives because it’s entirely possible that the error occurs at the VPS kết thúc. Rethành viên to lớn tell the customer support representative sầu about the measures you took khổng lồ resolve the problem.

Wrapping It Up

This Error Establishing a Database Connection error is one of the most frustrating errors you can encounter with your WordPress website. There are several different possible root causes for this error making it difficult to lớn identify và resolve it.

Xem thêm: Cách Xóa Triệt Để Lịch Sử Tìm Kiếm Tài Khoản Google Hoặc Gmail

We covered some of the most probable reasons behind this error & walked you through three easy-to-follow methods to lớn fix it. If you follow the steps outlined in this tutorial extra carefully you’ll have sầu your site up và running in no time.

Were you able to identify and fix the Error Establishing a Database Connection error on your WordPress website? Which method worked for you? We’d love sầu khổng lồ hear about your experience so let us know in the comments section below!