You open browser and type the URL of your WordPress-powered website. You’re not able to load your site instead see message stating-’Error establishing a database connection’.
Don’t panic, there are a plenty of ways to resolve this error and get back your site to normal. But before that, let’s take a look at causes could be responsible for this error.
- The login credentials of your database are inaccurate in your WP configuration file.
- The WordPress database tables are not working correctly.
- The database has crashed because your server is running out of memory.
Let’s go through these issues and see if they affect you, and how to resolve them using simple methods.
If you want a comprehensive information on how to fix the error establishing a database connection problem in WordPress, 000webhost.com has presented a detailed article on the same.
For the quick solution please continue with the article.
Prerequisites
This tutorial assumes the following.
- Your database runs on the same server as WordPress.
- You have your database credentials, including the database name, username, password. This is the information that a user creates during the setup WP install.
- You’re operating WP on a machine which you have sudo and command line access to.
Let’s begin with the fixing error
1. Inspect the Server Memory Resources-
It is the very first step you can take to debug this issue. Log into your server and check if the system and MySQL are running properly.
You can log into your server through SSH (Secure Shell) using the following command. Don’t forget to replace the highlighted text with your own user and server credentials:
Now you have successfully logged into your server. Next, check if your MySQL is running properly using the following command.
$ sudo netstat -plt
This command prints information on server’s networking system. In this case, you require the names of the ‘programs’ and ‘listening’ for connections under the TCP socket. See the output for a line displaying localhost: mysqld highlighted in red below:
If you’re receiving the same output, it means MySQL is running properly and listening for connections. However, if you don’t find MySQL listed there, you can try starting it. It looks like as below on most systems. However, some Linux distributors such as notably and CentOs use ‘mysqld’ in place of plain ‘mysql’ for the service name.
Start MySQL and if it needs verification, you can re-run the netstat command that we mentioned above. Now check the output of MySQL process again. Make sure both WP and MySQL have good amount of memory.
If you think MySQL has low memory, check it in its error logs using the following command:
This command will search through different log files, which are archived as compressed .tar.gz files. You have to search for the line that includes allocating memory, in any error.log* file in the /var/log/mysql/ directory.
See the Output below
If you’re viewing line like given above in the screenshot, it means your MySQL server is running out of memory. If there are many lines like this, you should migrate to a server having enough memory for your WP site.
And if you don’t see such output after running the zgerp command, it means you need to move to next step to fix ‘Error Establishing a Database Connection’.
2. Confirm Your DB Login Credentials
As said before, wrong DB login credentials could be a major reason for ‘Error Establishing a Database Connection’ in WordPress.
So let’s check with login credentials. If you have recently moved your WP site to some different hosting provider or server, you may need to update your DB connection details. These details are usually stored on the server in a PHP file known as wp-config.php.
Let’s see how can you locate your ‘wp-config.php’ file. Use the following command search this file.
This command is going to search every place from the root directory to find the ‘wp-config.php’ file. After finding the file, you need an editor tool to open it. We suggest using nano editor. It is capable to open the file with a complete configuration variable.
Your database connection information should look like below.
Now check if all 3 variables (DB name, username, and password) are accurate as per your record. If not correct them, save the file and exit.
Make sure now you have the same login credentials in database and WordPress configuration file.
If you are seeing the same credentials on both places you should be able to log in to your database. Reload your site to see if the error is fixed.
If the issue still persists try next step.
3. Repair Your WP Database
Luckily, WP offers an inbuilt feature to fix the database. By default it is disabled but since it has no access controls it can be a security issue. So first you have to enable this feature and run the repair, and then disable it.
Again you need to open your wp-config.php file and paste the following code in any blank line.
This line defines a variable that allows WordPress to enable repair feature. Save and close this file and move to your browser where you have to load the following address.
Note: You have to change the red portion with your own domain and IP address.
As soon as you load this URL in your browser you will find the following screen.
Next, hit the ‘Repair Database’ button, it will take you to a result page where you can view the repair process in real-time.
As soon as this process completes, again open up your wp-config.php file and remove the line you pasted before i.e.
Again try loading your site and see if the error is gone. If you’re still unable to resolve this error, probably you need to restore the database from a backup. For that, you may seek the help from your web hosting provider. Check if they have provided some tutorials for fixing the ‘error establishing a database connection’.