DED9

How to Fix the Database Connection Error in WordPress

This tutorial will tell you how to troubleshoot and fix database connection errors in WordPress. Therefore, if your site is unavailable and you are facing an error establishing a database connection, it may not be too much to worry about! By following this tutorial, you can find and fix the source of this problem.

The error in connecting to the database means that your site (your WordPress PHP filPHP does not have access to your site’s database. Yes, the meaning of this error is clear, but this very clear meaning can have various reasons. For example :

(You were surprised that the server problem was the last one? You are right! Because unfortunately, what is common among most users is that for every problem they encounter, first of all, they say that the problem must be from the server side, while it is not the case)

In the following, we will describe the steps for troubleshooting errors in communicating with the database in WordPress, but before going to the main story, make sure to create a complete backup of your site in this state. Anyway, there is no fault in working hard. And one more important thing, if you have a new backup of your website that was working without problems, you can easily restore that backup and relax. But if it is not possible for you to do such a task, you can follow the troubleshooting steps with us.

Disable plugins

First, let’s make it clear that we don’t mean that if you haven’t installed any new plugins, haven’t updated any plugins, and haven’t made any changes to your site with any plugins, then come and disable all plugins. . Of course, there must have been a change in your site related to plugins to come to this step.

For example, you may have recently installed a new plugin on your site, and after that,t you had a problem communicating with the database. For example, you may have changed the prefix of your tables or the access level of your files with the help of a security plugin, which has caused problems on your site.

Since any interference between plugins and applying any change on behalf of a plugin on the database can lead to WordPress not having access to the database of your site, at this stage, it is necessary to disable all the plugins on your site. . If your site is up again after disabling the plugins, you have found the problem. Now you have to reactivate the plugins one by one to see which one caused this problem and think of a solution for it. If the problem is not with the plugins, you should naturally go to the next steps.

You should note that if a plugin has made a change on your database that has caused the site to go down, deactivating the plugin will be ineffective,e and the problem should be solved in one of the following ways after troubleshooting in the next steps. In the following, we mention that it should be resolved.

Check database connection information.n

Enter your host and find your wp-config.php file in the main branch of the site (usually in public_html) and open it. In this file, there is a section where you can see the connection information to the database. Like the following code:

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘database_name_here’);

/** MySQL database username */
define(‘DB_USER’, ‘username_here’);

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

/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

Check the above information completely and ensure that the database name, username,e, and password match the above information. (For this, you can refer to the control panel of your host and see the specifications included in this section with what you see on your host, and most importantly, make sure that your database is available on your host)

In the picture above, in addition to making sure the name of the database and its user name,e, and its existence in the host, we also made sure that we assigned the correct user to the database and that this user has permission to access the database.

Here are some tips. Although your database password may seem reasonable to you and does not contain unauthorized characters, unfortunately, the presence of some characters in the database will lead to the inability to connect. (So ​​don’t overdo it in choosing strange passwords for the database). Sometimes it even happens that the passwords suggested by cPanel for the database do not work,e and you cannot connect to the database with them.

So if your password is too weird, make it a little simpler and stick to a combination of numbers, small and capital letters, and the @ symbol,l, for example! (It was bad advice, but it is true) To change the database password, it is enough to go to the MySQL Databases section of your host and find your database user at the bottom of the page in the Current Users section and click on the Change Password link in front of i,t and the password Change your database to match the information in the config file. Like the picture below:

Also, don’t forget to assign all access to the user you created. Not having enough access can be one of the reasons for your website’s problem. You can delete the user you have already created and create it again if needed. Remember that,t in this case, you have to assign the user to the database again.

On the other hand, sometimes all the information entered here may be correct. Even though this information was working correctly just a few hours ago, the connection with the database still cannot be established. The weird thing we’ve seen before is that sometimes changing localhost to 127.0.0.1 will fix your problem. It seems that the reason for this is that, at one moment,t WordPress was unable to access the MySQL service for any reason or this service was down,n, and this information was cached in the system. In this case, forcing WordPress to connect to the database at another address will solve your problem.

After confirming the above information, look for a line similar to the following in the wp-config.php file:

$table_prefix = ‘wp_’;

The wp_ expression in the code above must match the prefix of your database tables. So if you have changed the table prefix with a security plugin, replace wp_ with the correct prefix in the above code and save the changes.

OK, now refresh your website a few times. If your website were up, we would thank God. If not, it’s time to test the connection information to the database. There may have been a mistake in entering the information. To test the connection information to the database, create a file called dbtest.php in the main path of your site and put the following code in it:

<?php
$link = @mysqli_connect(‘localhost’, ‘database_username’, ‘database_password’);
if (!$link) {
die(‘Could not connect’);
}
echo ‘Connected successfully’;
$link=null;

Replace your_hostname, database_username, and database_password in the above code. Then call the file in your browser. (If you have placed the file in the main path of the site, its address follows the pattern in front http://domain.com/dbtest.php) In this case, if you receive the message Connected successfully, the connection information is correct, and the connection with the database is The correct form is done. Still, your connection information is incorrect if you receive the Could not connect message.

Modify the site address

If you have moved your website to a new address and forgot to replace the correct address in the WordPress settings, or for any other reason, there has been a change in the address of your site, resulting in a mismatch between the address stored in the database and the address from If you are calling the site, this issue can be the reason for the error of communicating with the database.

To fix this problem, refer to the wp-config.php file on your host. Look for the following lines in this file:

define(‘WP_HOME’,’http://your-site.com’);
define(‘WP_SITEURL’,’http://your-site.com’);

If you find these lines, ensure that the address entered in the http://your-site.com section matches your site’s address. But if you don’t find these two lines, add them to the wp-config.php file yourself, put your site’s correct address in it, and save the changes. Check your website again to see if the problem is fixed. If the problem persists, delete the inserted lines and continue troubleshooting.

Now go to the wp-includes folder on your host and find the functions.php file. Check and see if you can find the following two lines in it:

update_option( ‘siteurl’, ‘http://your-site.com’ );
update_option( ‘home’, ‘http://your-site.com’ );

If these two lines are present in the mentioned file, replace the address entered in them with the correct address of your site, and if these two lines do not exist, add them yourself with the correct addresses to the beginning of the file right after <?PHP and Save the changes. Please open the website and its management area and refresh the page several times. Do not forget that after doing this and testing whether your website is up or not, be sure to delete these codes from the mentioned file. These two lines of code should not remain in the functions.php file.

When entering addresses, pay attention to the fact that you enter the address with www or without www, as well as with http or with https, because any mistake in entering the address can lead to your website not loading and not fixing the problem.

Modify accesses or replace damaged files

If your problem is still not resolved, we can assume that there may be a problem with your WordPress core files. Here, we don’t want to go into the complexity of the accesses and their detailed explanation. Let’s say that you can check your hosting and make sure that the access level of your WordPress files is equal to 644 and the access level of your folders is equal to 755. (Usually, this access level will be correct, but the folder access may differ based on the hosting environment).

If everything is fine and the problem persists, you can re-download a version of WordPress and replace the WordPress core files with the recently downloaded version.

To do this, download a raw version of WordPress (compatible with your current version), extract it on your computer and delete the wp-content folder and the wp-config-sample.php file from it, and then Transfer the main WordPress files to your website host. Note that the wp-content folder and the wp-config.php file should not be overwritten and lost. Doing this can lead to the loss of your site information. So do this carefully.

Repairing the WordPress database

If, after doing all the steps above, your website is still not working properly? Maybe your database needs minor repairs! This issue is especially likely when you only have access to your site’s administration or user area (but not both). To do this, first, open your wp-config.php file for editing and then put the following code in it:

Contact your hosting support

When communicating with the support, please always remember that being a creditor and declaring that the problem is 100% from the host will not solve anything. It may not be true, and you may have missed something or not done a step correctly. Therefore, in this situation, you can ask for guidance from your hosting support and tell them what steps you have gone through to see how you can solve the problem that is still standing.

This is a good place to reiterate the importance of making regular backups of your site. It is natural that if you have an up-to-date backup of your site available, everything will be completely solvable without doing complex tasks, and you can re-establish your website in the shortest possible time.

We hope that such problems have not happened to you and your website and will not happen to you in the future. But we will be happy if you have faced this problem before, put your experiences in the comments section and tell us how you were able to solve it.

Your solution may be able to help others solve this problem.

Exit mobile version