• Our Story
  • Work | Our Services
  • Contact
  • SEO Blog
Commander SEOCommander SEO
  • Our Story
  • Work | Our Services
  • Contact
  • SEO Blog
Disaster Recovery

How to Fix Error Establishing a Database Connection in WordPress

December 20, 2022

WordPress is presenting you an “Error establishing a database connection” message? This is clearly a major incident – no one can de facto access your WordPress site any longer.

This error happens when WordPress is unable to connect to the database. There are a number of things that can go wrong with your WordPress database connection, which makes it hard for beginners to figure out what’s wrong.

This article will show you how to quickly fix the WordPress error “Error establishing database connection”.

What goes wrong when WordPress tries to connect to a database? The “Error establishing a database connection” error can be caused by wrong database information in your WordPress settings, a damaged database, or the database server simply is not responding. A database is a piece of software that makes it easy to store, organize, and get information. WordPress is a content management system that stores your website’s content and other data in such a database. Every time someone visits your website it tries to connect to this database. For WordPress to connect it needs the following information:

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

WordPress tries to get this information from a file called wp-config.php, your WordPress configuration file.

If this file contains wrong data WordPress won’t be able to connect to your database server, and you’ll see the “Error establishing a database connection” message.

This error can not only be caused by wrong credentials in a config file: The database server could be down or the database files could be physically damaged.

Let’s look at a step-by-step guide to fixing the “error establishing database connection” problem in WordPress.

1. Check the login information 

Wrong database credentials are in 9 out of 10 cases the reason for a database connection error in WordPress. 

As said before your database login information for WordPress is saved in the wp-config.php file. It is the configuration file for WordPress, which has important settings like information about the database.

In the wp-config.php file, check out the following lines:

1
2
3
4
5
6
7
8
9
// ** 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' );

Verify that the database’s host, username, and password are all correct.

You can check this information on the dashboard of your WordPress hosting account.

Simply access your hosting account, navigate to the Databases section, and then click MySQL databases.

In this example, we’ll be using the Bluehost control panel, but the steps are more or less the same for any other web hosting company.

cpanel MySQL Databases Fix the Error Establishing Database Connection in WordPress

Selecting this link will take you to the control panel section of your hosting provider where you can make changes to your database. In this section, you can discover both the name of the database you have access to and the name of the users.

cpanel current Databases

Below that, you’ll find the database users and links to change the user’s password.

cpanel Databases Users

You can modify your wp-config.php file after you’ve double-checked your database name, username, and password. Check if the database connection error still appears when you go to your site. Something else is wrong if you can still see the error.

2. Check Your Database Host Information

If you are sure that your database name, username, and password are correct, you may want to check that you are using the right database host information. Most places that host WordPress use localhost as the host for your database. But some companies host their databases on separate servers. In that case, your database host information will not be localhost. You need to make sure that your WordPress hosting company has the right information about your database host.

3. Fix the database of WordPress

If you’re getting a different error on wp-admin, such as “One or more database tables are not available,” you’ll need to do something else. The database may need to be fixed,” then your database needs to be fixed.

Add the following line to your wp-config.php file to do this. Make sure to add it right before “That’s it, no more changes!” Happy blogging’ line wp-config.php.

1
define('WP_ALLOW_REPAIR', true);
 

Once you’ve done that, go to this page: http://www.yoursite.com/wp-admin/maint/repair.php to see the settings.

4. See if your database server is offline

If everything appears to be right but WordPress still can’t connect to the database, your database server (MySQL server) may be down.

This could happen if a server is getting a lot of traffic. Basically, your host server can’t handle the amount of traffic (especially when you are on shared hosting).

Your site will become very, very slow, and some users may even see an error message.

At this point the most effective strategy is to contact your hosting company via phone or live chat and ask for details about the status of your MySQL server.

You can further verify that your SQL server is down by checking the functionality of any other websites hosted on the same server. You could perhaps connect the database and access PhpMyAdmin from your hosting control panel if you don’t have any other sites using the same account. If you can connect, we need to make sure your database user has the right permissions.

Make a new file and call it “checkconnection.php.” Then copy and paste the following code into it:

1
2
3
4
5
6
7
8
<?php
$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
}
echo 'Everything gonna be alright!';
mysqli_close($link);
?>

Make sure that the username and password in the script are changed. Now you can put this file on your website and use a web browser to access it. Getting a positive response means that your user has the right permissions, but something else is fishy here.

Check your wp-config file again to make sure everything there is right (re-scan for typos).

Users’ other solutions that have worked
If the troubleshooting steps above didn’t fix the database connection error on your website, try these extra steps.

Users have told us that these steps helped them fix the database connection error on their websites.

1. Change the WordPress site address

Use PHPMyAdmin to change the URL of your WordPress site. Just go to your hosting account’s dashboard, find phpMyAdmin, and choose your WordPress database.

phpmyadmin SQL Databases

Subsequently, click on the SQL menu at the top and type in the MySQL query below.

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

Change wp options to your own database table name, of course.

2. Rebooting Web Server

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

This will force your web server and database server to start over, which may fix any temporary problems that are causing the error.

3. Ask for help

If nothing else works, you may need to contact the support of the company that hosts your website. They are perhaps able to figure out the error and 4 eyes always do see more than 2. 

We hope this article helped you fix the error you were getting when trying to connect WordPress to a database.

Contact us
markus-seo

Markus Schad | Senior SEO Strategist

Markus Schad is Senior SEO Consultant with over 8 years experience in getting more traffic and visitors for his customers. He is the founder of Commander-SEO.com.

Our other Blog posts
ada032c3 8b21 4dfb 99e2 160646b112c8

Top 10 Mistakes Small Business Owners Make When Using SEO

February 24, 2023

Know Which Common SEO Mistakes to Avoid to Grow Your Local Business In spite of the importance of search engine

Read More »

6 Top Places to Hire WordPress Developers

March 5, 2023

Sometimes you need the help of a WordPress Professional. As WordPress is so widely used, there is no shortage of

Read More »
There is nothing to show here!
Slider with alias none not found.
Share
0

Recent Posts

  • 6 Top Places to Hire WordPress Developers
  • Top 10 Mistakes Small Business Owners Make When Using SEO
  • FREE AI Chatbot For WordPress using ChatGPT

Table of Contents

Commander-SEO.com – Your SEO Expert.

Facebook Twitter

© 2023 · Commander-SEO