First, make sure that you are getting the same error both on the front end of the site and on the back end of the site. The front end of the site is what the user interacts with (and the back end of the site is what the developer/admin interacts with.

If the error is not showing, or different on the back end of the site (wp-admin) then you may only need to repair your database. Follow the instructions in Step 1 to repair the database. If the error is the same on both the front end and back end of the site, then proceed to Step 2.

Step 1: First, locate the wp-config file
This file is found by accessing your website’s files on your hosting servers. Usually, this is done by either logging into your Control Panel (cPanel) from your hosting provider, then using the File Manager tool, or accessing the server files directly via an FTP (File Transfer Protocol) Client.

An FTP Client is an app you install on your computer to upload, download and manage the files on your server. A commonly used free FTP client is FileZilla.

Once you have accessed your site’s files, locate the file named wp-config.php. This should be in the “root” or “parent” folder where your website resides. (it’s in the main folder, not a subfolder) You may have to scroll down through the files to find it. (If you see folders like wp-admin, wp-content, etc. then you’re in the right place, just scroll down).

Once you find the wp-config.php file, it’s wise to make a backup copy of the file. You can either make the copy on the server or on your local computer. Once you’ve created the backup, then edit the file. The File Manager tool in your Control Panel has a built-in editor, but if you’re using an FTP client, you must first download the file to your computer and open the downloaded file using a text editor, such as Notepad++. Once the file is open in the text editor, scroll down to the very bottom of the text until you see ‘That’s all, stop editing! Happy blogging’. Start a new line after this line and type or copy the following text:

Once you find the wp-config.php file, it’s wise to make a backup copy of the file. You can either make the copy on the server or on your local computer. Once you’ve created the backup, then edit the file. The File Manager tool in your Control Panel has a built-in editor, but if you’re using an FTP client, you must first download the file to your computer and open the downloaded file using a text editor, such as Notepad++. Once the file is open in the text editor, scroll down to the very bottom of the text until you see ‘That’s all, stop editing! Happy blogging’. Start a new line after this line and type or copy the following text:
define(‘WP_ALLOW_REPAIR’, true);
Save the file and then if necessary, upload it using the FTP client. Overwrite the already existing wp-config file. (As long as you have your backup copy saved) This will allow WordPress to attempt to repair corrupted files within the database. Just put the domain name of your website where “yoursite” is in this example. Then click Repair and Optimize Database. Once this is done, check to see if the error is gone. If it is still not working, then please proceed to Step 2.

Note: Once this has been completed, remove the command ‘define(‘WP_ALLOW_REPAIR’, true);’ from your wp-config.php file. Then save it and upload it again.

Step 2: Check Your Database Credentials in Your Config File
You can check the information located in your WP-Config.php file. (See step 1 for how to access your wp-config file) You need to verify that the database name, database username, database password, and hostname is correct. Not all hosting providers use the localhost for the DB_Host value. If you aren’t sure what these credentials should be, you should check with your web hosting provider to ensure that you have the correct information. In the wp-config.php file it will look something like this:

define(‘DB_NAME’, ‘database-name’);
define(‘DB_USER’, ‘database-username’);
define(‘DB_PASSWORD’, ‘database-password’);
define(‘DB_HOST’, ‘localhost’);

Sometimes these values can be changed by a malware infection, or to an incorrectly executed site migration, or perhaps by a well-meaning but inexperienced web developer. Once you have double-checked that these values are correct, you will need to save the wp-config.php file (and reupload it to the server again if you’re using an FTP client). Again, you should always keep a backup copy of any file you change, before overwriting the existing file.

Once the file has been uploaded, check the connection again by refreshing your website in your browser. If the error “Error Establishing a Database Connection” still displays, move on to Step 3.

Step 3: Replace localhost with the IP Address
This issue is more common when running WordPress on a local server (like your own computer). The IP will vary by hosting providers, just like the localhost does. Make sure you check with your hosting provider to ensure that you have the correct IP. Once you have the correct IP, change the data ‘localhost’ in the example above to the servers IP address. Once you have updated this information, save the file and upload it to via the FTP client, overwriting the existing file.

If You’re Still Seeing “Error Establishing a Database Connection”

If after following these steps you are still receiving the error, then there may be something wrong on the server end. Sometimes if a WordPress site becomes overloaded with traffic, it can cause MySQL to shut down, causing this error to show (Error Establishing a Database Connection). Usually, it reboots right away, and the site works again after a minute or so, but sometimes it will need to be manually rebooting by your hosting provider. Does this seem like too much of a headache for you? If you need to Hire WordPress Developers or WordPress development agency, You can contact us for a good WordPress development company or WordPress developer for your help.

Greeting,
Rachel Gomez