How to Fix “Error Establishing Database Connection” in WordPress

If you've gotten this far, it's likely that your WordPress installation is displaying the following message: “Error establishing database connection” Or, when entering your site, you see a completely blank page. In programming terms, this means that your WordPress app is unable to connect to the database. Therefore, your site appears down, although you may have already realized that.

In most cases, this error appears for one of the following reasons:

  1. the credentials you have entered to access your database they are not correct
  2. there was an error installing or migrating site data
  3. the tables that make up your database have been corrupted.

In any case, You will be relieved to know that a connection error to your wordpress database is one of the easiest problems to solve in WordPress.At least almost always.

To learn more about this error, how it occurs, and how to fix it, I recommend starting at the top of the list of solutions, and implementing them in consecutive order until your site is back online.

What is this error and how does it occur?

Your site can be seen as made up of two main parts: its static files (images, for example), and its database. The database, coded in the MySQL language, stores much of the total information displayed on your site. For example: usernames, passwords, posts, categories, tags and other content.

The PHP files that make up WP are programmed to request this information from the database, and the database returns it along with the locations of the static files. Both sets of information are involved when a browser asks the hosting server (Duplika, for example) to send the data so that your site can be displayed on the screen.

If for some reason there is a conflict in the database, the PHP files request information to be able to display the content of the site, but the database does not respond, instead presenting the following error:

“Error establishing a database connection.”

Your site's PHP files are then unable to connect to the database, and the reasons can be so varied that the hardest part of solving it is figuring out exactly what caused it.

The error connecting to the MySQL database by WordPress can be due to:

  • An error installing WordPress, or migrating it.
  • The database may have been accidentally deleted.
  • The credentials with which it is intended to access are wrong.
  • There is a conflict with your database prefixes.
  • Your file permissions may be assigned incorrectly.
  • Your site URL may be incorrect.
  • There may be plugins or themes added that are causing a conflict.
  • The files or the database may be corrupted.
  • Your server may be malfunctioning, or overloaded.

Sometimes, the error can occur after making some changes to your site they might seem innocent enough, until they "break" something. For this reason, it is important to mention that testing changes before publishing them can help avoid this type of situation in the future.

Possible solutions to the database connection error

If your site appears as a blank page, or displays a "database connection error" message, try the following solutions in the order they appear. The idea is to attack the problem from the most common situations, to reach (if they do not solve the problem) more complex solutions.

1. Deactivate your Plugins

Sometimes, a plugin can make a database change that ends up breaking the normal functioning of a site. Examples of this are those security plugins that modify the prefixes of the database, or the permissions of the files. If your other plugins don't get along with this change, the result is a general malfunction, and your site will go down.

In addition, some hosting plans have a strict list of plugins, where it is not allowed to install and activate certain components to ensure the security of the server you rent. If you do it anyway, your site is likely to go down.

This is why disabling your plugins can be an effective way to fix the connection error. While you don't have access to your backend to do this, this can be done by accessing your files.

How to deactivate a plugin if I don't have access to my site?

After logging into your cPanel, go to Files> File manager.

There, look for the directory in which your site is located. Navigate to /wp-content/plugins/ and temporarily rename each listed plugin folder.

Click on the first name in the list, then select the button Rename in the options bar at the top of the page.

Keep the original name intact so you don't forget what the plugin was, but add something to the end of it, like “-deactivated” to remember why you renamed it.

Continue renaming the other plugins in the list in the same way. If your site comes back online, it means that one of your plugins was responsible.

Now that your site is back online, rename the plugin folders back to their original names one at a time, checking with each rename if your site is still available or has crashed again. When this happens, the culprit will be the last plugin you have returned to its original name.

At this point, you can take a closer look at the plugin to find what exactly caused the error, and if it can be replaced with a similar plugin.

It is important to note that if one of your plugins made a big change to your database that caused the site to crash, disabling the indicated plugin will not fix the problem. In this case, you will need to continue to the list of solutions below to try another remedy.

2. Check your credentials

Check your site URL

If renaming the plugins did not fix the problem, check the access details you have written in your file wp-config.php and compare them with the information in your database. Fortunately, you will not need to access the backend of your site to perform this step.

In cPanel, you can find the file wp-config.php going to Files > File Manager and navigating to the directory where your site is hosted. Click on the file wp-config.php from the list, then press Edit at the top of the page.

There are a few things you'll need to look for in the edit text (and make a note of), so you can compare what you find there with information that should be identical in your database.

First, take a look at the file wp-config.php and see if you can find anything Similary to the following example:

define('WP_HOME','http://your-site.com');
define('WP_SITEURL','http://your-site.com');

You may not see these two lines, but don't worry; if they do not exist in the text of the file you have opened, you can add them yourself. But if they exist in advance, you need to take note of them and make sure the information is correct. If it is not (for example, the name of your site –or its URL– does not appear correctly), you must apply some corrections that I explain below.

To correct the URLs, substitute http://your-site.com in the example above with the correct URL of your site. Also, make sure that both lines are perfectly structured as we wrote them above (not counting the name of your site and URL). Check that they do not have a typing error, or a missing tilde. Another thing: if you have an SSL certificate enabled on your domain, you should replace: http:// by https://.

You also need to verify that the URL you found has the path www. included, as long as it corresponds to your site's path settings. If your database includes a URL without the triple W, or vice versa, this must be changed to match the correct pathname of your database. Is not the same http://www.misitio.com that http://misitio.com, and each domain has its own URL configuration (plus, as we mentioned before, if you have a security certificate, the http should change to https.

If these lines don't appear in the text you're viewing, you'll need to add them manually. Sometimes the database may not be available because an incorrect URL was entered on the login page. General Settings from the admin dashboard, within basic WordPress installations, or before Multisite has been enabled.

If you add these lines and the error persists, you can remove them and continue trying to fix it with the following options.

I now recommend a similar approach, this time with the file functions.php which is in the folder /wp-includes/ .

See if you can find lines similar to the following example:

update_option( 'siteurl', 'http://your-site.com' );
update_option( 'home', 'http://your-site.com' );

As in the previous example when we edit wp-config.php, these lines that we show you now will only exist for the file functions.php Yes you have added them before. If you find them, make sure they are correct and free of typos; If not, fix them, and then check to see if your site is back online.

replaces http://your-site.com with your real URL (and if you have an SSL certificate installed, don't forget to change https:// by http:// .

Also, check if www. is included in the URLs, because if the triple W is not, but it is present in this file (or vice versa), then your site is down because of it. Make the change to include or remove www. as necessary. If this didn't work, continue with the following tests.

Check your database details

reopen the file wp-config.php and look for this block of text (which you'll usually see at the beginning of the document):

// ** 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');

Check your database name, username, password, and hostname. In the example above, your database name should appear where it says database_name_here, your real username should replace username_here, and your actual password will be found where the example shows password_here.

It is likely that your hostname was the same (ie localhost) although it may also be another in your case. For this reason, it is important that you contact your hosting provider and verify if this line is correct or if localhost it should be replaced with a different name.

If you make a change to your hostname, save the file wp-config.php and check if your site has been reactivated. If your hosting administrator checks that your hostname is correct, or the change you make doesn't fix the problem, make a note of all the other credentials from the example above and scroll down the document.

Find a line now Similary to the next to find the prefix of the database that your wordpress is requesting:

$table_prefix = 'wp_';

You can have something different in your file instead of wp,but whatever this value is, it should represent your database name as well. Take note of this.

As the next step, go back to the cPanel page and click the MySQL databases inside section Databases.

Scroll down to the section Current databases and find your site's database in the list.

Compare the database name and username that appears here with the same information that appears in your file wp-config.php. If the details don't match, change these details in your file to match. Don't forget to save the changes.

You must change the login credentials mentioned above in the lines define('DB_NAME', 'database_name_here'); Ydefine('DB_USER', 'username_here') . Also, change the part of  database_name_here of the real name of the database you searched for and username_here to the correct username you found.

You can also try updating the password associated with your database username. In the page MySQL databases, go to the section Current users and click the button Change Password.

Once you have changed the password, update your file wp-config.php to reflect the change. Modify the line define('DB_PASSWORD', 'password_here');  and, in particular, the part password_here for the new password. Check if your site is online again, and if it isn't, you can at least verify that the database username and password are correct, and that this user can successfully connect to the database.

Create a test file at the root of your site and save the following code in it to make sure there is no room for human error:

<?php
$link = mysql_connect('your_hostname', 'database_username', 'database_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connection successful!';
mysql_close($link);
?>

don't forget to change your_hostname with your correct hostname, databse_username with the current database username and finally replaces database_password with the actual password.

You can name this file whatever you want, but I recommend something descriptive, like testdatabase.php either testdb.php. Just make sure it ends in .php or this test will not work!

Once this is done, access this file from your browser. Using the example above, load the address http://tusitio.com/testdb.php and if the login details work, you will see a “connection successful” message on the page.

If the connection to your database was unfortunately unsuccessful, then you know that your username or password is not matching and needs to be changed in your file wp-config.php. You can review the steps above to make the changes.

You can also try creating a new username and password if all else fails. In cPanel, go to Databases > MySQL Databases and scroll down to the section Add new user.

Enter a new username and password, and click the button create user. Now you can add this new user to your database.

The next area of the page allows you to add the new user you have created to your database. Select it from the list labeled as User.

Next, select your database from the list just below. press Add, and your new username will be added.

The final step required is to update your file wp-config.php to reflect these new login details. You could accomplish this as previously described, but be sure to include the prefix from the database when adding it to your file wp-config.php or else it won't work.

Don't forget to save the changes. Now, you can visit your site to check that it is online. If it's still down, you can try to repair your database.

3. Fix your MySQL database

Restore the correct permissions

When all else fails, you can try checking your file permissions to make sure they're correct. If they are not configured correctly, they can cause the database to generate this connection error.

You can replace all core files with a fresh install. To achieve this, download the latest copy of WordPress from wordpress.org. Unzip the folder and delete the folders wp-config-sample.php Y /wp-content/ .

Both contain crucial data that you need to make your site work, so it's not a good idea to replace them. Therefore, removing them from the new copy avoids accidentally overwriting them.

Use an FTP client to transfer fresh WordPress core copies to your site, and don't forget to choose the option to overwrite all old files with new ones. This way, you won't have to worry about deleting old files before transferring new ones.

Repair your database

Repairing your database tables is an ideal alternative when all of the above did not hit the spot. Luckily, cPanel has a tool to check tables for errors in just a couple of clicks.

In cPanel, go to Databases > MySQL Databases and scroll down to the section Modify databases.

There you can both verify and repair them.

4. Contact your Hosting Administrator

If you've tried everything and nothing seems to work, contact your hosting provider, as they may detect problems on your server that are not related to the common solutions listed here.

Let them know about the tests you have done, and send them the link of this note as an easy way to explain the steps you are following.

Don't worry, it's a fixable bug, and hopefully your site should be back online and the “error establishing database connection” go down in history.

Still, I recommend taking regular backups of your site so that you can easily restore it in case something goes wrong in the future.

Customize WordPress Database Error

In case we want to show a slightly more descriptive error, instead of the classic “Error establishing connection”, we can take advantage of a simple functionality that does not require any type of plugin.

It will simply suffice to create a file with the name db-error.php inside the wp-content directory, with this example content (which we can customize to our liking).

<?php

	header('HTTP/1.1 503 Service Temporarily Unavailable');
	header('Status: 503 Service Temporarily Unavailable');
	header('Retry-After: 3600'); // 1 hour = 3600 seconds
	mail("[email protected]", "Database Error", "There is a problem with teh database!", "From: Montgomery Scott");

?>
<!DOCTYPE HTML>
<html dir="ltr" lang="en-US">
	<head>
		<title>Service temporarily unavailable</title>
		<style type="text/css">
			h1, p {
				font-family: Helvetica, sans-serif;
				font-size: 24px;
				color: #333;
				}
			p {
				font-size: 14px;
				}
		</style>
	</head>
	<body>
		<h1>Database error</h1>
		<p>We have a technical problem — Please try again shortly</p>
	</body>
</html>

We are Duplika

Give your site the hosting it deserves

no comments on How to Fix “Error Establishing Database Connection” in WordPress

back to blog

Duplika

Duplika

We are online, we are not a bot :)

I will be back soon

Duplika
Hi 👋
Select the prefered contact method to get in touch.
Connect via:
chat