Fix the Error Establishing a Database Connection in WordPress

Are you getting ‘Error establishing a database connection’ notice on your WordPress site? It is a fatal error that makes your WordPress site inaccessible to the users.

This error happens when WordPress can’t make a connection with the database. Various things can influence your WordPress database connection which makes it somewhat hard for beginners to troubleshoot.

In this article, we will tell you the best way to effortlessly fix the mistake setting up a database connection in WordPress.

Fixing the database connection error in WordPress
© Woorkup

What Causes is Error Establishing a Database Connection in WordPress?

The ‘Error establishing a database connection’ issue can be caused by incorrect database data in your WordPress settings, corrupt database, or an irresponsive database server.

Build a WordPress AJAX Form in 4 Easy Steps

Database connection mistake in WordPress

A database is a software that makes it simple to store, organize, and recover information into other software.

As a content management system, WordPress uses a database to store all your content and other site information. It at that point connection with the database each time someone visits your site.

WordPress needs the following information for connection with the database:

  • Database name, Username, Password
  • Database server

This data is stored in your WordPress configuration file called wp-config.php.

If any of these things are mis6, WordPress would neglect to connect with your database server, and you’ll see the ‘Mistake setting up a database connection’ error.

It is one of the most widely recognized WordPress mistakes. Aside from incorrect credentials, this error can also show up if the database server is down, or the database records are corrupt.

Let’s take a look at how to fix error building up database connection issue in WordPress with step by step instructions.

How to Add a Search Bar to WordPress Navigation Menu

1. Check Your WordPress Database Credentials

The most widely recognized purpose behind database connection error in WordPress is incorrect database credentials. If you have moved your WordPress site to another host, at that point this could be the in all likelihood reason.

Your WordPress database credentials are stored in the wp-config.php record. It is the WordPress configuration file that contains important WordPress settings including database data.

If you have not edited wp-config.php file previously,

You’ll be searching for the following lines in the wp-config.php file.

// ** 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 have to ensure that the information for the database name, username, password, and database have is right.

You can confirm this data from your WordPress hosting account dashboard. Just sign in to your hosting account and click on MySQL databases under the database section.

Database data

This will take you to the database of the main page in your hosting dashboard. From here, you can discover your database name and the username.

Best GoDaddy Alternatives for Cheaper and Reliable hosting

Database name and username

Underneath that, you will see the database user and link to change the user password.

Changing database client user password

When you have confirmed your database name, username, and password, you can change that data in your wp-config.php document if necessary.

From that point on, take a look at visiting your site again to check whether the database connection mistake has gone.

If you sti6 observe the error, at that point this implies something different isn’t right. Keep reading for all the more troubleshooting steps.

2. Repair WordPress Database

If you are getting an alternate mistake on the wp-administrator, for example, something like “at least one database tables are inaccessible. The database should be repaired”, at that point, you have to repair your database.

You can do this by including the following line in your wp-config.php record. Try to include it just before ‘That is all, stop editing! Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

When you have done that, you can see the settings by visiting this page:

Easy Ways to Add a Privacy Policy in WordPress

Repair WordPress database

Note: the user shouldn’t be signed in to get to the database repair page. When you are finished fixing and optimizing your database, make sure to remove this code from your wp-config.php.

3. Check if Your Database Server is Down

If everything is right, WordPress still can’t connect with the database, at that point, your database server (MySQL server) might be down.

This could occur because of heavy traffic on a server. Fundamentally, your host server just can’t deal with the load (particularly when you are on shared hosting).

Your site will get really slow and for certain users, it might even show the mistake. So the best thing you ought to do is to contact your hosting provider via a live chat or telephone and inquire as to whether your MySQL server is responsive.

If you have different sites running on a similar server, at that point you can check those sites to confirm that your SQL server is down.

Somehow you don’t have some other site on the same hosting account, then quickly go to your hosting dashboard and try to get to phpMyAdmin and connect the database.

Rules For Using WordPress Logo and Trademark (Explained)

Suppose you can connect, then we have to check if your database user has adequate authorization. Make another record called testconnection.php and paste the following code in it:

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

Make sure to replace the username and password. You would now be able to upload this document to your site and access it through the internet browser.

If the script connected effectively, then it implies that your users have sufficient permission, and there is something else that isn’t right.

Return to your wp-config document to ensure that everything is right (re-check for grammatical mistakes).

4. Check Your Database Host Information

If you are confident that your database name, username, and password data is right, at that point you might need to ensure that you are using the right database to have data.

Most WordPress hosting company use localhost as your database have. Some managed WordPress hosting company uses separate servers to have databases. All things considered, your database host information won’t be localhost.

You have to contact your WordPress hosting company to confirm your database host information.

Easy Tips to Get 100% in WordPress Site Health Check Score

Different Solutions That Have Worked for Users

If the above-mentioned troubleshooting steps failed to fix the database connection error on your site, then you may try these extra steps to troubleshoot it.

Reported by our users, these steps have helped a few users to settle their database connection errors on their sites.

1. Update WordPress Site URL

Try updating the WordPress site URL using phpMyAdmin. Essentially go to phpMyAdmin from your hosting account dashboard, and select your WordPress database.

Running SQL in phpMyAdmin

After that click on the SQL menu on the top and enter the following MySQL query.

Start a WordPress Blog the EASY WAY in Easy Steps

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

Remember to give your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

2. Request help

If everything fails, then you may need to contact your web hosting company. All popular WordPress hosting companies will assist you to troubleshoot the issue, point you the correct way, or even fix it for you.

You can also hire a WordPress developer that can help you with fixing this issue at sensible rates.

3. Rebooting Web Server

Clients on dedicated servers, nearby servers and virtual private servers (VPS) can try to reboot their servers.

This will restart your web and database server which may fix some temporary glitches causing the error.

How to Create Coupon Popup in WordPress

We hope this article helped you learn about Top best WordPress plugins for fast and secure website to use on your project. You may also want to see our list of other articles too. If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Image Source

Editorial Staff

Editorial Staff at BeFirstly is a team of content writer, Editors and SEO experts. Our staff helps you to make your website rank better in search engines like Google and Bing. Follow us for more 🎉