• Home
  • Blog
  • How to Fix the Establishing a Database Connection Error in WordPress
  • Blog
  • How to Fix the Establishing a Database Connection Error in WordPress

How to Fix the Establishing a Database Connection Error in WordPress

If you’re seeing the following on your WordPress website, ‘error establishing a database connection’, you could be in for a tough ride. This is a fatal error that causes your website to become inaccessible to users, which isn’t ideal.

When WordPress is unable to make a connection to the database, it throws up this error. Various things can cause this to happen, making it difficult for some people to troubleshoot.

We’re going to show you how to fix the error establishing a database connection in WordPress, easily, with step-by-step instructions.

Table of Contents

What Causes Error Establishing a Database Connection in WordPress?

error establishing a database connection wordpress

The ‘Error establishing a database connection’ issue is usually caused by incorrect database information in your WordPress settings. It can also be caused by a corrupt database or unresponsive database server.

WordPress uses databases to store, organize, and retrieve data which converts into other formats. For example, your website’s content and data will be stored in your WordPress database.

WordPress connects to a database everytime someone visits your website. Information is stored in your WordPress configuration file (wp-config.php).

To connect to your database, WordPress needs:

  • Database name
  • Database username
  • Database password
  • Database server
If any of the database items are incorrect, WordPress will fail to connect to your database server. If this happens, you’ll get the ‘error establishing a database connection’ issue.
If you’ve encountered this error, don’t worry, it’s one of the most common errors users encounter with WordPress. Let’s take a look on how you can fix this error with simple step-by-step instructions.

1. Check Your WordPress Database Credentials

If your WordPress database contains incorrect credentials, you’ll get the ‘error establishing a database connection’ error that we’ve been talking about. Why does this happen? If you’ve recently moved to a new host, this could be the reason for the error.

Like I mentioned earlier, your WordPress database credentials are stored in the wp-config.php file. If you’ve never looked at this file before, I’m going to walk you through how to do this. If you’re familiar with it, you can move on to the next step.

How to Access WP-Config file

Before messing around with the wp-config.php file, it’s important you create a backup of your WordPress website. The slightest mistake could cause havok, making your site inaccessible to users.

You’ll need an FTP client like FileZilla to connect to your website and securely access your files. Connect to your website using the FTP client (you can get your FTP login information from your web host).

The wp-config.php file is often located in the root folder of your website. Right click the file and select download. This will download the file onto your computer, allowing you to edit it using Notepad.

Check Your Credentials

Once you’ve accessed the wp-config.php file, you’ll want to make sure your database name, username, password, and host is correct.

In order to ensure this information is correct, you’ll need to access your WordPress hosting account dashboard. Click on MySQL databases under the database section.

Here you can take a look at your current databases to find out your database name and username. If you’re unsure of your password, you can change the user password so that it matches the wp-config.php file.

If the information already matches, or your website still shows the error, it means something else is causing an issue. Not to worry, we have more to cover!

2. Check Your Database Host Information

Once you’ve established that your database name, username, password, and host name is correct, you’ll need to check that you’re using the correct database host information.

Most WordPress hosting providers use localhost as the database host. But if you’re using a managed WordPress hosting company, they may use different servers to host databases. If this is the case, localhost will not be the correct information to use.

In order to find out your database host information, you’ll need to contact your WordPress hosting company and ask them for this.

3. Repair Your WordPress Database

If you’re getting a slightly different error, maybe one like ‘one or more databases are unavailable’, you may need to repair your database.

To repair your WordPress database, head back to your wp-config.php file. Find the line that says ‘That’s all, stop editing! Happy blogging’, then add:

define(‘WP_ALLOW_REPAIR’, true);

Then navigate to www.yourwebsite.com/wp-admin/maint/repair.php to see the settings. Here you can repair the database. Because you don’t have to be logged in to access this page, make sure you remove the line of code from your wp-config.php file once you’re done.

4. Check to See if Your Database Server is Down

If you’ve got this far and you’re still seeing the error, it may be that the database server is down. This can happen for a number of reasons, including an overload due to heavy traffic. If you’re using a shared hosting account, this is more common.

Your best bet will be to contact your web hosting provider via phone or live chat for the quickest response. Ask them if your MySQL server is responsive. 

If you can’t get hold of them, you can verify whether your SQL server is down by checking your other websites running on the same server. 

If you don’t have any other sites on your server, you can go to your hosting dsahboard and attempt to access phpMyAdmin to connect to the database. If you can connect to the database, you’ll need to make sure your database user has the right permissions.

Create a new file called testconnection.php and paste the following code in to the file:

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

Replace the username and password, then upload the file to your website. If the script runs successfully, it means your user has the correct permissions and something else is causing the error.

Other Solutions You Can Try

If the above tips didn’t fix the ‘error establishing a database connection’ in WordPress, there are some other steps you can take to try and resolve the issue.

1. Update WordPress Site URL

Using phpMyAdmin, you can try and update the WordPress site URL. Access phpMyAdmin via your hosting dashboard, then select your WordPress database.

Here you can click on the SQL tab at the top to bring up the SQL menu. Enter the following MySQL query and replace the iste URL with your own URL.

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

2. Reboot Your Web Server

If you’re using a dedicated server or VPS hosting, you can try and reboot the server. This will restart your website and database server, potentially fixing some issues.

Note: Make sure you know what you’re doing before attempting this. If you’re unsure, you can contact your web hosting provider for more information.

Related Posts: