How to Fix the Error Establishing a Database Connection in WordPress

Spread the love
Error establishing a database connection

Does your WordPress website display the message “Error establishing a database connection”? Your WordPress website becoming inaccessible to users is a fatal issue.

When WordPress is unable to connect to the database, this error occurs. It can be challenging for beginners to troubleshoot your WordPress database connection because a variety of factors can affect it.

This blog will demonstrate how simple it is to fix the WordPress database connection error.

What Causes Error Establishing a Database Connection in WordPress?

Incorrect database information in your WordPress settings, a faulty database, or an unresponsive database server can all contribute to the “Error establishing a database connection” problem.

Error establishing a database connection

A database is a software component that makes it simple to store, arrange, and access data in other programs.

WordPress uses a database to store all of your content and other website data because it is a content management system. When someone visits your website, it then establishes a connection with the database.

The following information is necessary for WordPress to connect to the database.

  • Database username
  • Database password
  • Database server
  • Database name

Your WordPress configuration file, wp-config.php, contains a place to save this data.

If any of these are off, WordPress won’t be able to connect to your database server and you’ll get the “Problem establishing a database connection” issue.

The most frequent WordPress mistakes include this one. The database server may be offline or the database files may be faulty, in addition to invalid credentials, which can also cause this error to show up.

Let’s examine a step-by-step troubleshooting guide for resolving the WordPress error establishing database connection issue.

1. Check the credentials for your WordPress database.

WordPress database connection errors are most frequently a result of incorrect database credentials. This could be the most likely cause if you recently switched your WordPress site to a new host.

The wp-config.php file contains the credentials you use to access the WordPress database. Important WordPress settings, including database information, are contained in the WordPress configuration file.

The following lines in the wp-config.php file are what you need to check for.

// ** 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' );

You must verify that the database name, username, password, and database host are all accurate.

From the dashboard of your WordPress hosting account, you can verify this information. To access MySQL databases, simply log into your hosting account and select the database option.

In our screenshot, we’re utilizing the Realjossy Hosting dashboard, but the procedure is the same regardless of the hosting control panel.

MQL Database

This will direct you to your hosting dashboard’s database management page. You can get your database name and username from this point.

The database users and links to change user passwords can be found below that.

Once your database name, username, and password have been confirmed, you can modify them if necessary in your wp-config.php file.

Visit your website once again after that to check if the database connection error has been resolved.

If the mistake is still visible, then something else has to be wrong. For additional troubleshooting procedures, keep reading.

Also Read: Troubleshooting WordPress Errors for Beginners (Step by Step)

2. Check Your Database Host Details

Make sure you are using the correct database host information if you are certain that your database name, username, and password information is accurate.

The majority of WordPress hosting providers host your database on localhost. Yet, some managed WordPress hosting providers maintain databases on separate servers. In that situation, localhost won’t be listed as your database host information.

To confirm your database host details, get in touch with your WordPress hosting provider.

3. Restoring the WordPress Database

If you are experiencing a different issue on the wp-admin, such as “One or more database tables are unavailable. The database may need  to be repaired”, you must repair your database.

Add the next line to your wp-config.php file to do this. Ensure that you insert it right before “That’s all, finish editing!” Happy blogging, file wp-config.php.

define('WP_ALLOW_REPAIR', true);

Visit this page to view the settings after you’ve done that:

how to fix Error establishing a database connection

It should be noted that anyone can view the database repair page without having to log in. Make careful to remove this code from your wp-config.php after you’ve finished fixing and optimizing your database.

4. Check if your database server is unavailable.

If everything appears to be in order but WordPress is still unable to connect to the database, your MySQL server may be offline.

A server experiencing high traffic may be the cause of this. In essence, your host server is just unable to handle the traffic (especially when you are on shared hosting).

Your website will become extremely slow, and it can even produce an error for some users. So, the best course of action is to contact your hosting company via phone or live chat and inquire as to whether your MySQL server is responsive.

You can check the other websites that are hosted on the same server to see if your SQL server is down.

Go to your hosting panel, try to access phpMyAdmin, and connect the database if you don’t have any other websites hosted on the same account.

If you are able to connect, we must check the permissions of your database user. Paste the following code into a brand-new file called testconnection.php:

<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully';
mysqli_close($link);
?>

Be sure to change the password and username. This file is now available for web browser viewing and upload to your website.

If the script was able to connect, that suggests your user has the necessary permissions and something else is wrong.

Make sure everything in your wp-config file is right by returning to it (re-scan for typos).

Also Read: The Beginner’s Guide to WordPress 404 Error Page

Other Solutions That Have Worked for Users

You can attempt these extra actions if the troubleshooting support offered above did not resolve the database connection error on your website.

These techniques, according to what our users have reported, have assisted some users in fixing the database connection error on their websites.

1. Update the WordPress website’s url

Try using phpMyAdmin to modify the WordPress site’s URL. Simply choose your WordPress database in phpMyAdmin by going to the dashboard of your hosting account.

Phpmyadmin

Next, select SQL from the top menu and type the following MySQL query.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

In case you modified the WordPress table prefix, don’t forget to provide your own site URL and replace wp options with the name of your own table.

2. Restarting the Web Server

Users of virtual private servers (VPS), local servers, and dedicated servers might attempt resetting their machines.

This will restart your web server and database server, which may resolve any short-term issues that were the cause of the error.

3. Request support

You might need to get in touch with your web hosting company if all else fails. Any reliable WordPress hosting provider will assist you in identifying the issue, send you in the correct path, or even resolve it for free.

Get WordPress developers from Codeable who can assist you in resolving this problem for cost-effective prices. The best thing is that the Codeable staff has thoroughly vetted these developers, so you can trust them.

Leave a Reply

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