How to Fix an Error in Establishing a Database Connection in WordPress

Joomla! or WordPress: what CMS should you use?
How Does Affordable SEO Services Work?

Are you ready to get rid of another one of the most common errors in existing WordPress? In this guide, we will show various methods to correct the Error Establishing a Connection to the database in WordPress.

Overall, WordPress is written in a combination of two languages:. PHP and MySQL Whenever you open a page of your website, this is exactly what happens. The PHP code issues requests to access the MySQL database in order to seek the necessary information to open a web page. Thus, a request is driven to write the title of the post, to seek the author’s name and so on. 

So whenever you see the error message to Establish a connection with the database, it simply means that your PHP code has not been able to establish a connection to the MySQL database. It was also not able to get the details needed to form and display a page.

image shows the error while establishing a connection to the wordpress database

There are many reasons why your site may have problems connecting to the database. They are divided into three broad categories.

  1. Your login credentials to access the MySQL database are wrong. The WordPress database uses login credentials (username and password) different from the control panel of your hosting account. If these credentials have been changed recently or if your WordPress site has been moved to a new hosting account or hosting provider, it can not access the database with the old credentials. This is the most common cause of the error.
  2. Problem with one or more plugins . Sometimes a problematic plugin can also be the cause of this error in WordPress.
  3. The database server is down . There is a possibility that the server your database is idle. Or may be experiencing problems due to some setbacks in which your hosting company is having. In such cases, your site will have problems establishing a connection to your database and, therefore, will indicate the error.

In this tutorial you will learn how to find the cause of Error Establishing a Connection to the database in WordPress . Better than that: you will learn to how to fix it once and for all!

What will you need

Before beginning this guide, you need the following:

  • WordPress site already in use;
  • Access to the hosting account control panel where WordPress is installed;
  • write access to the WordPress installation files;
  • basic understanding of the WordPress file structure.

Step 1 – Determine where the Error Establishing a connection to the database is going

First, you should make sure your stuff. 1) You are getting the same error on the front end of the site  seusite.com . 2) The site’s back-end seusite.com/wp-admin (replace seusite.com with the URL of your homepage in WordPress). If the message Error Establishing a connection to the database appear in two pages, then proceed to Step 2

But if your site is showing a different message when trying to log in it, then you may need to repair your database. This message can be: One or more database tables are unavailable. The data bank may need to be repaired

Worried? Relax! WordPress has a built -in tool that helps you recurar your database. To do this, you need to access the file wp-config.php ,  that contains the WordPress installation settings.

You can find wp-config.php  in the root directory of your WordPress site.

access the wp-config directory wordpress to resolve the error while establishing a connection to the database

After opening the wp-config.php , add this line of code to the bottom and save the changes:

  1. define ( ‘WP_ALLOW_REPAIR’ , true ) ;
define ( 'WP_ALLOW_REPAIR', true);

Save the changes. This line will allow you to optimize and repair your database. Do this by browsing link seusite.com/wp-admin/maint/repair.php (just replace yourwebsite.com with it to your actual URL).

step to repair wordpress database and correct the error while establishing a connection to the database

A screen like the one above should appear. From here, you can choose two options: Repair Database and Repair and Optimize Database . Feel free to choose. If you choose the first option, the optimization will take longer. If you choose the second, the repair and optimization will take place at the same fear. 

It is important to know that the the database repair page is not secure. And anyone with access to the URL can open it without login. Also, once you’ve finished the repair process, make sure to remove the line of code added to the wp-config.php file.

It is highly recommended to keep the file wp-config.php and make sure the error is resolved. If not, we will use that same file in the next step.

Step 2 – Checking database connection settings in wp-config.php

If the previous step does not work, the next thing to do is move the database settings in the file wp-config.php .

In this step, we check the login credentials of your database. You will need the file wp-config.php again. If for some reason you closed the end of the previous step, log in File Manager or FTP and look for the file wp-config.php between the core files of WordPress. 

Open the file and fill in the requested credentials  near the top of the file. It should look something like this:

  1. // ** MySQL settings – You can get this info from your web host ** //
  2. / ** The name of the database for WordPress * /
  3. define ( ‘DB_NAME’ , ‘u694443746_aujev’ ) ;
  4. / * ** MySQL database username /
  5. define ( ‘DB_USER’ , ‘u694443746_vaven’ ) ;
  6. / ** MySQL database password * /
  7. define ( ‘DB_PASSWORD’ , ‘zuWupEXEdu’ ) ;
  8. / * ** MySQL hostname /
  9. define ( ‘DB_HOST’ , ‘MySQL’ ) ;
  10. / ** Database Charset to use in creating database tables. * /
  11. define ( ‘DB_CHARSET’ , ‘UTF – 8′ ) ;
  12. / ** The Database Collate type. Do not change this if in doubt. * /
  13. define ( ‘DB_COLLATE’ , ) ;
// ** MySQL settings - You can get this info from your web host ** //
/ ** The name of the database for WordPress * /
define ( 'DB_NAME', 'u694443746_aujev');
/ * ** MySQL database username /
define ( 'DB_USER' 'u694443746_vaven');
/ ** MySQL database password * /
define ( 'DB_PASSWORD' 'zuWupEXEdu');
/ * ** MySQL hostname /
define ( 'DB_HOST' 'MySQL');
/ ** Database Charset to use in creating database tables. * /
define ( 'DB_CHARSET', 'UTF-8');
/ ** The Database Collate type. Do not change this if in doubt. * /
define ( 'DB_COLLATE', '');

If you roll your mouse pointer a little lower down, you will also see a line that defines prefer your MySQL database:

  1. $ table_prefix = ‘wp_’ ;
$ Table_prefix = 'wp_';

WordPress need the following information for you to access your database:

  1. The database name ( DB_NAME )
  2. The user login name ( DB_USER )
  3. The login password ( DB_PASSWORD )
  4. The database host ( DB_HOST )
  5. The database prefix ( table_prefix )

If any of these values ​​are incorrect, WordPress can not connect to the database.

The best way to ensure that the details are correct is to compare them with the actual details of the MySQL database. To do this, you need to navigate through your hosting account and go straight to the section MySQL Database . Below are the steps on how you can do this from the control panel of the cPanel Hostingere. 

control panel Hostinger

database access in hpanel to correct the error while establishing a connection to the database

Once the screen MySQL Databases , locate the section Bank of Current Data . It lists all databases and users who are ready to use for your site. The information we are looking for are located in columns Databases and Privileged Users . Our goal is to combine the values in these two columns with the values DB_NAME and DB_USER the file wp-config.php .

values ​​in the mysql database by hpanel to correct error while establishing a connection to the database in wordpress

Make sure that the values wp-config.php coincide with litados values in section MYSQL database in the Hostinger panel. If not, update your file wp-config.php with the correct values and save the changes you make.

Recharge your WordPress site. If you still have the connection error the database, it is possible that the password of your MySQL user and password for the wp-config.php are not the same. In this case, you just need to redo a new password for your MySQL server. You can do this by pressing the icon + next to the MySQL database and press the button Change Password .

change password in mysql hpanel database to resolve the error while establishing a connection to the database

Now update the wp-config.php with the new password and recharge your WordPress site. If nothing happens, there is a chance that WordPress is using a table prefix incorrect. You can check this by going to phpMyadmin and opening your WordPress database. See the table prefix and make sure that it is identical to the value of the file wp-config.php .

table prefix by phomyadmin to correct error while establishing a connection to the database

If still the By Error Establishing a connection with the MySQL database stay, it’s time to try Step 3.

cPanel

access to mysql database by cpanel to correct the error while establishing a connection to the database

When you are already in the screen database MySQL , find the section Current Database . It lists all databases and users can be used on your site. The information you want is located on the columns Databases and Privileged Users . We aim to combine the values in these two columns with the values values DB_NAME and DB_USER the file wp-config.php .

values ​​in the mysql database in cpanel to correct error while establishing a connection to the database

You do not necessarily need to complete all of the following actions to restore connectivity of WordPress. Still, when completing each stage, always remember to recharge your WordPress site to see if the bug has been fixed.

Checking the value of the database name

checking the value of the database name to correct the error while establishing a connection to the database

We will start with the value of the database name. If the section Current databases do not list the database that is shown in the file wp-config.php with the DB_NAME value, the database is missing.

This usually happens when a website is transferred to a new hosting provider and the database is not completely moved. We recommend that you contact the support team your new hosting provider and ask them to move or completely restore the database WordPress. This should lead to the creation of a new database, and fill your content with the backup of your WordPress database.

Checking the value of the database login user 

Second, we will check the column Privileged Users . In this example, we can see that the column Privileged Users list a different user name shown in the file wp-config.php – the latter with a ‘0’ on the right. After updating the value DB_USER the somuvnet_wp67 to somuvnet_wp670 , fixed the incorrect user name. Be sure to recharge your WordPress site to see if the error has been resolved.

Adding a privileged user who is missing

When transferring to another WordPress site hosting provider or install WordPress manually, you may forget to add a user to the database. This problem usually arises in control panels that support advanced user privileges. In this case, you would see a field Users Privileged empty.

adding a privileged user name to correct the error while establishing a connection to the database

To fix this, we have to use the section Add User to Database , which is located at the bottom of the screen:

add new privileged mysql user to correct the error while establishing a connection to the database

  1. The first step is to locate the section Current Users and see if the value DB_USER file wp-config.php is listed.
  1. If it is listed, choose that user in the drop – down field Add User To Database> User . Choose the respective database from the drop down field Database and click Add . You will be prompted to set privileges. Select  All privileges , click Make Changes and then click Back . 
  1. If it is not listed, you have to use the section Add New User , which creates a MySQL user name. Here you need to enter a user name or suffix in the field User Name and recommend using the field Password Generator to the field the Password .

IMPORTANT: you must immediately update the value DB_PASSWORD the file wp-config.php with the new value generated so that it is not lost in the process.

Once the user is added (created), you need to perform the above step (2) to add it to the database. After performing the steps above, make sure to update your WordPress site to see if the error has been resolved.

Checking the value of the database host

If the steps above have not yet solved the error, there are two other information that may be incorrect. They are: the login password or database host. The value of the database host is usually supplied with the account details.

We recommend checking this information with your hosting provider if it can not be found in the e-mail account configuration or the control panel. If you changed the value of the database host, update your WordPress site to see if the error has been resolved.

Checking the value of the login password

The login password can be quickly updated using the section MySQL Database> Current Users in Control Panel.

  1. Locate the user for which you want to update the password in the column user and click Change Password in the respective line.
  1. You can use the value DB_PASSWORD file wp-config.php or generate a new password using the Password Generator . If you choose to generate a new password, be sure to update the value DB_PASSWORD the file wp-config.php with the newly generated password.
  1. Click Change Password .

This completes all the checks to be made with the file wp-config.php . Be sure to update your WordPress site to see if the error has been resolved. If the error persists, go to the last step.

Checking the value of the table prefix

checking the value of the table prefix in cpanel to correct the error while establishing a connection to the database

Last but not least, make sure that the prefix of your table is correct. Open your database using phpMyAdmin and see if the values match the file wp-config.php .

Step 3 – Disabling plugins in WordPress

If the previous two steps do not resolve, it’s time to check installed plugins in your WordPress site. The best way to do this is first disabling all plugins and check if your site back to normal soon after. If so, a faulty plug may have been the real cause of the error. And once the error has been diagnosed, you can still try to find what the exact plugin is responsible for Error Establishing a connection to the database. Follow the steps below to disable plugins in WordPress.

Log in to your account using a client FTP .

Locate the core WordPress files, and look for the file called wp-content . Access it. Find the folder named plugins .

Rename the file to something else, such as plug-disabled . It’s just to fool WordPress that this is not the plugins folder.

renaming plugins folder in wp-content to correct error while establishing a connection to the database

Check your site and confirm that these steps corrected the error.

If so, the problem was actually in one of the plugins. Now, let’s find the really problematic plugin. What was causing the error to Establish a connection with the database . What you need to do is:

Rename the just back of plugins for plugins and access it.

Rename each plugin one by one, to a different name to ensure that WordPress does not charge those same plugins. An example of a name that you can give is akismet-disbaled .

Check your site every time you disable a plugin to see if the problem was resolvido.Ainda so if your site does not return to normal, it is time to try the next step.

Step 4 – Replacing the core files of WordPress

IMPORTANT: Be sure to create a full backup of your site before proceeding with this step.

If the Error Establishing a connection with the database  still exists, you can try replacing the core files of your WordPress site. This is for a couple occasions when corrupted or infected files may be preventing your site to connect to the database.

In this situation, you should consider the possibility of replacing the core files of your WordPress site with copies of new and unchanged files. You thought too risky or complicated? Do not worry: at the end of this step, you will be quiet and your site will usually be restored.

To replace the key files from your WordPress site, do:

  1. Download the latest version of WordPress and unzip the folder.
  2. Delete your directories wp-includes and wp-admin server.
  3. Upload new versions of these folders.

And that’s all you have to do to replace the core files of your WordPress site. That should put an end late on  Error Establishing a connection to the database .

Step 5 – Contact your hosting provider

If you followed the steps above and your website still shows the message Error Establishing a connection to the database , this can be a problem with your hosting provider. Contact the company’s support. Talk to them and provide all the details of the error and troubleshooting actions you tried before. They should be able to help you determine the cause of the error.

But before you contact them, there is one more thing you can do to find out why you are having the message  Error Establishing a connection to the database . Sometimes it is the database itself provided by your web hosting company that is offline.

This can happen for many reasons and it is not as rare as you might imagine. If this is the case, you would have more information at hand to pass on to your hosting provider. Something that would speed up the process of resolving the problem.

To verify that the database is offline or not, you need to run a simple PHP script. The only work of this script is to find out if your site may or may not complete a connection to the database. The script is below:

  1. <? Php
  2. $ servername = “mysql.hostinger.com” ;
  3. $ database = “u266072517_name” ;
  4. $ username = “u266072517_user” ;
  5. $ password = “buystuffpwd” ;
  6. // Create connection
  7. $ conn = mysqli_connect ( $ ServerName, $ username, password $, $ database ) ;
  8. // Check connection
  9. if ( ! $ conn ) {
  10. die ( “Connection failed” . mysqli_connect_error ( ) ) ;
  11. }
  12. echo “Connected successfully” ;
  13. mysqli_close ( $ conn ) ;
  14. ?>
<? Php
$ Servername = "mysql.hostinger.com";
$ Database = "u266072517_name";
$ Username = "u266072517_user";
$ Password = "buystuffpwd";
 
// Create connection
 
Mysqli_connect $ conn = (ServerName $, $ username, password $, $ database);
 
// Check connection
 
if (! $ conn) {
 
    die ( "Connection failed". mysqli_connect_error ());
 
}
echo "Connected successfully";
mysqli_close ($ conn);
?>

The only details you need are your server name and the specific name of the database that you are trying to connect. Besides, of course, the username and password to login. We list the values of another of our tutorials, which is also valid check because it shows how to connect to the MySQL database by PHP .

Needless to say, but you must replace these values ​​with the values ​​of connection with your MySQL database showed that in Step 2.

Next, we create a new variable $ conn, which  will capture the return value of the method mysqli_connect () . If this variable is empty, you will see the message Connection failed: <ERROR_NAME> . On the other hand, a successful connection will cause the message Connected successfully appears on the screen.

This script must let you know if the database is offline or not. Something that could help you to give valuable information to your hosting provider.

Drago help you troubleshoot the error while establishing a connection to the database in wordpress

Conclusion

This tutorial is to help you correct the  Error Establishing a Connection to the database  in WordPress. By applying these steps, we can determine whether the error lies in the corrupted database or database connection of incorrect values in the file wp-config.php .

Eventually, it may be that something is wrong with the hosting provider. But it is worth going through the steps above before contacting with your hosting company. 

Want to rely on 24/7 support in your web hosting plan? See our plans and come to Hostinger!