Loading...

Knowledge Base
Up to 75% off on hosting for WordPress websites and online stores

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?

Error establishing a database connection

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

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.

LeafDNS

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.

wp-config.php file

How to Find your wp-config.php File

  1. Log in to your cPanel.
  2. Within cPanel, look for the File Manager under the Files section.
  3. Navigate to your website's document root or directory.
  4. 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.
  5. A popup window will appear. Click Edit again to proceed.
  6. In the script, locate the line that reads "/** The name of the database for WordPress */" once you are in the file editor.
  7. 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.

cPanel - Database and Database User and 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.

MySQL Databases - Change Password

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.

MySQL Databases - 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:

Table Prefix

How it looks like in the phpMyAdmin database:

phpMyAdmin Table Prefix

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

Repair Database did not work. What else can I do?

If repairing the table options won't fix the issue, try the following steps.

  1. Create a new database username.
  2. Assign a new user to the database.
  3. Assign permissions to the user.
  4. 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.

Error Logs

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.

Did you find this article helpful?

 
* Your feedback is too short

Loading...