Friday, November 22, 2024
HomeWordPress TipsHow To Fix the "Error Establishing a Database Connection" in WordPress

How To Fix the “Error Establishing a Database Connection” in WordPress


Did you just try to access your WordPress site only to be met by a scary message saying Error establishing a database connection instead of your WordPress site?

The Error establishing a database connection WordPress error is a somewhat common error that routinely pops up after changing hosts or migrating a WordPress site, though you might see it in other situations, too.

It can be scary because it seems like your site has completely disappeared and all of your data is gone.

First – don’t worry. Even though things look bad now, all of your site’s data should be safe and secure in the database. We’ll talk about this more in a second, but the issue here is that your WordPress site isn’t able to access your data, not that the data doesn’t exist.

Second, there are some troubleshooting steps that you can take to quickly fix the issue and get your site working again. So you should be able to fix the problem with minimal effort.

In this post, we’ll do two things:

First, we’ll explain a little more about what the Error establishing a database connection WordPress error means and why you might be seeing it.

Second, we’ll show you how to fix the WordPress Error establishing a database connection message for good. The exact troubleshooting steps that you’ll need to take often depend on when you’re encountering the message, so we’ll share different troubleshooting steps for two scenarios:

  1. Seeing this error on a newly created site or right after migrating a site.
  2. Seeing this error on an existing site where you didn’t change anything.

By the end, you should have a fully working WordPress site again!

What Does “Error Establishing a Database Connection” mean?

The Error establishing a database connection error message should look something like this on your WordPress site:

To help you understand what this error message means, we first need to break down WordPress into its two fundamental components.

There are roughly two parts to your WordPress site:

  1. Your files – these are your site’s PHP and HTML files, your CSS stylesheets, your JavaScript scripts, and so on.
  2. Your data – this your blog post content, your site settings, your users, your comments, and so on.

All of your site’s files sit on your server, but all of your site’s data lives in the database (hence the name).

If your site’s files can’t “talk” to the database, your site has no way of accessing all of the content, settings, and so on that it needs to render your site to visitors.

Because it’s impossible to display your site without accessing the database, your WordPress site will display the Error establishing a database connection message instead.

What can cause your site to have issues with connecting to the database? Well, we’re going to dig into that in detail in the troubleshooting steps in the next section, but some of the most common issues are the following:

  • Incorrect database credentials – just like you need a username/password to log into your WordPress dashboard, your site needs a username/password to connect to the database. If these credentials are wrong (or if the database name is wrong), that will trigger the error. This issue can happen a lot when you migrate a site to a new host.
  • Unresponsive database server – The server where your database lives could down, which is an issue with your hosting rather than your WordPress site.
  • A corrupted database – Bad plugins, add-ons, themes, and data-transfer interruptions might leave your database short of tables or completely corrupted at worst. Thankfully, WordPress includes its own database repair tool.
  • Corrupted WordPress files – issues with core WordPress files can also cause this error in fringe situations. The fix is to re-upload a clean copy of the core WordPress software.

In the next section, we’ll outline the most common solutions that are known to work.

How to Fix Error Establishing a Database Connection in WordPress

There are two high-level situations when you might see the Error establishing a database connection message in WordPress:

  1. After migrating a site to a new host/server or trying to create a brand new WordPress install.
  2. On an existing site where you haven’t made any recent changes.

These situations typically have different fixes, so we’re going to divide them into different sections. You can click the links above to jump straight to the specific section with your scenario.

With that being said, this can be a tricky error. So if the tips in one section don’t work, we recommend also trying the tips in the other section. Basically, we’re just trying to direct you to the troubleshooting steps that are most likely to fix your specific situation so that you don’t waste time.

1. Error Message After Migrating a Site or Creating a New Site

If you see the Error establishing a database connection message after migrating your WordPress site to a new host or creating a brand new WordPress site, the problem is almost always incorrect database credentials.

In order for your WordPress site to connect to the database, it needs to know the following information about the database:

  • Database name
  • Database user username
  • Database user password
  • Database location

When you migrate a site, sometimes the database credentials at the new host can be different than they were at the old host, which will trigger the error.

Here’s how you can fix that.

Check Your Database Login Credentials

To begin, you’ll want to find out which WordPress credentials your WordPress site is configured to use.

To do this, you’ll need to access your site’s wp-config.php file, which you can do by connecting to your server via FTP or using something like cPanel File Manager or your host’s file manager tool.

The wp-config.php file should be located in your site’s root folder. Open the file and look for the following lines of code:

// ** MySQL settings - You can get this info from your web host ** //

/** The name of the database for WordPress */
define('DB_NAME', 'database_name');

/** MySQL database username */
define('DB_USER', 'database_user');

/** MySQL database password */
define('DB_PASSWORD', 'database_password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

The details in the second pair of parentheses, where we have ‘database_name’, ‘database_user’ etc, will not appear as shown in the code above. These details are specific to your website and host.

The four lines simply mean:

  • 'database_name' – The name of your database goes here e.g. ‘wordpress_vista’
  • 'database_user' – Your database username goes here e.g. ‘wordpress_new’
  • 'database_password' – This is where you put the password for your database user e.g. ‘test!@#!@#’
  • 'localhost' – This is where you input your database host. The default is ‘localhost’ for most web hosts.

If any one of these four credentials are incorrect, you will get the Error establishing a database connection message.

At this juncture, what you need to do is counter-check these credentials against those stored in your database.

Check the Database Name

First, let’s check if you have the correct database name in your wp-config.php. Complete the following steps:

To get started, open phpMyAdmin. In most hosting accounts such as HostGator, GoDaddy, and Bluehost, phpMyAdmin is usually located under the Databases or Database Tools tab in cPanel or something similar:

Open phpMyAdmin

Your host should automatically log you into phpMyAdmin. Or, you might be prompted for a login screen in rare cases.

Once you’re logged into phpMyAdmin, click on the Databases tab to query a list of your databases. From the list of databases, can you see the database that appears after DB_NAME in your wp-config.php file?

Check for database name
Example of what to expect.

If you can see it, you’re golden – the problem does not lie in the database name. If you can’t see the database in phpMyAdmin, you need to contact your WordPress hosting provider, as the problem might be their server.

If the database doesn’t exist, you will need to restore one from backup (if you have a backup in place) or create a new one. See why it’s super-important to back up your WordPress site?

Let’s assume you’ve found your database and you have the correct name in wp-config.php. Now, let us check whether your database username, password, and host are correct.

Check Your Database Username and Password

Checking your username and password involves a little work. You need to create a .php file and store it in your WordPress root folder. This is the same folder where you found the wp-config.php file. Create a new .php file, and name it check.php or whatever you want.

You can create the file using FTP or via something like cPanel File Manager:

Create a new file

Once you create the file, it should appear in your root folder in a moment. Right click on it and choose Code Edit, or select it and click Code Editor on the menu. This should lead you to a blank page. Add the following code:

Be sure to replace 'localhost''root' – and 'password' with the database host (this will almost always be the default, localhost), username, and password in wp-config.php respectively. Save the changes and go to the following URL in your web browser – yoursite.com/check.php

Make sure to replace yoursite.com with your actual domain name.

If you see the message, “Connected successfully”, your login details are correct.

If you see “Could not connect: Access denied for user ‘yourusername’@’localhost’ (using password: yourpassword)” or some other error, your database host, username, or password is the culprit here.

If your login details are not working, you need to create new ones and then update your wp-config.php with the new details.

Oftentimes, the easiest option here is to just:

  1. Create a new database user.
  2. Assign that user to your WordPress site’s database.
  3. Update your wp-config.php file to use that new database user.

Aside: Your problem could be as a result of the last credential, DB_HOST. By default, most web hosts use ‘localhost’, but you can try changing this to an IP address or URL as per your web host. You can get a list of hosts and their DB_HOST values on the WordPress Codex. If your host is not on the list, please contact them for this information.

Moving on, let’s create a new user.

Create New Database User and Password

Go back to your cPanel and access MySQL Databases. It is also located under database tools, the same tab you found phpMyAdmin:

Open database list

Clicking on MySQL Databases should open something like this:

Database interface

Once the MySQL Databases screen loads, navigate down to where you have MySQL Users. Under this, you will notice Add New User:

Create a new database user

Enter a new username and password, and click Create User. Keep note of these details for your wp-config.php file.

Note: Your setup could be different, but the procedure is much the same.

Once you create the new user, you need to assign the user to your  WordPress database. This is the database that is shown after DB_NAME in your wp-config.php file.

To assign the user to your database, go to the MySQL Databases screen once again, and scroll down to where you will see Add User to Database:

Assign database user to database

Select the user you just created and your database, and click Add. On the next screen, make sure to give the database user all privileges and save those changes:

Give database user all privileges to fix error establishing a database connection

Now, go back to wp-config.php and update the file with the new username and password information.

Make sure there are no spaces between the quotes. For example, ' your-username ' is bad, but 'your-username' is great. Also, use single quotes as opposed to double quotes. Save all changes you’ve made to your wp-config.php.

Now, try accessing your website. If everything went well, it should be working again.

Are you still getting the error? If that’s a yes, you can try the other fixes as well.

2. Error Message On an Existing Site That You Haven’t Migrated

If you see the Error establishing a database connection message on an existing site where you haven’t made any big changes recently (like migrating to a new host), it can be a little trickier to troubleshoot the problem. However, there are still some steps that you can take.

Make Sure Your Host Isn’t Experiencing Any Issues

Before you dig too deeply into troubleshooting your site, you’ll first want to make sure that the problem isn’t caused by your web host.

Some web hosts configure their environments in such a way that your site’s database server is separate from the file server. In these configurations, it’s possible for the database server to be down but the file server to still be functioning. When that happens, you’ll see the Error establishing a database connection message.

A good host should be monitoring for this and quickly fix the problem. However, it’s worth sending a quick message to support or checking your host’s uptime status just to rule out any issues on their end.

Check for Corrupted Database (and Repair If Needed)

Another problem that can suddenly affect a WordPress site is a corrupted database. Thankfully, it’s pretty easy to test if this is the case.

To test, try to open up your site’s backend admin dashboard.

If your backend dashboard loads fine but the frontend of your site shows the Error establishing a database connection message, you might have a corrupted database.

Or, you also might see a different message like “One or more database tables are unavailable” in the backend, which is another sign of a corrupted database.

On the other hand, if you see the exact same Error establishing a database connection message in both the backend and the frontend, this is not the issue and it’s pointless to continue with this step. Instead, just skip ahead to the next step.

So – how can you fix a corrupted WordPress database?

Thankfully, WordPress includes its own tool to do this – all you need to do is enable it. Here’s the quick version for enabling the WordPress database repair tool – we’ll show you more detailed instructions below:

  1. Access your server’s files by connecting via FTP or using cPanel File Manager (or your host’s custom file manager tool if applicable).
  2. Edit the wp-config.php file.
  3. Add the following code snippet – define ( 'WP_ALLOW_REPAIR' , true);
  4. Visit yoursite.com/wp-admin/maint/repair.php in your browser, making sure to replace yoursite.com with your actual domain name.
  5. Run the database repair tool by clicking Repair Database.
  6. Once the database repair tool finishes, remove the code snippet that you added to the wp-config.php file.

Here’s what it looks like to add the code snippet to your wp-config.php file – add it above the line that says “/* That’s all, stop editing! Happy publishing. */”

define ( 'WP_ALLOW_REPAIR' , true); 
Enabling WordPress database repair tool

Once you’ve done that, you can go to yoursite.com/wp-admin/maint/repair.php to launch the database repair tool. Click the Repair Database button to start the process and WordPress will handle the rest:

Using WordPress database repair tool

Once the database repair process finishes, your site should hopefully start working again.

If it does, make sure to go back to your wp-config.php file and remove the code snippet that you added earlier. With this code snippet added, anyone can access the database repair tool, even if they’re not logged in to WordPress.

You don’t want that, which is why it’s disabled by default and why you need to make sure to disable it again when you’re finished.

Re-Upload a Clean Copy of WordPress to Fix Corrupted Files

In addition to a corrupted database, another common cause of the Error establishing a database connection message can be corrupted core WordPress files.

Diagnosing the specific problem is tricky, but you’ll find lots of situations where some weird tweak in a certain file is causing problems.

Thankfully, you don’t have to find the specific error. Instead, you can just re-upload a clean copy of the core WordPress software to make sure that all of your core files are exactly how they should be.

Don’t worry – re-uploading the core WordPress software will not overwrite any of your data. It’s basically just reinstalling the WordPress software, which is the same process your site undergoes every time you update WordPress.

However, because you probably can’t access your WordPress dashboard, you’ll need to do this via FTP.

To start, do the following:

  1. Go to the WordPress download page at WordPress.org
  2. Download the latest version of WordPress – it should be a single zip file that’s around 15 MB
  3. Once the download completes, extract the zip file on your computer

Now, you should have a folder that contains all of the core WordPress software.

Open this folder and delete the following files/folders:

  • wp-content folder
  • wp-config-sample.php file
  • wp-config.php file – this file shouldn’t actually be there, but if you do see it for some reason make sure to delete it.

Once you’ve prepped the clean copy of WordPress by deleting those files/folders, you’re ready to upload it to your site:

  1. Connect to your WordPress site’s server using FTP
  2. Upload all of the remaining WordPress files/folders to your root folder. You want everything to match up so that the new files are getting uploaded to the same location as the existing files.
  3. When your FTP program asks you what to do about duplicate files, tell it to overwrite all files with the new version. If you don’t see a warning about duplicate files, make sure that you’re uploading the new files to the correct location. You want to overwrite all the existing core WordPress files on your server.

Once the upload finishes, you should have a clean copy of all your core WordPress files and that will hopefully fix the Error establishing a database connection message.

Nothing Working? Try Restoring a Recent Backup

If nothing has worked so far, your best bet might be to just try restoring your site from your most recent backup.

If you or your host take regular backups of your site, you should be able to restore your site to working condition with little to no data loss.

WordPress Error Establishing a Database Connection FAQs

To finish out this post, let’s go over some common questions you might have about the Error establishing a database connection WordPress problem.

Why is WordPress not recognizing my database?

If WordPress isn’t recognizing your database, you’ll first want to make sure that the database name, user, and password are set properly in your site’s wp-config.php file.

If you have the correct credentials, another problem could be that the database user doesn’t have full permission to access the database. Make sure that you attached the database user to the database in cPanel.

Why is the WordPress error establishing a database connection intermittent?

If the Error establishing a database connection message is intermittent (i.e. it comes and goes), you can be confident that the problem is not incorrect database credentials. Instead, it’s likely that your database hosting server is being overwhelmed and failing during high-load periods.

You can consider reaching out to your host for help or upgrading to more powerful WordPress hosting.

How do I fix a corrupted database in WordPress?

To fix your corrupted database, you can try using WordPress’s built-in database repair tool. You’ll need to activate this tool in your site’s wp-config.php file. Then, you can launch the repair tool by going to yoursite.com/wp-admin/maint/repair.php.

How do I check my database connection in WordPress?

To test your WordPress database connection, you can upload a simple PHP file with a short code snippet to your site. We show you how to do it at the beginning of the troubleshooting steps in this post.

Why am I seeing the error establishing a database connection after migrating my WordPress site?

If you started seeing the Error establishing a database connection WordPress message after migrating your site, the problem is almost certainly an issue with the database name, username, or password that are defined in your site’s wp-config.php file. Make sure that all of these details are correct for the new host and also that the database user has full permission to access the database.

Fix This WordPress Error Today

The WordPress Error establishing a database connection error can be frustrating. However, in almost all situations, your site’s data is still there, you just need to find a way to fix the connection so that WordPress can access the database again.

In this post, we’ve shared some troubleshooting tips to help you solve the problem.

If you recently migrated your site or created a fresh WordPress install, it’s probably an issue with your site’s database credentials. If not, try the other solutions to fix the problem.

We hope this article helped you learn how to fix the “Error Establishing a Database Connection” error in WordPress. For some other tips on troubleshooting WordPress, you may also want to see our article on how WordPress beginners can dodge these 7 common mistakes.

Now you…

Have you experienced and resolved this error before? What caused it, and how did you solve it? Please share your tips and solutions with us, and make this post more resourceful. Thanks in advance!



Source link

RELATED ARTICLES
Continue to the category

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular

Recent Comments