How to Fix the Error Establishing a Database Connection in WordPress

On your WordPress site, do you see the message “Error establishing a database connection“? It’s a deadly mistake that keeps people from accessing your WordPress site.

When WordPress can’t connect to the database, this problem message shows up. There are many things that can go wrong with your WordPress database link, which makes it hard for beginners to figure out what’s wrong.

I have faced this issue countless time in my life when working with a client’s websites or even sometime on my own projects as well.

But Today in this article, I will show you how to easily Fix the Error Establishing a Database Connection in WordPress.

What Causes Error Establishing a Database Connection in WordPress?

“Error establishing a database connection” can happen if your WordPress settings are wrong for the database, if the database is damaged, or if the database server is not responding.

Error Establishing a Database Connection

One type of software that makes it easy to keep, organize, and get data into other software is called a database.

WordPress is a content management system, which means that it stores all of your website’s text and other data in a database. After that, every time someone views your website, it connects to the database.

For WordPress to connect to the database, it needs the following information:

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

wp-config.php is the name of the file in your WordPress installation that holds this data.

You’ll see the “Error establishing a database connection” error if any of these things are wrong. This means that WordPress can’t connect to your database server.

This is a very regular mistake in WordPress. If you enter the wrong passwords, this error can also show up if the database server is down or the database files are damaged.

This error can also be caused by old plugins that aren’t working right. If you got this error after updating or installing a plugin, you should probably just turn it off or remove it.

There was an error setting up the database link. Let’s look at how to fix it in WordPress step by step.

Check Your WordPress Database Credentials

Most of the time, WordPress database connection errors are caused by wrong database passwords. This could be the most likely reason if you just moved your WordPress site to a new host.

This is where your WordPress database passwords are kept: in the wp-config.php file. The WordPress configuration file has important settings for WordPress, such as database details.

Inside the wp-config.php file, you need to find these lines.

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

It is important to check that the database name, username, password, and database host are all right.

You can check this information in the dashboard of your WordPress server account. To do this, just go to your server account and click on MySQL databases under “Databases.”

The picture shows the Bluehost dashboard, but the steps will be the same in other hosting control panels.

This link will take you to the page in your hosting dashboard where you can handle your databases. You can get the name of your database and the username from this page.

After that, you’ll see a list of database members and links to change their passwords.

You can change your database name, username, and password in your wp-config.php file if you need to after you’ve confirmed them.

After that, go back to your website and see if the database connection error is still there.

There must be something else wrong if you can still see the mistake. Read on for more steps to fix the problem.

and if you are struggling to get these details just connect with your hosting support live chat and they will help you out in most of these steps.

Check Your Database Host Information

If you are sure that the database name, username, and password are correct, you might want to check that the database host information is right as well.

Your database is hosted on localhost by most WordPress hosts. One managed WordPress hosting company, on the other hand, hosts databases on different servers. Then your database host information won’t be localhost.

To make sure your database host information is correct, you need to connect with your WordPress hosting company support.

Repair WordPress Database

If you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following line in your wp-config.php file. Make sure to add it just before ‘That’s all, stop editing! Happy blogging’ line in wp-config.php.

define('WP_ALLOW_REPAIR', true);

To see the changes after that, go to http://www.yoursite.com/wp-admin/maint/repair.php for more information.

Note: To get to the database repair page, the user does not need to be logged in. Make sure to remove this code from your wp-config.php after you have finished repairing and optimizing your database.

Check if Your Database Server is Down

Your database server (MySQL server) may be down if everything else checks out fine but WordPress still can’t connect to the database.

Heavy traffic on a server could cause this to happen. The main reason is that your host server can’t handle it (especially if you’re on shared hosting).

Your site will load very slowly, and for some people, it may even display an error message. You should call or live chat support with your hosting company and ask them if your MySQL server is fast.

Other websites that are on the same server as your SQL server can be checked to make sure that your SQL server is not down.

If there isn’t another site on the same hosting account, all you have to do is go to your hosting dashboard, try to get into phpMyAdmin, and connect the database.

You need to check to see if your database user has enough permissions if you can connect. Copy and put the following code into a 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);
?>

Make sure the password and login are changed. Now you can put this file on your website and use an online browser to get to it.

The script should be able to connect if it did, which means the user has enough permissions and something else is wrong.

Check your wp-config file again for typos and make sure that everything is correct.

Other Solutions From My Personal Experiences

If the fixing steps above didn’t work to fix the database connect error on your website, you may try these extra steps.

Some of these steps have helped me fix the database connection error on my clients websites or even on some of my website these suggestions are form my personal experience working as a WordPress developer.

1. Update WordPress Site URL

Use phpMyAdmin to change the WordPress site’s URL. To get to your WordPress database, just go to phpMyAdmin from the homepage of your hosting account.

Then, go to the top page and click on SQL. Then, type in the following MySQL query.

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

Change wp_options to your own table name because you may have changed the WordPress table title.

Also, don’t forget to give your site’s URL.

2. Rebooting Web Server

Restarting dedicated servers, local servers, and virtual private servers (VPS) might help users who are having trouble.

By restarting your web and database servers, you may be able to fix any short-term problems that were causing the mistake.

3. Ask for help

You may need to contact to your site hosting company support team if nothing else works. A good WordPress hosting company will either help you figure out what’s wrong, point you in the right way, or even fix it for you.

You can also use a service like Seahawk to hire WordPress Developers like me who will help you fix this problem for you.

I hope this article helped you fix the problem you were having connecting to a database in WordPress.

1 Comments

  1. Brian Matters February 4, 2024 at 7:57 am

    I am also facing same problem on my website….

    Reply

Leave A Comment

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