You can get the “Error establishing a database connection” message if the WordPress website is unable to communicate with the database. This problem usually occurs, if WordPress is unable to access the database using the PHP commands.
Plenty of reasons are there for the occurrence of this issue. Some of the most common ones are if WordPress files have got corrupted somehow, then you might face this problem. Also, if the login credentials are incorrectly set, then there is a chance for you to get this error even.
Further, if the database is corrupted, then this issue might crop up. Moreover, if the database server is not working properly, then also this problem might show up.
How to resolve error establishing a Database Connection?
Here, you will get a multitude of methods that will help you to fix this error as soon as possible.
Inspect whether the Database is Corrupted
In order to connect a database to the WordPress website, you will have to correctly tweak ceratin settings, otherwise, this error will come up. Now, if you encounter this problem if the database gets corrupted, then follow the steps below.
- First, go to the WordPress website and then check the ‘Dashboard’ section in it. To see that section type the following URL – “Your website.com/wp-admin”.
- If you get this error then skip to the next step, otherwise, check the suggestions that WordPress is providing.
- Among the suggestions, if you get the following message ‘One or more database tables are unavailable […]’. It means that you will have to repair the database.
- To repair the database, follow the substeps below.
- Through FTP connect to your WordPress website once again.
- Next, log in to the WordPress and go to the root directory. After that, from that directory, locate the ‘wp-config.php’ file and download the same to the desktop.
- Now, open the ‘wp-config.php’ file in the ‘Notepad’ and then add the following line at the bottom but before the text ‘That’s all, stop editing’ and save it- “define(‘WP_ALLOW_REPAIR’, true);”
- Once everything is complete, next, upload the ‘wp-config.php’ file back to your WordPress website. This action will overwrite the previous version of the ‘wp-config.php’ file with the new one.
- Now, visit the following page – “Name of your website.com/wp-admin/maint/repair.php”.
- Inside that page, there will be two buttons. Out of those two, choose the ‘Repair and Optimize Database’ button. This will start the automatic repair process for the database.
- Wait for the automatic repair process to complete. Once it is over, go to your website and see if this problem is still there or not.
- Finally, open the ‘wp-config.php’ file and delete the last line that was added earlier and re-upload the file into your website.
Inspect the Login Credentials of the Database Connection
Sometimes, when the login credentials of the database connection get corrupted, if you have made any changes to the site files, then you might encounter this problem. The most feasible answer to resolve this problem is to check the login credentials.
- First, visit your WordPress website and go to the ‘wp-config.php’ file.
- Inside that file take a look at these four lines – “define(‘DB_NAME’, ‘some_db’)”, “define (‘DB_USER’, ‘some_username’)”, “define (‘DB_PASSWORD’, ‘some_pass’)”, “define(‘DB_HOST’, ‘localhost’)”
- Now, contact the web host support and check with them if the information within the four code above is correct or not.
- If the login information is not correct, then ask the web host support to provide the new credentials.
- After that, copy the new login credentials and paste them on the ‘wp-config.php’ file. Be sure to put the new login credentials in the right place.
- Now, upload the ‘wp-config.php’ file once again using the FTP and see if this error is there or not.
Check if the Theme files or Plugins are Corrupted
If the plugins or the theme files have been corrupted somehow, then you can get this problem. The most correct solution to resolve this problem is to check if any theme files or plugins are corrupted or not. How can you do it is as follows.
- First, visit your WordPress website via the FTP account.
- Now, navigate to the root directory of WordPress. The name of this directory is usually “public_html” or “www”
- Next, from the root folder, locate and double-click on the sub-folder “wp-content”.
- Inside that folder, find the “plugins” sub-folder and change the name of it to something different.
- Now, move back to the website and check if this error comes back or not.
- If the error is still there, then go back to the “plugin” folder and then alter the names of all the plugins one after another and see if this issue is there or not.
- If the “plugins” folder and all the plugins in it do not trigger this issue, then go to the “themes” folder and rename it.
Default WordPress File Restoration Process
If not a single method above is unable to solve this problem, then as a last resort all you can do is restore the WordPress files to the default. In order to do it, follow the steps below.
- First, go to the official WordPress website and download the newest package to the desktop.
- Next, unpack the contents within the package.
- After that, browse the unpack archive and then delete the “wp-content” directory.
- Now, connect to your WordPress website using FTP and then navigate to the root directory. At this time, upload the new WordPress files by overwriting the old files.
- Wait for the upload process to complete.
- Once the upload process is over, re-visit your website and check if this issue is present or not.