How to Troubleshoot a WordPress Error Connecting to a Database

You get up one morning, make a cup of coffee, and settle down at your desk. Surprisingly, you come across a blank white screen with the following message: “Error Establishing a Database Connection.”

You can’t view the webpage no matter how many times you refresh it.

Unfortunately, every minute your website is down, you’re losing the opportunity to create leads, convert visitors, and make sales.

The gravity of the issue is grave, but don’t lose your cool! Continue reading this tutorial to have a deeper understanding of the issue establishing a database connection in WordPress and to learn some practical solutions.

Contents

What Causes a WordPress Database Error?

First, let’s look at the meaning of this error message.

MySQL is the language that your database is written in. PHP is a server-side programming language that powers your website. When a visitor types your website’s URL into their browser, WordPress utilises PHP to retrieve all of the page’s information from the database and transmit it to the visitor’s browser.

The error establishing a database connection that you’re seeing is caused by a broken MySQL-PHP connection, which we need to explore.

Any of the following scenarios may cause this error:

  • It’s possible that your login credentials (username and password) have changed. This may have happened even if you didn’t make the adjustment yourself.
  • If you install a poorly coded plugin, your database may corrupt.
  • Your website may be experiencing a significant traffic increase that exceeds the capacity of your server.
  • Your WordPress web hosting may be at fault and the database is not functioning due to some problem at their end.

Before any attempt to fix the error establishing a database connection in WordPress, I advise you to take a backup! You have the following options to make a backup:

  • Download all web files from FTP (SFTP). To do this, log into your hosting account 
  • Export MySQL database through the Export function.
  • Alternatively, you can use UpdraftPlus to backup your data.

Determine the Cause of the Database Connection Error

Are you getting the same issue when you try to login to /wp-admin/ (back-end) and when you try to enter the website URL (front-end)? Continue to the following step if the error remains the same, i.e., error establishing a database connection.

Your database has been corrupted if you get the following error when trying to login to /wp-admin/:

“At least one database table is unavailable.” It’s possible that the database will need to be repaired.”

You must look for the wp-config.php file in your WordPress root directory. This is required in order to use WordPress’ built-in repair tool.

Launch Filezilla —–> (WordPress install folder) —–> public_html

To open the wp-config.php file, right-click it and choose a code editor, such as Sublime Text Editor.

After you’ve opened the wp-config.php file, add the following line at the bottom of the file:

define('WP_ALLOW_REPAIR', true);

Now navigate to:

yourwebsite.com/wp-admin/maint/repair.php

(Replace “yourwebsite.com” with your actual URL)

You’ll see the options listed above. We recommend that you select “Repair and Optimize Database.” Although it takes longer, it is a more thorough attempt to resolve the problem.

Because this repair page can be viewed without logging in, it poses a security risk. Remove the line of code you inserted to the wp-config.php file as soon as the repair and optimization are complete.

In wp-config, look at the Database Login Credentials.

If the approach indicated in the preceding paragraphs did not solve your problem, your database settings could be to blame.

If you update other features of your user information or database, your database login settings may go wild, as previously mentioned. This occurs when the system fails to update the wp-config.php file.

The most important file in WordPress is wp-config.php. It holds all of the database’s data.

To view or print this file, go to:
Launch Filezilla —–>(WordPress install folder) —–> public_html

To access the wp-config.php file, right-click it and choose a code editor like Sublime.

When you open the wp-config.php file, you’ll notice four important variables defined in the code that are required to connect to the database.

Database name, database username, database password, and database host, which in most situations is localhost (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' );

Note down these values. Now, login to your hosting,and select the “Applications” tab. Get into your WordPress application, from the “Access Details” tab, click “Launch Database Manager”.

Click the table named wp_options. It should reveal your website name, URL and other settings. This reveals whether you have the correct information in your wp-config.php file or not.

If the ‘siteurl’, and ‘home’ field information is different, then edit wp-config.php file and insert the same values as in the wp_options table.

Username and Password Reset

The username and password in your wp-config.php file must match the ones in phpMyAdmin. It’s “MySQL Access” in our situation.

That’s it! If you were attempting to do this via cPanel, you’d have to put in a lot more effort.

Database Host Value

DB HOST is the fourth important value in the WordPress wp-config.php file. Most web hosting companies require this value to be localhost, which is the default value in the file.

If you’ve followed all of the above instructions but still can’t connect to your database in WordPress, check out this official list of DB HOST settings for various web hosting companies. Otherwise, you can contact your web hosting provider and request the DB HOST value, which you can then insert in the wp-config.php file.

We hope you found this advice to be useful and that you were able to resolve the WordPress database connection error.

Frequently Asked Quetions

  1. What is a WordPress database connection error?
    In a nutshell, this issue happens when WordPress is unable to establish a database connection. And the cause of this handicap can differ. This could be due to the following reasons:

Your login and password are either incorrect or have been changed.
The database server that you’re using isn’t responding.
Your database has been tampered with.
The majority of the time, this error is caused by a server error, but there could be other causes as well.

Leave a Reply