6 Ways to Fix Error Establishing a Database Connection

An error establishing a database connection in WordPress happens when the site can’t connect and recover the database and its information. Its common causes include error in the wp-config file, invalid credentials, or corrupted data.

This can result in a bad experience for the user by making the site inaccessible to them. Given that multiple factors can affect this connection, the WordPress error establishing a database connection can be a little problematic to troubleshoot for beginners.

But do not worry. We will show you how you can fix your connection step by step.

Reasons for the WordPress error establishing a database connection

Wordpress error establishing a database connection

It is very important to understand the problem before solving it. Here are the reasons for  WordPress error establishing a database connection can occur.

Before jumping in the possible cause first make sure of these thing, Sometime Big problems are caused by small ignorance.

  • You have the correct username and password.
  • You have typed the correct hostname.
  • The database server is running.

 

Error establishing a database connection

  1. Incorrect database login credentials (in the wp-config file): WordPress uses login information from your hosting provider. If you change the MySQL database password and username, the old ID (Credentials) in your configuration files will be invalid or unacceptable.
  2. Corrupted database: Plugins and users continuously delete and install so many moving parts with themes. These faulty plugins or incompatible themes can corrupt database tables and their content. This can be caused by a missing or a single corrupted table, or there might be chances that some information got deleted accidentally.
  3. Core file corruption: WordPress core files define how your site works and functions. Faulty code in plugins and themes can corrupt them, causing WordPress error establishing a database connection.
  4. Database server error: This is common with shared hosts as they use the same resources for many users on the same servers.
    Many things can be wrong on the web host’s side, like overloading the data from a traffic spike or being unresponsive from too many parallel connections.
    WordPress core files define how your site works and functions. Faulty code in plugins and themes can corrupt them, causing database failure.
  5. Spike in Traffic: Depending on your web host, your server might be unable to handle many concurrent database connections. An increase in traffic can sometimes lead to a temporary WordPress error establishing a database connection.
  6. Corrupt Files in WordPress Installation: This can caused by hackers.

How to troubleshoot “WordPress Error Establishing a Database Connection.”

Before troubleshooting the WordPress error establishing a database connection, you are advised to back up the WordPress site. Most of the ways of troubleshooting the sites involve manipulating information in your database, and you obviously don’t want to worsen the situation.

You should always back up before trying to fix things on your WordPress site, as it is said that prevention is better than cure. You can also use  WordPress backup plugins; there are several plugins available.

1. Check Your Database Login Credentials

  • Connect to your website via FTP/SFTP or use a file manager from your web hosting control panel.

File manager

  • Access the main directory of your WordPress installation and locate the “wp-config.php” file.
  • Open “wp-config.php” in a text editor.
  • Look for the following lines and ensure that the database credentials are correct:

1. Database Name

2. ySQL database username

3. MySQL database password

4. MySQL hostname (server)

Database

  • You can test these credentials by attempting to log in to your database via a tool like phpMyAdmin or MySQL command line interface with the same credentials.
  • If you made changes, save the “wp-config.php” file and re-upload it if necessary.

2. Check Database Server

  • Verify with your web hosting provider that the MySQL database server is functioning properly.
  • Many hosts provide status updates on their dashboards or support portals.
  • Use command-line tools to ping the database server (if you have SSH access) to ensure it is reachable.

Ping database

  • Log in to your hosting control panel and navigate to the database management section to ensure that the database server is running.

3. Fix Corrupted Files

  • Before making any changes, back up your website files and database.
  • Download the latest version of WordPress.
  • Download the files and use FTP/SFTP to upload the “wp-admin” and “wp-includes” folders, replacing the existing ones. 
  • Check if the file and  the folder permissions are set correctly (e.g., folders should be 755, files should be 644).

4. Repair The Database on WordPress

  • Open “wp-config.php” and add the following before the ‘/* That’s all, stop editing! Happy blogging. */ line:

Allow repair

  • Visit the repair page by navigating to http://yourdomain.com/wp-admin/maint/repair.php.
  • Follow the instructions to repair and optimize the database.

Allow repair

  • After repairing, remove the define(‘WP_ALLOW_REPAIR’, true); line from “wp-config.php”.
  • Log in to phpMyAdmin from your hosting control panel.
  • Select your database, check all tables, and choose “Repair table” from the “With selected” dropdown.

5. Restore Latest Backup

  • Log in to your hosting control panel or backup service to access your latest backups.
  • Locate the most recent database backup file and restore it using the backup tool or via phpMyAdmin by importing the backup file into your database.
  • If necessary, re-upload your website files from the backup.
  • After restoration, check if the website is functioning properly.

6. Update the WordPress Website URL

  • Log in to phpMyAdmin from your hosting control panel.
  • Select the database and find the “wp_options” table.
  • Locate the rows with “option name” “site url” and “home“.
  • Edit these rows to reflect the correct URL of your website.
  • Log in to your WordPress admin dashboard.
  • Go to Settings > General and update the WordPress Address (URL) and Site Address (URL) to the correct URL if needed.
  • Ensure the .”htaccess” file in the root directory contains the correct WordPress rewrite rules.

Additional Ways to Fix WordPress Error Establishing a Database Connection

All the methods mentioned above should be enough to resolve the issue you have, but just in case those are still not resolved, here are some additional ways to fix WordPress Error Establishing a Database Connection.

Contact the Support Team

If you’re having WordPress Error Establishing a Database Connection, reach out to your web hosting provider’s customer support. They can check your website’s backend to find out what’s causing the issue. 

Customer support availability differs from one provider to another. If your web host doesn’t offer support around the clock, you can look for solutions on places like the official WordPress forum.

Just be careful not to share any private information, like your database login details, on public forums.

Reboot the Web Server

If you’re hosting WordPress and MySQL on a dedicated server, reboot it to refresh settings and stop any background services causing connectivity wordpress error establishing a database connection.

Alternatively, use SSH to restart via the command “reboot.” If not on dedicated hosting, contact your WordPress hosting provider for assistance.

Conclusion

Dealing with a “WordPress Error Establishing a Database Connection” can be difficult for most people, but it will be convenient with a step-by-step guide. With these methods, you can thoroughly address the issue and effectively get your site back online. It is strongly advised and mentioned repeatedly that you should back up your file before initiating the troubleshooting. 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *