Error Establishing a Database Connection in WordPress is a common error in WordPress. Which frightens an users that his site got “hacked” or “broken”. It is a look like of white death error or white screen of death error in wordpress. Because there is nothing without a text “Error establishing a database connection”. This error actually means your wordpress cannot communicate with Database, so your total website goes down. This is a type of error what you should take an action immediately and take lightly to resolve the error establishing connection. Cause it is bad for your SEO and your visitors as well. So
Literature Review
To understanding better why this error happens first we need to know how wordpress structured.
WordPress is built with PHP. And most people uses : Mysql Database for wordpress. Though we can use other DB for WordPress that is an advance part.
In very basic WordPress uses PHP for functional work and Mysql to store all of your content in the database. This includes your posts, pages, but also the smaller elements, such as the title of your site, the layout of your widgets, the color settings, and etc. Basically, the MySQL database is where every, even the smallest piece of information about your website, is kept.
WordPress uses php to connects the database (Mysql). Then get the information when it needs and shows in the screen as output.
The Error Establishing a Database Connection actually occurs when PHP cannot connect with the Database. This can happens by:
- Incorrect login credentials: WordPress requires an user name, password and hostname to connect database. If any of these are wrong then it shows this error.
- Corrupted WordPress files: If the core wordpress files got corrupted or it hampers in the mid of any kind of wordpress update then this error could happen. Messing updates can includes plugins update, theme update and core wordpress update and etc.
- Corrupted database: If the database got corrupted by hackers or messing by your plugins then also this error can occur.
- The database server is down: If the hostname I mean the db host is down then PHP cannot able to connect with the database and shows this error in wordpress.
How to fix Error Establishing a Database Connection in WordPress
Before we start we always recommend you to take backup your wordpress website. Once you completed your backup then we can proceed how we can fix this wordpress error.
There is actually several methods to fix this. Such as:
Step 1: Checking Database Login Credentials
Very first step to fix this error is to check db login credentials. So, we already know that to connect with database wordpress needs db_name, db_user db_passwords, db_host.
Check Database Credentials in cPanel
- Database Name: The first thing to check is the database name. To do this you will need to login to phpMyAdmin in cPanel under the Databases section.
After signing in to the phpmyadmin you will see your database name:
- Resetting Database Username and Password: If you already know the database username and password then jump into the next section. If you doesn’t know the database username and password then continue reading. To reset your database from cpanel navigate to database section and click on MySQL Databases.
Now check the username of database which one is assigned to your wordpress website’s database.
Now scroll down to the “Current Users”. And click “Change Password”.
Now generate your passwords:
Now check those at your wp-config.php file. To do that check underneath.
Mysql Database Name: In wp-config.php file fiind this code:
// ** MySQL settings ** // /** The name of the database for WordPress */ define('DB_NAME', 'xxxxxx');
Check your DB name if it is correct. If it is wrong then change this. It will solve your problem.
Mysql Database User name: In wp-config.php file find this code:
/** MySQL database username */ define('DB_USER', 'xxxxxx');
Check if the DB user name is correct.
Mysql Database Password: In wp-config.php file find this code:
/** MySQL database password */ define('DB_PASSWORD', 'xxxxxxxxx');
Check if he database password is correct. If it doesn’t change it. Then fix this.
Mysql Host Name: In wp-config.php file find this code:
/** MySQL hostname */ define('DB_HOST', 'localhost');
Check if the Host name is correct or not.
Here is an example below of what the file looks like when opened.
Step 2: Checking Corrupted Theme or Plugin
We already seen to check corrupted database to solve this error. WordPress has massive collection of themes and plugins. In your WordPress website you must have a theme and some plugin to maintenance your WordPress website. So it is a very common scenario that plugins or theme causes so many unknown problems.
- Checking corrupted theme: At first login to your Cpanel or Login to you FTP. Then “wp-content” Now rename the folder named : “themes” to anything such as: “themes1″. Now hit your wordpress dashboard url in the browser such as “yourwebsite.com/wp-admin” see if it is accessible. If the wordpress admin dashboard is accessible then navigate to “Appearance > Themes” and activate a default theme such as “Twenty Sixteen”. Then hit your website if you still see the error then check your corrupted plugins.
- Checking corrupted plugins: At first login to your Cpanel or Login to you FTP. Then “wp-content” Now rename the folder named : “plugins” to anything such as: “plugin1″. Now hit your wordpress dashboard url in the browser such as “yourwebsite.com/wp-admin” see if it is accessible. If the wordpress admin dashboard is accessible then navigate to “Plugins” and activate one by one and check which one is the culprit. It will solve your Error Establishing a Database Connection problem.
Step 3: Check if the database has been corrupted.
WordPress database is a very complex thing. There is so many things that many of the developers doesn’t know. This is a very rare situation an user can face. To fix this open you “wp-config.php” file. Then add these lines of code:
define('WP_ALLOW_REPAIR', true);
If you don’t understand where to add check the image.
Now in browser hit this url “https://yourdomain.com/wp-admin/maint/repair.php“. Note: in yourdomain put your domain name.
Then a window will appear. Choose “Repair Database” from there.
After running the repair of the database above, ensure that you remove the line of code you added to your wp-config.php file, otherwise anyone could run the repair.
If no error, great! If the error’s still there, next step is:
Step 4: Re-upload WordPress Core files
The next possible reason you might be seeing the error establishing a database connection message is that your wordpress core files have become corrupt.
So to fix this first backup your wp-config.php and wp-content folder. Then go to wordpress.org and download latest version of wordpress.
Unzip this file on your computer. Inside, you need want to delete the wp-content folder, as well as the wp-config-sample.php file.
Now paste your wp-content folder and wp-config.php file here. Then upload through your FTP or Cpanel. It will solve the wordpress error.
Step 5: Contact your Host
If you see no troubleshoot is solving the problem last step is contact your hosting provider. They have the access in PHPMYADMIN. They can check if the database server is down. When they up the DB server then the problem will solve automatically
How To Avoid This Error In Future?
- Take a good hosting provider
- Back up daily.
I hope this tutorial help you to solve the Error Establishing a Database Connection in WordPress! And as always, feel free to reach out to me if you’re having some tough time with an error establishing a database connection.