Have you ever encountered with a blank white screen page having a simple brisk message: “Error Establishing a Database Connection”?
If yes, you might have tried refreshing many times, but were unable to access the website.
It’s a fact that for each minute your website goes down, you lose the chance to generate leads, have conversions and enjoy sales.
No doubt, the situation is serious, but calm your nerves down and don’t panic! This guide will help you to better understand and learn some practical approach on fixing the error establishing a database connection in WordPress.
- The Reason for Occurrence of WordPress Database Error
- Verifying Error Establishing a Database Connection
- Check Database Login Credentials in wp-config
- Reset Username and Password
- Database Host Value
The Reason for Occurrence of WordPress Database Error
First, let’s check what this error message actually means.
MySQL is a language that is used by your database. Your website is dependent on a server-side language called PHP. Whenever someone enters your website URL into the browser, WordPress uses PHP to recover all the information related to the page from the database and sends it to the visitor’s browser.
You get the error establishing a database connection because this MySQL-PHP connection is cut-off for some reason, which needs to be investigated.
Check the following scenarios that may cause this error:
- Check if you have changed your login credentials (username and password). In case you haven’t, those might have been changed somehow.
- Did you install a poorly coded plugin? A poorly coded plugin might corrupt your database.
- Is your website experiencing a heavy traffic spike, which is beyond your server’s capacity?
- Check if your WordPress web hosting is at fault and the database is not functioning due to some problem at their end.
Prior to attempting to fix the error establishing a database connection in WordPress, I recommend you to take a backup! You can take the backup in the following ways:
- Download all web files from FTP (SFTP). This can be done by logging into your hosting account.
- Export MySQL database using the Export function.
- Alternatively, you can use the UpdraftPlus plugin to backup your data.
Verifying Error Establishing a Database Connection
Did you get the same error when you try to browse the website URL (front-end), and when you try to log in to /wp-admin/ (back-end)? If you are getting the error establishing a database connection, navigate to the next step.
But, if you get the below error when trying to login to /wp-admin/, it means your database has been corrupted:
“One or more database tables are unavailable. The database may need to be repaired”
To resolve this, go to your root WordPress file directory, locate your wp-config.php file. This will enable WordPress’ built-in repair feature.
Launch Filezilla —–> (WordPress install folder) —–> public_html
Right-click on the wp-config.php file and select a code editor, such as Sublime Text Editor to open the file.
Once the wp-config.php file is opened, add the below line at the bottom of the file:
Now go to:
(Replace “example.com” with your actual URL)
You will get the above-mentioned options displayed. Here select the “Repair and Optimize Database” option. Though it might take more time, it is a highly comprehensive attempt to resolve the issue.
To access this repair page there is no need to log in, so it creates a security issue. Instantly, after the repair and optimization have been done, remove the line of code you added to the wp-config.php file.
Check if this method fixed WordPress error establishing a database connection. If it has, congratulations! If not, keep on reading to find another fix for the error.
Check Database Login Credentials in wp-config
If the above solution hasn’t worked for you, then the possible cause of your problem is your database settings.
As said above, your database login settings might get disturbed, if you change other details of your user information or database. This happens if the system doesn’t properly update your wp-config.php file.
wp-config.php is the most important file of WordPress. It contains all the database information.
To open this file, go to:
Launch Filezilla —–>(WordPress install folder) —–> public_html
Right-click on the wp-config.php file and select a code editor such as Sublime to open the file.
After you have opened the wp-config.php file, you will see four key variables defined in the code, which are required for connecting to the database.
These are database name, database username, database password, and database host that is localhost in most of the cases (there may also be an IP address in place of localhost).
// ** 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' );
Make sure you note down these values. Now, login to your hosting account and select the “Applications” tab. Go into your WordPress application, from the “Access Details” tab, click “Launch Database Manager”.
You will see the table named wp_options, click on it. It will show your website name, URL and other settings. This shows whether you have the correct information in your wp-config.php file or not.
If the ‘siteurl’, and ‘home’ field information don’t match, then edit wp-config.php file and insert the same values as in the wp_options table.
Reset Username and Password
Check if the username and password in your wp-config.php file match those in phpMyAdmin. In this example, it’s “MySQL Access”.
If the username and password are different, open your wp-config.php file and edit it to include the values from the “MySQL Access” panel.
That’s it! If you would have tried this via cPanel, you’d have needed to take a lot of efforts.
Database Host Value
The last important value in WordPress wp-config.php file is DB_HOST. Most web hosting providers require this value to be localhost, which is the value in the file by default.
If you’ve performed all the above solutions, and still facing the error establishing a database connection in WordPress, then contact your web hosting provider and ask them to provide you with the DB_HOST value and enter it into the wp-config.php file.
Hope that you found this guide helpful and that you were able to solve the WordPress error establishing a database connection issue.
Have you found any other way to get rid of this problem? Let us know in the comments section below and mention what worked and what didn’t work for you. This will help others who are facing the same issue as you.