If you are not used to handling databases, you may be surprised to see the message “Error Establishing a Database Connection on WordPress.” It commonly happens when you migrate your WordPress website to a new server. Alternatively, there may be a problem with your database. In this article, we will observe how to fix this issue. However, before discussing the approaches to a solution, we will see why it happens.
The causes of the problem “Error Establishing a Database Connection on WordPress.”
Such an error can occur in the following 4 cases:
- You entered your database connection information incorrectly.
- A difficulty has happened with your database server.
- Some features of your server corrupted Your database.
- You don’t have to change hyperlinks if you’re using multisite WordPress.
As we can see, the issue is usually due to the database. We will therefore see how WordPress establishes the connection with a database.
How does WordPress connect with a database?
To establish a connection between your web server and your database, WordPress uses the wp-config.php file. Indeed, in the leading WordPress directory, you will see a wp-config.php file. This file has, for example, several functions and constants. Among the constants, we can mention the connection to the database, the codes to handle errors or the encryption data.
You typically need to create the data on your server to connect to the database. You will add your database data on lines 23, 26 and 29. You will see the error message when accessing the website if this information is unavailable or wrong. We will now explore how to resolve these problems.
You entered your database connection information incorrectly
If you mistype your database information, you will inevitably get an error. To achieve this, you must review whether you have the correct database name, username, or password. Frequently, if you’ve made a mistake on the password, you need to reset and replace it to resolve the issue.
A difficulty has occurred with your database server
For some reason, the website may have trouble connecting to the server. In this case, resetting the server might resolve the issue. You can contact your provider if you don’t have access to the reset feature.
Some features of the new database corrupted Your database.
Occasionally, the database gets corrupted. There can be several reasons for this.
- Server configuration
- The data encryption or decryption system has changed.
- An error occurred during updates.
You need to use the database repair feature to resolve such an issue. Then, you must add the code below to your wp-config.php file and reset your website.
When you add this code, all you have to do is launch the URL of your website, and options will appear on the screen,
You don’t have to change hyperlinks if you’re using multisite WordPress.
When you have a WordPress website with multisite functionality, you may get this message when migrating to a new domain name. In this case, you will have to modify the URL in the wp-config.php file and the wp_options, wp_site, and wp_blogs tables. To alter the links in the plains of your database, you can only go to the space for managing your DB. Then it would help if you did the queries below. First, change the URL in the wp_options table.
UPDATE wp_options SET option_value='https://dev.example.com' WHERE option_name='https://www.example.com'
In this expression, https://dev.example.com is the initial site URL, and https://www.example.com is the new website URL. It would help if you adapted this query for the wp_site and wp_blogs tables.
In summary, fixing the “Error establishing a connection to a WordPress DB” problem comes down to exploring the different approaches above. If, in the end, you find that adding the correct database information does not fix the problem, it is inevitably related to the web server. In this case, we recommend that you see your supplier. But, of course, you can also contact us; assisting would be a pleasure.