Fix Wordpress Error: Establishing a Database Connection on your own

Fix WordPress Error: Establishing a Database Connection on your own

Establishing a database connection error in WordPress is the most common and frightening errors that the WordPress users can encounter or see. It means your website is no longer communicating or it has no access to your database and your whole website goes down. Also, this could be caused by many reasons. It takes about 20 minutes to detect and fix the problem.

WordPress is written mainly using two languages – one is PHP and another one is MySQL. All the information that helps in building your website is stored in a MySQL database. The code written in PHP language is used to access your MySQL database.

When someone visits your website, PHP executes the code on the page. It then displays it to the visitor in their browser. PHP is helpful for the storing and retrieving information from that database.

Error establishing a database connection in WordPress means that for some reasons the PHP code is unable to connect to your MySQL database for the retrieval of the information needed to build that page. That’s the reason you see this error on the blank page. If you can’t connect to your database no information about your website is available and there is no way to know what to display on the website.

Keep reading this blog if you want to know more about this database error and how to fix it. You can also take help from our WordPress support team to get help via remote access. Dial our toll-free helpline number 1 888 614 0555  to have a word with them.

Why do you get this WordPress error establishing a database connection?

  • Your database has been corrupted or your database server is unresponsive.
  • Many times this error happens due to some sort of the server error.
  • Your web hosting company may be at fault.
  • Your WordPress site may be using the separate login information for connecting to its MySQL database.
  • You have entered your database information incorrectly during the WordPress 5 minute install.
  • When moving a WordPress website to a new host DB_HOST it may be something unique to that host.

Does the problem occur for /wp-admin/ as well?

Firstly you should make sure that you are getting the same error on both the front-end of the site and on the back-end of the site (wp-admin). If the error message is the same on both the pages Error establishing a database connection then proceed to the next step.

If you are getting a different error on the wp-admin like the database tables are unavailable the database needs repair. You can do this by adding the following line in your wp-config.php file. Add it just before Happy blogging line wp-config.php.

define(‘WP_ALLOW_REPAIR’, true);

Once you have done, you can see the setting by going to this page:

http://www.yoursite.com/wp-admin/maint/repair.php

wprepairdb

The user doesn’t need to be login to access this functionality when this define is set due to its main intent is to repair a corrupted database. When the database is corrupted users cannot log in. So once you are done repairing and optimizing your database, remember to remove this from your wp-config.php.

If this repair has not corrected the problem, or you are having trouble running the repair, then continue reading this post as you can get another solution to work.

Checking the WP-Config file

WP-Config.php is one of the most important files in your complete WordPress installation. This is where you specify the details of WordPress to connect your database. If you have changed your root password or database user password, then you also have to change this file. First, you should always check that everything in your wp-config.php file is similar.

define(‘DB_NAME’, ‘database-name’);
define(‘DB_USER’, ‘database-username’);
define(‘DB_PASSWORD’, ‘database-password’);
define(‘DB_HOST’, ‘localhost’);

Remember that your DB_Host value may not always be a localhost. On the basis of the host, it will be different. Some folks suggested that they have fixed their problem by replacing the localhost with the IP. It is common to see such a problem at the time of running WordPress on a local server environment.  For example on the MAMP, the DB_Host value is working while changed to the IP.

define(‘DB_HOST’, ‘127.0.0.1:8889’);

IP’s will vary for online web hosting services.

In the case everything in this file is correct (make sure you check the typos), it is reasonable to say that there is something wrong at the end of the server.

Check your Web Host (MySQL Server)

Often you’ll see this error establishing database connection when your site gets swarmed with a lot of traffic. In fact, your host server cannot handle the load (especially when you are on shared hosting). Your site will actually slow down and error will also be output for some users. So the best part is that you should go with your hosting provider on the phone or livechat and ask them if your MySQL server is responsive.

For those users who want to test that the MySQL server is running itself, you can do a few things. Examine other sites on the same server to see if they have problems. If they are getting the same error, then there is definitely something wrong with your MySQL server.

In case you don’t have any other site on this same hosting account, just go to your cPanel and try to access phpMyAdmin and connect to the database. If you can connect, then we need to verify that your database user has sufficient permission. Create a new file named testconnection.php and paste the following code into it:

<?php
$link = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

Make sure that, replace the username and password. If successfully connected, it means that your user has sufficient permissions, and there is something else wrong. Go back to your wp-config file to make sure you have everything right (re-scan for typos again).

If you cannot connect to the database by visiting phpMyAdmin, then you know that this is something with your server. This does not necessarily mean that your MySQL server is down. This may mean that your user does not have sufficient permissions.

In our case, our MySQL server was running. All other sites on the server were working fine except wpglobalsupport. When we tried to visit our phpMyAdmin, we received an error:

#1045 – Access denied for user ‘foo’@’%’ (using password: YES)

We went on the phone with the Hostgator and their support immediately got the problem. Either way, our user’s permissions were reset. Not sure how this happened, but clearly this was the reason. They went back and restored the permissions and we were able to make the site back live.

So if you have been denied access to your phpMyAdmin or access via testconnection.php results, then you should contact your host immediately to fix it.

Solutions that Worked for Others

It is important to keep in mind that they cannot work for you. Use at your own risk and make sure that you have enough backups if something goes wrong.

The client was getting the error that the database needs repair. Even after repairing the database, the error did not go away. They tried different things and in the end, this issue was the site URL. Obviously, that was changed so that the error continues. He went to SQL queries by visiting phpMyAdmin:

UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’

Remember to replace YOUR_SITE_URL with the real URL example: http://www.wpglobalsupport.com. If you have changed the default WordPress database prefix then wp_options will be different.

It seems to fix the issue for him and few others people also commented on his post. This error was able to connect the database with testconnection.php. Therefore he changed the wp-config.php user to the root user. WordPress started working perfectly fine. Then he returned the settings back to the database-user, and it continued to work. He could not understand what was wrong but concluded that it was a typo.

They removed the content of active_plugins in the wp_options table and edited the recently edited content. Actually, it seemed to fix the problem.

We have read on several sources that users have uploaded the latest copy of WordPress and fixed this error. This is really a disappointing error. What have you tried that seems to work for you? We will be happy to extend this resource, so others will not have to waste time in finding a solution.

Well, that’s all on our behalf. If you need any additional support or help try calling our WordPress customer support phone numberToll Free : +1 888 614 0555.

More useful articles:

How to fix white screen of death in WordPress?

How to Fix 404 Error in WordPress



Leave a Reply