Is your WordPress website displaying the ‘Error establishing a database connection message? It is a fatal mistake that prevents users from accessing your WordPress website.

When WordPress is unable to connect to the database, this error occurs. Various factors might disrupt your WordPress database connection, making it challenging for beginners to resolve.

In this article, we’ll teach you how to quickly fix the issue when attempting to connect to a database in WordPress.

What Causes is WordPress Error Establishing a Database Connection?

Incorrect database information in your WordPress settings, a faulty database, or an unresponsive database server can all cause the ‘Error establishing a database connection error.

A database is a piece of software that allows you to easily store, organize, and retrieve data into other parts of the software.

WordPress stores your website’s content and data in a database as a content management system. Each time someone visits your website, it connects to the database.

WordPress needs the following information to connect to the database:

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

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

If any of these elements is incorrect, WordPress cannot connect to your database server and will display the ‘Message establishing a database connection error.

It’s one of the most prevalent WordPress mistakes. In addition to invalid credentials, this Error can occur if the database server is unavailable or the database files are faulty.

Let’s look at how to troubleshoot the Error by establishing a database connection in WordPress.

1. Verify the WordPress Database Credentials

Incorrect database credentials are WordPress’s most prevalent cause of database connection errors. This could likely be why you recently switched your WordPress site to a new host.

Your WordPress database credentials are stored in the wp-config.php file. The WordPress configuration file provides critical WordPress settings, including database information.

If you have not edited the wp-config.php file before, then look at our guide on how to edit the wp-config.php file in WordPress.

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

  1. // ** MySQL settings – You can get this info from your web host ** //

  2. /** The name of the database for WordPress */

  3. define( ‘DB_NAME’, ‘database_name_here’ );

  4. /** MySQL database username */

  5. define( ‘DB_USER’, ‘username_here’ );

  6. /** MySQL database password */

  7. define( ‘DB_PASSWORD’, ‘password_here’ );

  8. /** MySQL hostname */

  9. define( ‘DB_HOST’, ‘localhost’ );

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

You may confirm this information from the dashboard of your WordPress hosting account. Log in to your hosting account and navigate to the database section, where you will find MySQL databases.

Once you have confirmed your database name, username, and password, you can change that information in your wp-config.php file if needed.

Afterward, return to your website to see if the database connection error has been resolved.

If the Error can still be seen, it suggests something else is incorrect. Continue reading for additional troubleshooting tips.

Read more on: Image SEO, HTTP Status Codes Cheat, Black Hat SEO, Website Speed Optimization

2. Verify the Database Host Information

If you are confident that your database name, username, and password are accurate, you should double-check that you are using the correct database host information.

The majority of WordPress hosting providers use localhost as your database host. However, some managed WordPress hosting firms host databases on separate servers. Your database host information will not be localhost in that scenario.

To confirm your database host details, contact your WordPress hosting company.

3. Restore the WordPress database

If you get a different error message in the wp-admin, such as “One or more database tables are inaccessible. If the database needs to be repaired,” you must repair it.

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

  1. define(‘WP_ALLOW_REPAIR’, true);

repairwpdb

After that, go to this page to examine the settings: http://www.yoursite.com/wp-admin/maint/repair.php.

It should be noted that the user does not have to be logged in to access the database repair page. Once you are done repairing and optimizing your database, remove this code from your wp-config.php.

4. Check if Your Database Server is Down

If everything seems correct and WordPress cannot connect to the database, your database server (MySQL server) may be down.

This could happen due to heavy traffic on a server. Your host server cannot handle the load (especially when you are on shared hosting).

Your site will get slow, and for some users, it may even output the Error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

If you have other websites running on the same server, you can check those sites to confirm that your SQL server is down.

If you do not have any other site on the same hosting account, then go to your hosting dashboard and try to access phpMyAdmin and connect to the database.

We need to verify if your database user has sufficient permission so you can connect. Create a new file called testconnection.php and paste the following code in it:

  1. <?php

  2. $link = mysqli_connect(‘localhost’, ‘username’, ‘password’);

  3. if (!$link) {

  4. die(‘Could not connect: ‘ . mysqli_error());

  5. }

  6. echo ‘Connected successfully’;

  7. mysqli_close($link);

  8. ?>

Replace the username and password if necessary. This file can now be uploaded to your website and accessed via a web browser.

If the script is connected successfully, it suggests your user has adequate permissions and something else is wrong.

Return to your wp-config file and double-check that everything inside is right (re-scan for typos).

Other Solutions That Have Proven Effective for Users

If the above troubleshooting procedures did not resolve the database connection error on your website, you might try these different actions.

As reported by our users, these techniques have assisted some users in resolving the database connection error on their websites.

1. Change the URL of your WordPress site.

Using phpMyAdmin, try changing the URL of your WordPress site. Go to your hosting account’s dashboard and choose your WordPress database.

Then, pick the SQL menu on the top and input the following MySQL query.

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

If the WordPress table prefix has changed, remember to supply your site URL and update wp options to your table name.

2. Restarting the Web Server

Users with dedicated servers, local servers, and virtual private servers (VPS) can restart their machines.

This will restart your web and database servers, which may resolve any temporary issues causing the Error.

3. Seek assistance

You may need to contact your web hosting provider if all else fails. All reputable WordPress hosting companies will assist you in troubleshooting the issue, directing you in the proper direction, or even fixing it for you.

You can also engage WordPress developers from Codeable to assist you in resolving this issue at a fair cost. The best thing is that the Codeable team has thoroughly vetted these developers so that you can be confident in their abilities.

We hope this information helped you resolve the database connection error in WordPress. You may also be interested in our WordPress troubleshooting guide, which contains information on resolving WordPress difficulties.

You May also like sitescorechecker, Pro Site Ranker

Last Updated on July 19, 2022 by Alex Burma