How to deal with the error when establishing a database connection WordPress

Your website suddenly shows a white screen with the text “Database connection issue? ”. This error may occur, one of which is due to your database experiencing errors corrupt.

If this error occurs while establishing a database connection on your site, do not panic. Because you have come to the right place. In this article you will learn about the ins and outs of the error to establish a database connection. Starting from understanding, causes, to how to overcome them. Come on, look to the end!

What’s wrong with establishing a database connection?

Establishing a database connection is a bug that occurs when PHP cannot access data from your site’s database.

WordPress is built using two components, namely PHP and MySQL. When you access a web page through a browser, PHP will perform its function of displaying the data in the MySQL database and displaying it in the browser. PHP queries are required to display content in the form of a post title, author name, date, and so on.

In certain situations, the website will display an error that establishes a database connection due to various errors. For example, PHP code cannot link to a MySQL database to get the necessary details when a web page is displayed.

Causes of Errors Establishing a Database Connection

The cause of the error establishing a database connection in WordPress can occur due to the following things:

  1. Your database is corrupt. There are many things that can cause your WordPress database to crash corrupt. The most common cause is usually due to the installation of certain plugins.
  2. The credentials for your database are incorrect. Your WordPress database uses different credentials (username and password) than those set up in your host account’s cPanel.
  3. Your host server is down. The database server may be down and causing this problem, or something else may be happening with your hosting. In addition, it can also be caused by too many visitors to your site so that the server can not accommodate them.

In this tutorial you will learn how to find the cause of the error establishing a database connection in WordPress and how to fix it.

Also read: How to optimize MySQL database with PHPMyAdmin

How to fix WordPress error by establishing a database connection?

Before proceeding to the next step, make sure that you:

  • WordPress site runs normally
  • Access to the hosting control panel where WordPress is installed
  • Access Your WordPress Website Installation Files
  • Knowledge of basic file structure of WordPress

1. First determine the location of the error

The first thing you need to do is make sure that the error that appears is the same when you access the website ( or the WordPress admin dashboard page ( If the error that appears on both is the same: Could not establish a database connection, please continue Step 2.

If your site is down and you get another error message when you log in like this:

One or more database tables are unavailable. The database may need to be repaired

That is, you need to restore the website database.

In fact, WordPress already has a feature that can recover the database automatically. Just, you need to activate it first.

First log in to cPanel. Enter the Niagahoster Membership Area page, then click on menu Manage Hosting. Then click File manager.

Please access files wp-config.php in the guide public_html.

The first way to resolve this issue is to modify a database connection by modifying wp-config.php

Right click on the file this, then click Edit. Then add the following code at the bottom of the script.

define(‘WP_ALLOW_REPAIR’, true);

This line allows you to optimize and restore the database by accessing the URL via a browser: (Please change with the domain of your website).

As seen in the image above, there are two options to repair or optimize the database. Well option Database recovery and optimization will take quite a while.

Please note that this database recovery page is not secure. This means everyone has access to the page without first logging in.

When you have finished restoring the database, make sure you remove the rule code that was added earlier, namely the rule: define (‘WP_ALLOW_REPAIR’, true); which is in wp-config.php. This will disable access to the recovery page and prevent others from damaging your database.

2. Check database connection settings in wp-config.php

If the previous step did not work, please go to the next step through the file wp-config.php.

In some cases, if there is a problem with the database connection, it is sometimes caused by changing your host service. In addition, it can also be caused by a change in the name or user information in the database and the file settings have not been changed wp-config.php.

Reopen the file wp-config.php You then do a check on the connection settings to the database.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'somuvnet_wp670');

/** MySQL database username */
define('DB_USER', 'somuvnet_wp67');

/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

As you can see in the code above, there is the information you need to access the WordPress database.

  1. Database Name (DB_NAME)
  2. Username (DB_USER)
  3. Password (DB_PASSWORD)
  4. Database Host (DB_HOST)

If there is an error in any of the values, it means that WordPress cannot connect to the database.

The easiest way to verify that the details are correct is to compare them with the MySQL database details. Please login to your hosting cPanel and select section MySQL databases. By way of illustration, here’s the menu display you’re facing.

After selecting the menu MySQL databasesplease go to section Current databases. There are all the databases and users used by your website. The information you are looking for is in the column Database and Privileged users.

The goal is to match the code in the two columns with the code in the DB_NAME and DB_USER what’s in the file wp-config.php. For more details, please see the image below:

You do not have to complete all the steps below to set up your website database connection. Only try a few times until the problem with establishing a database connection is resolved. For that, always refresh your website to see if the error is gone.

3. Check Database Name

Still trying on database settings check database name. As part Current databases not the same as the one on DB_NAME on file wp-config.php, it means your database is missing. This happens when your host services change and the database does not switch completely.

We encourage you to contact support for your new hosting service and ask them to fully recover your website database. This is because you need to create a new database and fill it with a backup file for your website database.

4. Check the Username Database

The way to fix the error that establishes a database connection is to check the database username in the column Privileged users.

In this example we can see the column Privileged users because the username is different as seen in the file wp-config.php. In this example, the number 0 is missing. To correct it, you just need to follow the DB_USER line of somuvnet_wp67 Word somuvnet_wp670. After that, please refresh your website.

5. Addition of less privileged users

However, when a WordPress site changes hosting or when you install it manually. you may not have added a user to the database. This issue is seen in cPanel host. You will see the column Privileged user empty, as in the picture below marked in yellow.

fixes error while establishing a database connection 4

To fix it, use options Add user to database as seen in the picture below.

fix error while establishing a database connection 5
  1. First, please go to section Current users and make sure Users same as in the line DB_USER on file wp-config.php.
  2. If the same, please select the user in the Add user to database > User via the drop-down list. For database, please go to options Database then click add. choose All privilegesclick Make changesthen click Go back.
  3. If it is different, you must add it via the menu Add new user by creating a MySQL username. Here are the details you need to enter in the fields Username. We strongly recommend Password Generator to create a password.
    URGENT: Please change the password on the line DB_WAGWOORD on file wp-config.php with the password you just generated. If it was created successfully. Please follow the step in number 2 to add a user.

After completing the above steps, refresh your website to see if the bug has been completely fixed or not. If the problem persists, please try the steps below.

5. Check Host Database

If the way to fix the error to establish a database connection in the previous step still does not work, there may be two problems here, the problem is: password or problem database hosts.

The host database is usually created along with your host account details. We recommend that you request this information from the host service you use.

If there is a change, please change the line DB_HOST on file wp-config.php according to the host of your host. By default usually used local host. When you’re done, please refresh your site again to check for errors.

6. Check the login password

Login password can be changed via MySQL databases > Current users in your cPanel hosting.

fix error establishing a database connection 8
  1. Please select the database user you want to change in the column Users then click Change password.
  2. Here you can also use the password contained in the file wp-config.php or generate a new password by Password Generator. If you choose to generate a new password, make sure you have updated the password on the line DB_WAGWOORD on file wp-config.php with the new password generated.
  3. Click Change password.

All checks can be confirmed by the file that it has been completed wp-config.php. Refresh your WordPress site to see if the bug is completely fixed. If there is still an error, please try the last step below.

7. Contact the host

If the error establishing a database connection in WordPress did not work, there may be a problem with your host service.

For this, please contact the support team of the host you are using and tell them the details of the error. Explain in detail the actions you have taken so that it can help to solve the problem you are experiencing properly.

Also Read: How to Fix 40+ WordPress Errors

Goodbye Error establishing a database connection!

Error establishing a database connection is a notification that appears when you experience your site’s database corrupt. Other causes may also be due to an error in the code line to connect to the database in the wp-config.php file,

If your website is experiencing this, you can follow the method to overcome the error to establish a database connection that we reviewed in this article. By implementing the right steps, we hope that the error can be resolved properly.

4+ Cara untuk Menyelesaikan Ralat 503 Perkhidmatan Tidak Tersedia…

Adakah anda terkejut bahawa tapak web anda hanya memaparkan skrin putih yang menyatakan “Perkhidmatan Ralat 503 Tidak Tersedia”? Jangan panik, anda telah menemui artikel...
3 min read

Cara Menyelesaikan Error 405 Method Not Allowed dalam WordPress

Adakah tapak web anda pernah mengalami ralat dengan mesej “Error 405 Method Not Allowed? “. Mesej ralat ini pastinya bukan berita baik. Kerana kaedah...
5 min read

9 einfache Möglichkeiten, den 401-Fehler auf der Website zu…

Man kann sagen, dass der 401 Unauthorized-Fehler einer der nervigsten WordPress-Fehler ist. Warum? Besucher können sich nicht auf der Website anmelden und ihre Funktionen...
6 min read