Establishing a Database Connection WordPress

Are you having trouble with the “Error Establishing a Database Connection” in WordPress? I’ll show you how to quickly fix this issue and get your site up and running again.

There are several troubleshooting steps to diagnose and resolve this error effectively. By following these steps, your site can return to normal operation without losing any content or data.

First, I’ll explain some common reasons why you might see the “Error Establishing a Database Connection” in WordPress. Next, I’ll guide you through each step to address these potential causes, helping you restore your site’s functionality.

Error Establishing a Database Connection in WordPress

What causes the Error Establishing a Database Connection in WordPress?

WordPress requires both a web server and a database to work. The web server hosts the PHP files and other files your WordPress site needs to run, such as media files. The database holds all your post and page information, plus site configuration details. When a user goes to view a page, PHP pulls the data for that page from the database. If this connection is broken for any reason, you get a database connection error.

Here are some common causes of the Error Establishing a Database Connection in WordPress:

  • Incorrect login credentials – incorrect database login credentials is the most common cause of database connection errors. This is usually the cause if you see the message immediately after moving your WordPress site to a new host.
  • Insufficient user privileges – you might have the right username/password, but the user might not have the proper privileges. Again, this is more common if you experience the error after migrating your site.
  • Database corruption – this can result from direct manipulation of the MySQL database but can also happen accidentally, for example, from installing or deleting a plugin.
  • File corruption – as with the database itself, corrupt files can also cause the Error Establishing a Database Connection in WordPress.
  • Database server problems – if your web host uses a separate database server, issues with that server could trigger the Error Establishing a Database Connection in WordPress message even if the server that hosts your site’s files is working fine.
  • Sudden increase in traffic – a sudden spike in traffic can cause this database error to happen as your site struggles to cope with an increase in concurrent database connections. This is more likely if your site is using shared hosting.

Again, the first two problems are most common if you experience the error after migrating your site to a new host. The other problems can happen to any site.

How to fix the Error Establishing a Database Connection in WordPress

Now we know what the potential causes are, it’s time to fix the problem. Follow this step-by-step guide to solve the database connection problem and get your site back online.

A good starting point is to make a backup of your site. In solving the problem, you don’t want to risk harming your site further. Having a backup is a sensible safeguard against that happening. Make sure you backup both your files and your database.

Check your database login credentials in wp-config.php

Your first port of call should be your wp-config.php file, which you will find in the directory of your site called public_html, htdocs or www. Use an FTP client to download it.

Or, if your host offers cPanel, find the File Manager icon in your dashboard and navigate to the appropriate folder.

cPanel File Manager

Download the wp-config.php file and keep it safe. You’ll want this version as a backup copy.

Download wp-config.php from cPanel File Manager

Next, look for the following lines of code in wp-config.php:

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

/** The name of the database for WordPress */

define( 'DB_NAME', 'database_name_here' );

/** Database username */

define( 'DB_USER', 'username_here' );

/** Database password */

define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */

define( 'DB_HOST', 'localhost' );

/** Database charset to use in creating database tables. */

define( 'DB_CHARSET', 'utf8' );

/** The database collate type. Don't change this if in doubt. */

define( 'DB_COLLATE', '' );

The above example shows sample data. For the first four settings listed (database name, database username, database password and database hostname), you should have information supplied by your host. If you installed WordPress manually, you might have defined the first three credentials yourself.

You can check if the information you have is correct by verifying the database name, database username, database password and database hostname. You’ll find the relevant information in the Databases section of cPanel if you’re a cPanel user.

cPanel Databases

Check the database name

To check the database name, go to phpMyAdmin and look for it on the left of the screen. It should match the database name in your wp-config.php.

phpMyAdmin showing database name
wp-config.php showing database name

Double-check it’s the right website by clicking on the database name, then the wp_options table (yours might have a different prefix). Look for the site URL and home page. These should show your website’s URL.

wp_options siteurl and home

Check the username and password

If the database name checks out, you can test if the username and password in wp-config.php are correct by running a simple PHP script on your server.

Enter the following code into a text editor such as Notepad and save it as testconnect.php:

<?php

$link = mysql_connect('localhost', 'username', 'password');

if (!$link) {

die('Could not connect: ' . mysql_error());

}

echo 'Connected to database';

mysql_close($link);

?>

Where the script says username and password, replace those with the values from your wp-config.php for database user and password.

Upload this script to your web hosting in the public_html, htdocs, or www directory (the exact one will depend on your host).

Run the script by going to yourdomainname.com/testconnect.php. If you get the success message, you know that the username and password are correct.

If you get an error, you will need to reset the database user’s password or create a new database user and password.

In cPanel, you can find the database user under MySQL Databases.

cPanel MySQL Databases

To change the database user’s password, look for Current Users and select the Change Password option for the user you want to update. Use the Change Password button when you’re done.

Databases Current Users Change Password

Alternatively, you can add a new user by completing the Add New User form.

MySQL Users Add New User

When you have created your user, add that user to the database by selecting from the drop-down controls and using Add.

Add User To Database

You’ll then see a screen to Manage User Privileges. Check All Privileges, then Make Changes.

MySQL Manage User Privileges

Remember to update wp-config.php and save it with the password and/or user. You can also delete the testconnect.php from your server.

Check your database hostname

If you’re still getting a connection error after you’ve verified your database, user and password, check your database hostname. Database hostname is usually localhost, though some hosts may use 127.0.0.1. If neither of these work for you, check with your host to find out the correct value.

Check database user privileges

If you still get the error message after verifying your credentials, you can check if your database user has the correct privileges. If you created the database user yourself, you may have inadvertently given them insufficient privileges.

In cPanel, go to MySQL Databases, then Current Databases. Look for the Privileged Users and click on the one you want to check.

Current Databases Privileged Users

See what privileges are checked and make sure that All Privileges are enabled. Use Make Changes to save all privileges. Next, try accessing your site again.

Insufficent privileges for the database user

Repair the database

If you’ve checked the database login credentials and database user privileges, the next step is to try repairing the database. You might need to do this if you have the message “One or more database tables are unavailable. The database may need to be repaired.” in the wp-admin.

You can access repair mode by adding the following line to the end of wp-config.php:

define('WP_ALLOW_REPAIR', true);

Edit your wp-config.php and save, or upload your updated wp-config.php file to your web space.

Then, visit yourdomainname.com/wp-admin/maint/repair.php.

You will get two options: one to repair the database and the other to repair and optimize the database. The second one takes more time.

WordPress Repair Database

Choose the one you want to use, and when it’s complete, check again to see if the website has resolved the connection error.

WordPress repairs complete

Remember to remove the repair function from your wp-config.php, or anyone will be able to run the repair script on your server.

You can also run the repair command within phpMyAdmin. Log in and select your database; check All tables; and run the Repair table command found in the With selected drop-down menu.

phpMyAdmin repair table

Finally, you can use the Repair Database function. It’s available from the Modify Databases option within MySQL Databases in cPanel.

Modify Database Repair Database function

Check for corrupted themes or plugins

If you have corrupted theme or plugin files, this can be another cause of the database connection error.

You can change your theme in phpMyAdmin if you can’t access the wp-admin.

If this solves your problem, you know there’s an issue with your theme. Check to see if there’s an updated version that resolves the error, or use another theme.

To test for faulty plugins, you can deactivate all your plugins within wp-admin. If the wp-admin is inaccessible to you, you can deactivate your plugins by renaming the plugins folder within wp-content via FTP or File Manager to plugins_old.

Rename the plugins folder in File Manager

Now, check to see if the connection error was fixed and if you have access to wp-admin. If so, you know that one of your plugins was at fault. Rename the plugins_old folder back to plugins. Then, find out which plugin was faulty by activating them one by one in the wp-admin and checking if the error appears.

If you’ve checked your theme and plugins and still experience the Error Establishing a Database Connection in WordPress, read on.

Restore WordPress core files

Another possible reason for the failure to connect to the database is corrupted core files. The way to fix this is to upload a fresh copy of WordPress core files to your web space.

It’s a good idea to take a backup first before carrying on. If you’ve made any changes to your files, such as .htaccess, you’ll want to preserve the copy and use it when the other core files have been uploaded.

Download the latest version of WordPress from https://wordpress.org/download/.

Get WordPress

Unzip the file. Then, delete the wp-content folder and the wp-config-sample.php file. This is so you don’t overwrite your existing content or create a new wp-config.php file.

Delete the wp-content folder and wp-config-sample file

Using FTP, upload all the other files to your web host, overwriting the existing files.

When you’re done, check to see if the database connection problem has gone away. If not, proceed to the next step.

Check if your database server is down

Another source of the Error Establishing a Database Connection in WordPress is that your database server might not be working correctly. This is more likely if you use shared hosting because shared hosts allocate limited resources to each website. If your site has experienced a big jump in popularity, the database server might not be able to handle the increased number of database connections required.

One way to lighten the load on your database server is to use a caching plugin to serve static versions of your web pages. This way, the web server doesn’t have to query the database so often to access your content.

If you’re running multiple WordPress sites on your host, check to see if they are running. If the others are down, it may indicate a database server problem.

You should also check with your hosting provider to see if there is an issue with the database server and, if so, when they expect it to be fixed.

Contact your host

If all else fails, contact your host and ask them for help with the problem. Let them know what you’ve tried already – they may be able to spot something you’ve missed.

Fix Error Establishing a Database Connection in WordPress for good

The Error Establishing a Database Connection message in WordPress is something that you’ll want to sort out swiftly, as it breaks your website. Fortunately, there are a number of ways to resolve it.

By far the most likely cause is incorrect database credentials – you should always check these first. If that’s not the case, you can check the database user privileges, repair the database, check for file corruption, see if your database server is working properly, or get help from your host.

Once you fix the problem, your site should start functioning properly again without any data loss or ongoing issues.

For some more general tips on fixing problems in WordPress, you might also want to read our guides on general WordPress troubleshooting and common errors.

Do you still have any questions about fixing the Error Establishing a Database Connection message on your WordPress site? Let us know in the comments below!

Yay! 🎉 You made it to the end of the article!

0 Comments
Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!