Error establishing a database connection

Error Establishing a Database Connection: Possible Causes & How to Fix it

Using self-hosted WordPress, you’re likely to run into some technical issues at some point. One of the more common problems that people face is the “Error Establishing a Database Connection” error. In this post, you will learn what causes it and how to effectively fix it.

If you’re looking for quality shared hosting for your WordPress website, we highly recommend A2 Hosting. They offer unlimited SSD space, free automatic site backups, hosting optimized for speed and 24/7/365 guru technical support.

What does the error mean?

The Error Establishing a Database Connection error simply means that WordPress is unable to connect to the MySQL database. There are a number of things that can interfere with your WordPress website’s database connection, which can make it tough to fix.

In this post, we will go over possible causes for this common error, as well as ways to fix it.

What causes the Error Establishing a Database Connection error?

This error can be caused by a few different things, including:

  • Incorrect MySQL database details in your wp-config file.
  • A corrupted MySQL database.
  • An unresponsive database server.

So, what is a MySQL database? A MySQL database is an open source relational database management system (RDBMS) which uses a client-server model. Put simply, it is a place where data is stored and organized. The stored data is organized in tables.

SQL stands for Structured Query Language, and it tells the server what to do with various data. SQL statements can instruct the server to perform specific operations, including:

  • Data query – Requesting certain information from the existing database.
  • Data manipulation – Adding, deleting, changing, sorting and other modifications to the data, values or visuals.
  • Data identity – Defining a schema or relationship of each database table.
  • Data access control – Providing security measures to protect data, including specifying who can view or use information stored in the database.

The WordPress CMS requires MySQL to store and retrieve all of its data, including page and post content, user profiles and any custom post types. WordPress uses PHP to retrieve data from the database, and runs SQL queries to dynamically generate content.

In order for WordPress to connect to your MySQL database, it needs the following information:

  • Database name
  • Database username
  • Database password
  • Database hostname

The items listed above are stored in the WordPress configuration file, appropriately named wp-config.php.

If any of the information is incorrect in that file, WordPress will not be able to connect to your MySQL database, and you will get the “Error Establishing a Database Connection.”

Verify that your WordPress MySQL database credentials are correct

Usually, the MySQL database connection error has to do with incorrect details in your wp-config file. If you’ve recently moved your site from one host to another, this is likely the problem.

// ** 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 can access your wp-config.php file in order to check your existing database credentials. This can be done a few different ways, including via ftp or in using the file manager within your hosting account’s cPanel (if it has one).

IMPORTANT: Before you ever do anything with your wp-config file, you should create a full website backup. Even though your site is not functioning correctly if you’re getting the “Error Establishing a Database Connection” back everything up anyway.

Using ftp

If you’re using an ftp client (e.g. FileZilla, CyberDuck or something else), then you’ll find your wp-config.php file in the root folder (often called public_html) with a bunch of other php files, as well as wp-admin, wp-content and wp-includes folder.

wp-config.php file in public_html folder

To edit the file, you can simply right-click on PC (or CTRL click on a Mac) the file and choose View/Edit from the menu. Then, the file will open in a text editor and you can check the information for database name, username, password and hostname are correct.

MySQL Databases in the Databases section of cPanel.

To check all these items within your hosting account, log in to your cPanel and click on MySQL Databases in the Databases section. Here, you will be able to see the database name and username in the top table. Then, you can set a new password by clicking on Set Password in the table near the bottom of the screen.

Set password MySQL database user

Once you’ve confirmed that all of this information is correct, you can update it within wp-config.php if need be, and save your changes. Since you clicked on View/Edit, when you save the file it will automatically be uploaded back to ftp.

After you’ve saved your changes, visit your website to see if the error has been resolved. If it persists, then the problem lies elsewhere.

Try creating a new database user and password

If you’ve verified that your current MySQL credentials are correct, but you’re still getting an error, you can create a new database user and password.

Click the MySQL Databases icon in the Databases section and scroll down until you get to MySQL Users.

Add a new user to MySQL Database in cPanel.

Here, you can choose a username and password (write them down because you’ll need to add this info in your wp-config.php file). Click the Create a User button, then select the newly-created user and correct database from the dropdowns in the area below. Finally, click the Add button.

Check your MySQL database hostname

Since you’ve already made sure that database name, username and password are all correct, the next step is to check the database hostname.

While most WordPress hosting companies use localhost for this value, some of them use entirely separate servers to host MySQL databases. In this case, you’ll need to enter a value other than localhost in that line of your wp-config.php file.

Get in touch with your hosting company to get the proper MySQL database hostname.

Try repairing your MySQL database

If you’re seeing an additional error in wp-admin, your database may need to be repaired. This could include “One or more database tables are unavailable. The database may need to be repaird.”

There are a couple ways to repair your MySQL database:

#1 Using the WordPress database repair tool: Adding the following line to your wp-config.php file right before the “That’s all, stop editing! Happy blogging” line near the bottom.

1 define('WP_ALLOW_REPAIR', true);

After you’ve added that code and saved your wp-config file, you can check the settings by visiting http://www.yourwebsite.com/wp-admin/maint/repair.php

Please Note: You DO NOT need to be logged in to get to the database repair page. After you finish repairing and optimizing your database, don’t forget to remove the code you added to your wp-config.php file.

#2 Repairing your database in cPanel: You can easily repair your database directly without touching any WordPress files.

  1. Log in to your cPanel
  2. In the Databases section, click on MySQL Databases.
  3. Near the top in the Modify Databases area, choose the database you want to fix from the dropdown next to Repair a Database.
  4. Click the Repair the Database button.

See if your MySQL database server is offline

If you’ve used the troubleshooting methods mentioned above, but are still unable to connect to the database, it’s possible your database server is down.

This often happens as a result of heavy traffic, especially on shared hosting. If you suspect this is the problem, you should reach out to your web host’s customer support and see if your MySQL server is working.

Do you have multiple sites on your hosting account? If you have other websites, you can see if the error appears on those sites as well. This can help you confirm that the server is in fact down.

If you don’t have other sites with the same web host, you can go to your hosting dashboard and try to access phpMyAdmin in cPanel to connect the database.

Are you able to connect? If so, we need to verify that the database user has sufficient permission. Create a new text file called connectiontest.php or something similar (the name doesn’t matter, but the .php file extension does).

Paste the following code in the file:

<?php
$test Connection = mysql_connect('localhost', 'username', 'password');
if (!$testConnection) {
die('Error: ' . mysql_error());
}
echo 'Database connection working!';
mysql_close($testConnection);
?>

Replace the username and password, then save the file and upload it to your site’s root directory. Then, go to http://www.yourwebsite.com/connectiontest.php (use your domain name and whatever you called the file you made). If you see a successful connection message, then everything is all good. If not, you’ll see more details about the error.

Make sure you double-check that all the details are correct in your wp-config.php file.

Other possible fixes

If you’ve tried using the above methods without any success, there are a few more things that might help you resolve the error connecting to your MySQL database.

Updating your WordPress Site URL in phpMyAdmin

Within phpMyAdmin in cPanel, select your WordPress database and then click on the SQL link at the top.

Then, you will enter the following MySQL query:

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

Of course, here you want to use your own site URL and table prefix associated with your database.

Finally, click the Go button in the bottom right-hand corner to update everything.

Restarting your web server

If you’re using a dedicated server, local server or virtual private server, you can try rebooting it. As with your computer, sometimes all it needs is to be restarted.

Asking your web host for help

If you’ve exhausted all the possible fixes in this post, then you should contact your web hosting provider. If you’re using a quality web host, they will be able to help you solve your problem or at least give you some insight regarding the cause and how you may go about getting everything working right again.

Conclusion

Hopefully, this article has helped you effectively fix the “Error establishing a database connection” on your WordPress site.

Do you have anything you’d like to add? We aim to make our WordPress tutorials as thorough as possible, and always welcome additional information. Feel free to leave a comment with your questions or comments about this annoying error.


If you found this post helpful, please share it and spread the word!

Want more like this? Sign up for my newsletter to receive helpful WordPress Tutorials, Tips and Tricks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top