How to Troubleshoot a Database Connection Error
It is common for WordPress websites to encounter database connection errors, which may seem overwhelming at first. This article provides an overview of this error and how to troubleshoot database connection.
I am getting the "Error establishing a database connection" page. What is it?
A database is software that makes it easy to store, organize, and retrieve data into other software. As a content management system (CMS), WordPress uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.
The "Error establishing a database connection" is common among WordPress websites. It may be quite scary since it hardly displays any details. However, it is quite straightforward to understand. Every time a page is loaded on your WordPress site, it needs to access the database to retrieve the data required to display it. Simply put, if your website fails to connect to its database, it will not function properly.
This issue can be caused by the following:
- Incorrect database information in your WordPress settings
- Corrupt database
- Irresponsive database server
For more information on how to locate your WordPress database, please visit the How To Find Your WordPress Database article.
Here are some of the common causes of database connection errors and how to fix them.
- How to Fix the Database Connection Error in WordPress
- Make sure your domain is connected to the correct server where your database is
- Check if your database connection information is correct
- Check if your database name and database user are correct
- Check if your database password matches the value of 'DB_PASSWORD' in your wp-config.php file
- Check if your database user has the correct privileges
- Check if your database's table prefix matches what's in your wp-config.php file
- Try "repairing" your database
- Check the error logs in the website's directory or document root
- Summary
How to Fix the Database Connection Error in WordPress
To resolve the database connection error in WordPress, please follow the steps below.
Make sure your domain is connected to the correct server where your database is
You may use DNS lookup tools available online, such as LeafDNS, to check your DNS records.
Check if your database connection credentials are correct
You may want to check if your website is using the right database connection information, such as database name, username, and password, within your website's wp-config.php file. The connection will fail or terminate if the database credentials are missing, incorrect, or altered.
How to Find your wp-config.php File
- Log in to your cPanel.
- Within cPanel, look for the File Manager under the Files section.
- Navigate to your website's document root or directory.
- Within the directory, right-click on the wp-config.php file and select Edit from the list of options. You can also click the Edit icon in the toolbar.
- A popup window will appear. Click Edit again to proceed.
- In the script, locate the line that reads "/** The name of the database for WordPress */" once you are in the file editor.
- You should see the current database details used by your WordPress website.
Check if your database name and database user are correct
Confirm the database name and database user you have in your wp-config.php file exist and are correctly spelled within the cPanel's MySQL® Databases section. Please follow the steps in the How to Create and Delete MySQL Databases and Users article to confirm the existence of your database and database user.
Check if your database password matches the value of 'DB_PASSWORD' in your wp-config.php file
Within the cPanel's MySQL® Databases section, you can change your database password and update the 'DB_PASSWORD' in your wp-config.php file with the new password. Please refer to the How To Reset MySQL Database User & Password article for more information.
Check if your database user has the correct privileges
Check to see that your database has the correct user privileges within the cPanel's MySQL® Databases section. Simply put a checkmark on ALL PRIVILEGES, then click on the Make Changes button.
For more information, please visit the How to Create and Delete MySQL Databases and Users article to check your user privileges.
Check if your database's table prefix matches what's in your wp-config.php file
Check your wp-config.php file again and look for the line with "$table_prefix." The value should match your database table prefix.
The wp-config.php file shows:
How it looks like in the phpMyAdmin database:
Try "repairing" your database
You may want to try using the MySQL Databases' Repair Database functionality.
Within cPanel's MySQL® Databases section, select your database name from the dropdown menu under Modify Databases, then click Repair Database.
Repair Database did not work. What else can I do?
If repairing the table options won't fix the issue, try the following steps.
- Create a new database username.
- Assign a new user to the database.
- Assign permissions to the user.
- Copy the wp-config.php file for backup purposes and update the connection string in the new wp-config.php file with the new database user information.
Please check out the following support article for more information.
Check the error logs in the website's directory or document root
A file named 'error_log' provides detailed information on your website. This file shows exactly what scripts are causing the error messages. For example, this will let you know if the site's issue is caused by a corrupt file, a plugin, or a theme.
Within your document root, look for a file named error_log.
If there are no error logs, you might need to enable the WP_Debug in the wp-config.php file. For instructions, please check out the article on How to Enable WP_Debug.
Summary
The WordPress database connection error is a common issue that prevents your website from accesing its data storage, causing maulfunction.
To resolve the database connection error, you can:
- Ensure your database name, username, password, and server address (usually "localhost") are correct in the wp-config.php file.
- Make sure your database user has the necessary privileges to access the database.
- Your web hosting provider's cPanel might have a "Repair Database" function to fix corruption.
For advanced users, enable debug mode for more detailed error messages to pinpoint the exact cause (corrupt plugin, theme, etc.).
If you need further assistance, feel free to contact us via Chat or Phone:
- Chat Support - While on our website, you should see a CHAT bubble in the bottom right-hand corner of the page. Click anywhere on the bubble to begin a chat session.
- Phone Support -
- US: 888-401-4678
- International: +1 801-765-9400
You may also refer to our Knowledge Base articles to help answer common questions and guide you through various setup, configuration, and troubleshooting steps.