WordPress is the largest content management system reserves 30% of the internet in the world. Every popular Website runs on WordPress.
In fact, WordPress is amazing but apart from it, there are many issues that you might encounter while using WordPress. For example, WordPress Login and Internal server errors and many others. ‘Error establishing a database connection’ is also one of the errors establish abnormally.
But why do you get this database error? I will answer this question in a minute.
In this article, I will explain to you how to fix ‘database connection error’ in WordPress. If you aren’t comfortable with technical words. Don’t worry, take it easy.
By the end of this article, you will learn what the database error is and how to solve this error? Eventually, if you say ‘It was magnificent’ then it could be a great return for me.
This guide is not only for solving it but also for informing you why is this error being generated?
What is WordPress database error?
For understanding the entire process, you need to know two things. The first is PHP and the second one is MYSQL.
PHP (Hypertext Preprocessor) is a Language designed for developing website scripting. Generally, used by open source software like WordPress, Joomla, Drupal etc.
Before I talk about MYSQL, Let’s have a look at Database.
The database is a storage which helps to manage and storing web stuff in the form of the matrix. Sometimes it is called ‘open source database management system.
Now let’s take the earlier discussion.
MYSQL is a free database storage application or server. Any time you publish a post in WordPress, is stored in MYSQL space.
Ok, now you know both the things so it would be easy to know how WordPress interacts with MSQL database.
The answer is the PHP, it works like a bridge between WordPress and MYSQL. WordPress uses PHP to retrieve the data from MYSQL database server. PHP dynamically interacts with the database and identify the corresponding data.
MYSQL deals with data that means it requires a very large storage. More data you add to your website, the larger space it requires. Your server can’t perform well unless your database has enough space. Thankfully, Hosting companies provide you with a suitable space according to your business so that you may store more web stuff.
Firstly, I want to list out all the reasons responsible for ‘database connection error’.
- A huge spike in website traffic
- Wrong database login credentials
- Wrong database address (use IPs instead of ‘localhost’)
- Change in user’s permission
- Faulty plugin
- Less server memory (RAM)
- MYSQL database has been upgraded
- Some sort of unknown server errors
Above are the possible reasons that may cause database connection error. But these aren’t equally responsible for it. This error can be seen on both side wp-admin (back-hand) and website (front-hand). If you don’t get the same message on both the ends then you need to repair your database.
For doing so, head to CPANEL > file manager > PUBLIC_HTML folder and look for WP_ CONFIG.PHP file. Open the file and find the following code.
define(‘WP_DEBUG’, false); [Make sure you replace false with true]
So, your current code looks like
If you don’t find the code, add the code given below in between these two lines written below.
* @link https://codex.wordpress.org/Debugging_in_WordPress
/* That’s all, stop editing! Happy blogging. */
Now, open this link http://www.yoursite.com/wp-admin/maint/repair.php. [Make sure you replace yoursite.com with your domain name].
Once you have done, you are ready to repair your database. You can choose any option either ‘repair database‘ or ‘repair & optimize database‘. After that you can reload your site to check whether it works.
I heard from more than thousands of people and identified that ‘Wrong database login credentials’ is potentially causing this error. I have plotted a pie chart through which I observed that approximately 370 (Out of 1000) people are encountering this error due to incorrect database login credentials. Let’s have a look at the data…
You might be wondering how this error is hard to investigate if you don’t know the right thing, isn’t it? Don’t worry, I am here to assist you.
Let me tell you something that you must be remembering.
- Before you start tweaking with WordPress database, take the backups so that you may recover your website. You can either download the plugin or take the backup manually.
- If you need guidance with each aspect of the database, the hosting provider has your back.
- Must know a little bit about WP-config.php file.
- Sometimes, this error arises for a few minutes. So wait for at least 10 minutes or ask for hosting server support.
By the way, there are many options to address. Let me take each of these ones by one…
7 Ways To Solve ‘Error Establishing A Database Connection’ In WordPress, 2nd is Very Important
#1. A huge spike in website traffic
As you get more and more traffic, you will find one thing to be true: your MYSQL server respond time become high and it may cause the database connection error. This is because your WordPress is unable to connect the database.
#2. Wrong database login credentials
If you’re new to it, WP-CONFIG.PHP is a file connect WordPress to MYSQL database. This file contains your DB_NAME, DB_ PASSWORD, DB_ USERNAME, DB_HOST. This is the main part of your WP-CONFIG.PHP file. If these are incorrect, it will cause the ‘WordPress database error ’. Compare these data with your actual MYSQL database credentials. If any of these data incorrect then make them correct.
As I have mentioned that MYSQL is a server storage that stores all your site’s stuff. So, it’s essential to connect it to WordPress. WordPress uses SQL strings within the PHP tag to interact with the database. This Info is available on the WP-CONFIG.PHP file. PHP compile the file and retrieve the site details from the database.
While entering wrong details, phpMyAdmin will show the following error.
If there is no match between WP-CONFIG.PHP DB details and actual MYSQL database credentials. That means your WP-CONFIG.PHP file has incorrect DB_USERNAME and DB_PASSWORD.
If you’re unable to find the DB_NAME, you can find it by going into the phpMyAdmin. Remember that you need DB_USERNAME and DB_PASSWORD for getting in.
But if you don’t even know the DB_USERNAME and DB_PASSWORD, then you need to contact your hosting provider.
#3. Wrong database address (use IPs instead of ‘localhost’)
Sometimes people observe that using server IPs instead of ‘Localhost’ may fix this error. But this isn’t always the case but it might be.
This depends on the server location. By default, the address of hosted-database is ‘Localhost’. But the location may be different for different hosting.
#4. Change in user’s permission
WPbeginners noticed that change in user’s permission may cause error establishing a database connection. WPbeginners contacted to hosting provider and within a few minutes, they solve the issue.
#5. Faulty plugins
While the plugins are important, you don’t want to delete them. But unwanted plugin not worth installing. So, only download those that are essential. Because a faulty plugin may cause many issues form login issues to ‘error establishing a database connection’.
Here you need to deactivate all the plugins on your server. It could be done either by getting into the Cpanel or FTP. Once you were done, you can reload your site to ensure whether it works.
#6. Less server memory (RAM)
This cause is similar to the first one. If your server has loaded with tons of files. It means there is no space for running a PHP application. The solution would be similar to first one.
#7. MYSQL database has been upgraded
Sometimes your hosting provider upgrades your server. For example, MYSQL 5.4 upgraded to MYSQL 5.7.19. This transition may change your database name. This change can’t be taken by WP-CONFIG.PHP file. Resulting, WordPress doesn’t recognize the database and shows this error.
Let’s see how you can make it fix
First of all, head to the cPanel > phpMyAdmin. Once entered, you see your database name in the left corner of the screen. Now copy the name carefully and paste it here.
Save the file and try to reload your website. Hopefully, it would work for you.
#8. Some sort of unknown server errors
There might be some additional issues that cause ‘error establishing a database connection’. If you try everything but nothing works then contact your hosting provider.
I have explained every possible reason, and I am sure by reading this guide you won’t miss any mass. But there might some other nonsense that you can’t identify. In such case, you can hire any web developer. If you aren’t an expert then I refer you contact your hosting provider. They will guide you what’s the problem and how to solve it.
If you find this article helpful, do share on Facebook, Twitter, LinkedIn and Reddit.