Enjoy Up To
80% OFF

How to Fix the WordPress Error Establishing a Database Connection

How to Fix the WordPress Error Establishing a Database Connection

Is your WordPress website displaying an ‘Error establishing a database connection’ message? It’s a fatal error that prevents users from accessing your WordPress site.

When WordPress is unable to connect to the database, this error occurs. A variety of factors might affect your WordPress database connection, making it tough to troubleshoot for newcomers.

In this article, we’ll teach you how to easily fix the error on WordPress by setting up a database connection.

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 cause the ‘Error establishing a database connection’ error.

wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

A database is software that makes it easy to store, organize, and retrieve data into other software.

WordPress uses a database to store all of your content and other website data as a content management system. Each time someone visits your website, it connects to the database.

WordPress needs the following information for connecting to the database:

  1. Database name
  2. Database username
  3. Database password
  4. Database Host
  5. Database Prefix
wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

This data is saved in the wp-config.php file in your WordPress installation.

If any of these items are incorrect, WordPress will be unable to connect to your database server, and you will receive an error message stating “Error establishing a database connection.”

One of the most common WordPress mistakes is this. This error can also display if the database server is offline or the database files are faulty, in addition to invalid credentials.

With step-by-step debugging, let’s look at how to fix difficulties establishing database connection issues with WordPress.

1. Check Your WordPress Database Credentials

Incorrect database credentials are the most prevalent cause of database connection failures in WordPress. This is most likely the case if you have recently relocated your WordPress site to a new host.

The wp-config.php file stores your WordPress database credentials. It’s the WordPress configuration file, and it’s where you’ll find all of your critical WordPress settings, including database information.

If you’ve never altered a wp-config.php file before, check out our tutorial on how to modify a WordPress wp-config.php file.

The following lines should be found 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 need to make sure that the information for the database name, username, password, and database host is correct.

You can confirm this information from your WordPress hosting account dashboard. Simply log in to your hosting account and click on MySQL databases under the database section.

wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

This will take you to the database management page in your hosting dashboard. From here, you can find out your database name and username.

wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

Below that you will find the database users and links to change the user password.

wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

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

After that, try visiting your website again to see if the database connection error has gone.

If you can still see the error, then this means that something else is wrong. Continue reading for more troubleshooting steps.

2. Check Your Database Host Information

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

Localhost is the database host used by the majority of WordPress hosting businesses. Some managed WordPress hosting companies, on the other hand, host databases on different servers. Your database host information will not be localhost in that scenario.

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

3. Repair WordPress Database

If you see a different issue in wp-admin, such as “One or more database tables are unavailable,” here is what you should do. If you receive the message “The database may need to be repaired,” you must repair your database.

Add the following line to your wp-config.php file to do this. Make sure it’s right before ‘That’s it, no more editing!’ wp-config.php

define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

4. Check if Your Database Server is Down

If everything appears to be in order but WordPress still can’t connect to the database, it’s possible that your database server (MySQL server) is down.

This could occur as a result of high traffic on a server. Basically, the demand on your host server is too much for it to handle (especially when you are on shared hosting).

Your site will become extremely slow, and some users may experience an error. As a result, the best thing you can do is call or chat with your hosting provider and ask if your MySQL server is responsive.

If you have other websites on the same host as your SQL server, you can check those sites to see if your SQL server is down.

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

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

Make sure to replace the username and password. You can now upload this file to your website and access it via web browser.

Other Solutions That Have Worked for Users

If the above-mentioned troubleshooting tips failed to fix the database connection error on your website, then you may try these additional steps.

1. Update WordPress Site URL

Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database.

wordpress themes, wordpress plugins, crm wordpress, nextwoo, next addons, next3, amazon s3 console, amzon web service, Elementor, Gutenberg, woocommerce, amazon affiliate program, next campaign, email campaign, subscriber & newsletter, next social

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

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

Don’t forget to provide your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

2. Rebooting Web Server

Users on dedicated servers, local servers, and virtual private servers (VPS) can try rebooting their servers.

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

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

namecheap
William Jhonson
Written by

William Jhonson

William is a technology enthusiast who enjoys writing WordPress-related articles. He enjoys watching movies and sports in his spare time. He has a strong desire to go all around the world.

Table of Content

Table of Contents