There are several steps for resolving the error establishing a database connection on WordPress that you can try.
Before starting the tutorial, there are a few things you need to know to make it easier to understand the steps.
WordPress is built using two components, namely PHP and MySQL. When you access a web page through a browser, PHP will carry out its function to display data in the MySQL database and display it in the browser.
PHP queries are needed to display content in the form of a post title, author name, date, and so on.
In certain situations, the website will display the error establishing a database connection message.
This error can be caused by several reasons. For example, the PHP code cannot be connected to the MySQL datab2ase to get the required details when displaying a web page.
What Is Error Establishing A Database Connection?
Before continuing, you need to know first what the error establishing a database connection is? This error occurs because PHP cannot communicate with the database.
The WordPress CMS stores all of its data in the MySQL Database, which includes post data, page data, metadata, WordPress plugins, settings, login data, etc.
Both, if normal, will be related to each other in accordance with what is needed by website visitors.
When a website/blog visitor comes, PHP will execute the page code requested by the visitor and forward it to the database section, if you have found it then it will be displayed in the visitor’s browser.
If this process goes smoothly, no error messages will appear, but if there is an error, the error establishing a database connection message will appear on the browser as shown above.
Causes of Error Establishing a Database Connection
Before we discuss how to fix it, of course, we need to first identify what causes the error establishing a database connection message. Here are some of the most common causes when someone receives this message:
- Corrupt your database. There are many things that can cause your WordPress database to become corrupted. The most common cause is usually installing a certain plugin.
- Invalid Login Credential for Your Database. Your WordPress database uses a different login credential (username and password) than the one set up in your hosting account’s cPanel.
- Your Hosting Server is Down. The database server may be down and causing this problem, or maybe something else is happening with your hosting. In addition, it can also be caused by too many visitors to your website so that the server cannot accommodate them.
In this tutorial, you will learn how to find the cause of the error establishing a WordPress database connection and how to fix it.
How to Fix the WordPress Error Establishing a Database Connection?
Before you start the troubleshooting process, it’s a good idea to make a backup. The methods discussed below will manipulate the information in your database.
Therefore, it’s a good idea to back up your site data before you repair your WordPress website.
If you want to back up files manually, you can back up files using phpMyAdmin in your cPanel.
After you have finished backing up your website, then you can begin the process of troubleshooting your database connection problems.
Before moving on to the next step, make sure that you have:
- A WordPress website that is running normally
- Access to the control panel of the hosting where WordPress is installed
- Access to your WordPress website installation files
- Basic file structure knowledge of WordPress
1. Determining The Location of the Error in Advance
The first thing you have to do is make sure the error appears the same when you access the website (yourdomain.com) or access the WordPress admin dashboard page (yourdomain.com/wp-admin).
If the errors that appear in both of them are the same: Error establishing a database connection, please continue to Step 2 .
If your website goes down and gets a different error message when logging in like this,
One or more database tables are unavailable. The database may need to be repaired
that means you need to improve the website database.
Actually, WordPress already has a feature that can repair databases automatically. It’s just that, you need to activate it first.
Please access the wp-config.php file where your WordPress settings and configurations are located.
You can find the wp-config.php file in the main directory of your WordPress website. Open the file, then add the following code at the bottom of the script.
define ('WP_ALLOW_REPAIR', true);
This line allows you to optimize and improve the database by accessing the URL via a browser: yourdomain.com/wp-admin/main/repair.php (please change yourdomain.com with the domain of your website).
As seen in the image above, there are two options for repairing or optimizing the database. You can choose one, provided that the database optimization part will take a long time.
Please note, the database repair page is not secure. This means that everyone can access the yourdomain.com/wp-admin/main/repair.php page without having to log in first.
When you have finished repairing the database, make sure to delete the line of code that was added earlier, namely the line: define (‘WP_ALLOW_REPAIR’, true); file in wp-config.php.
This will disable access to the repair page and prevent others from tampering with your database.
2. Check Database Connection Settings in wp-config.php
If the previous step did not work, please check the next step through the wp-config.php file.
In some cases, if there is a problem with the database connection, it is sometimes caused by switching your hosting service.
In addition, it can also be caused by a change in the name or user information in the database and the wp-config.php file settings that have not been changed.
Reopen the wp-config.php file from your WordPress, then check 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 seen in the code above, there is the information you will need to access the WordPress database.
- Database name (DB_NAME)
- Username (DB_USER)
- Password (DB_PASSWORD)
- Database Host (DB_HOST)
If there is an error with one of the values, it is certain that WordPress cannot connect to the database.
The easiest way to determine if these details are correct is to compare them with the MySQL Database details.
Please enter your cPanel hosting and select the MySQL Databases section. As an illustration, here is a menu display that you will encounter.
After selecting the MySQL Databases menu, please enter the Current Databases section.
There are all databases and users used by your website. The information you are looking for is in the Database and Privileged Users column.
The goal is to match the code in the two columns with the code in DB_NAME and DB_USER in the wp-config.php file. For more details, please see the image below:
You don’t need to complete all the steps below to repair the website database connection if one of them is successful.
Therefore, always refresh your website to check if the error is gone.
Checking the Database Name
Start by checking the database name. If the Current Databases section doesn’t match the one in DB_NAME in the wp-config.php file, this means your database is missing.
This happens when you switch hosting services and the database doesn’t completely switch.
We advise you to contact support for your new hosting service and ask them to fully restore your website database.
This is because you need to create a new database and fill it with a backup file of your website database.
3. Checking the Username Database
Next, you need to check the Privileged Users column. In this example, we can see that the Privileged Users column for the username is different as shown in the wp-config.php file.
In this example, it is missing 0. To fix it, you just need to add it to the DB_USER line from somuvnet_wp67 to somuvnet_wp670. After that, please refresh your website.
4. Adding Less Privileged Users
However, when a WordPress website switches hosting or when performing a manual installation. it could be that you haven’t added the user to the database yet.
This problem is seen with cPanel hosting. You will see an empty Privileged User column, as in the image below which is marked with yellow.
To fix this, use the Add User To Database option as shown in the image below.
- First of all, go to the Current Users section and make sure the Users are the same as the DB_USER line in the wp-config.php file.
- If the same, please select the user in the Add User To Database > User via the dropdown menu. For databases, please enter the Database option then click Add. Select All Privileges, click Make Changes, then click Go Back.
- If different, you must add it via the Add New User menu by creating a MySQL username. Below are the details you need to enter in the Username field. We highly recommend Password Generator for generating passwords.
IMPORTANT: Please change the password in the DB_PASSWORD line in the wp-config.php file with the password you just generated. If it has been successfully created. please do step number 2 to add a user.
After the steps above are complete, refresh your website to check whether the error has been completely fixed or not. If the problem still persists, please try the steps below.
5. Check the Host Database
If the way to solve the error establishing a database connection in the previous step still doesn’t work, there could be two problems here, a password problem or a host database problem.
The database host is usually created together with your hosting account details. We recommend that you ask for this information on the hosting service you are using.
If there are changes, please change the DB_HOST line in the wp-config.php file according to the host of your hosting.
By default it usually uses localhost. When it’s finished, please refresh your website again to check for errors.
6. Check Login Password
Login password can be changed via MySQL Databases > Current Users on your cPanel hosting.
- Please select the user database that you want to change in the Users column then click Change Password.
- Here you can also use the existing password in the wp-config.php file or generate a new password using the Password Generator. If you choose to generate a new password, make sure you have updated the password on the DB_PASSWORD line in the wp-config.php file with the new password generated.
- Click Change Password.
All checks can be confirmed through the wp-config.php file. Refresh your WordPress website to check if the error is completely resolved. If there are still errors, please try the final steps below.
7. Contact the Hosting Service to Resolve the Error Establishing a WordPress Database Connection
If you have tried to solve the problem according to the steps above but you still get the Error establishing a database connection message, this could be due to a problem with your hosting service.
Please contact the hosting support team that you are using and tell them about the error details.
Describe in detail the actions you have taken, so that they can help solve the problem you are experiencing properly.
How To Avoid These Mistakes In The Future?
While there is no 100% fail-safe way to protect your site from errors in establishing a database connection, there are two main elements that you must have in your insurance puzzle to be at least 90% secure:
- Quality hosting. Only works with hosts that have optimized their platform to work with WordPress. That way, you can always count on extensive support and quick assistance. Not to mention that you’ll also be avoiding a lot of common WordPress problems. Here are our recommendations.
- Have a spare. Install a simple backup plugin like UpdraftPlus (it works automatically) and you should always have a working WordPress backup you can restore if all else fails.
This tutorial aims to fix problems on the website with the Error establishing a database connection message.
By applying the appropriate steps according to the cause of the problem, whether due to a corrupted database or an error in the line of code for connection to the database in the wp-config.php file, this problem can be handled properly.
Apart from problems caused by the website configuration, it is possible that there are changes to the WordPress hosting provider servers that cause this problem.
Are you still having trouble? Do not hesitate to contact us. Will you be happy to answer your questions in the comments column below?