How to Fix Error Establishing a Database Connection

How to fix error establishing a database connection on your WordPress website? It’s a fatal error that creates your WordPress website inaccessible to users.

Error establishing a database connection occurs when WordPress is unable to connect the database. A variety of things can affect your WordPress database connection which makes it seem difficult for beginners to troubleshoot.

In this article, we’ll show you the methods to simply fix the error establishing a database connection in WordPress.

We know that you’re probably scared immediately, but allow us to assure you that this problem is often fixed and any permanent damage is often prevented. In fact, the error establishing a database connection is sort of common.

Understand the Error

The nature of the error establishing a database connection is self-explanatory

Here’s what it means exactly:

WordPress uses two main pieces of technology to supply you with all its awesome abilities: PHP and MySQL.

PHP is a programing language. The core WordPress files are written in PHP.

MySQL is a database technology. WordPress uses a MySQL database to store all of your website’s content. This includes your posts, pages, but also the smaller elements, like the title of your site, the layout of your widgets, and the colour settings, etc. Basically, the MySQL database is where every, even kept the littlest data of your website.

WordPress uses PHP commands to attach to the MySQL database, get the knowledge it needs, then display everything on the screen.

This is where the error comes into play. The error occurs when, for whatever reason, WordPress cannot access the knowledge within the database via PHP commands. When this happens, WordPress basically doesn’t know what to try next, therefore the only thing it can say is, “Error establishing a database connection.”

Causes for Error Establishing a Database Connection in WordPress

The ‘Error establishing a database connection issue is often caused by incorrect database information in your WordPress settings, corrupt database, or an irresponsive database server.

A database is software that creates it easy to store, organize, and retrieve data into other software.

As a content management system, WordPress uses a database to store all of your content and other website data. It then connects to the database whenever someone visits your website.

WordPress needs the subsequent information for connecting to the database:

  • Database name
  • Database username
  • Database password
  • Database server

This information is stored in your WordPress configuration file called wp-config.php.

If any of those items are incorrect, WordPress would fail to attach to your database server, and you’ll see the ‘Error establishing a database connection error.

It is one of the foremost common WordPress errors. Aside from incorrect credentials, this error also can appear if the database server is down, or the database files are corrupt.

Following Are The Methods to Resolve This Error

1. Check Your WordPress Database Credentials

The most common reason for database connection errors in WordPress is wrong database credentials. If you’ve recently moved your WordPress site to another host, then this might be the foremost likely reason.

Your WordPress database credentials are stored within the wp-config.php file. It’s the WordPress configuration file that contains important WordPress settings including database information.

Open your website’s wp-config.php file, looking for the following line in the file.

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

Make sure that all database credentials are correct. You can cross-check these credentials from WordPress hosting dashboard. Simply login into our account and click on MySQL under the database section and cross-check the credentials.

After the confirmation, if your database credentials are correct then something else wrong with your website. Change the credentials if required then revisit your website to check the error has been gone or not.

If you still see the errors then something else wrong and follow this article for further troubleshooting.

2. Check Your Database Host Information

If you’re confident that your database name, username, and password information is correct, then you should make sure that you are using the right database host information.

Most WordPress hosting companies use the localhost as your database host. However, some managed WordPress hosting companies use separate servers to host databases. In that case, your database host information won’t be localhost.

You need to contact your WordPress hosting company to verify your database host information.

3. Repair WordPress Database

If you’re getting a special error on the wp-admin, as an example, something like “One or more database tables are unavailable. The database may have to be repaired”, then you would like to repair your database.

You can do that by adding the subsequent line in your wp-config.php file. Make sure to add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

define('WP_ALLOW_REPAIR', true);

Once you have done with editing, you can check the settings by visiting this page:

Repair WordPress Database – Sitepoint

Note:  After repairing the database, don’t forget to remove this code from your wp-config.php.

4. Check if Your Database Server is Down

If everything seems to be correct, and WordPress still cannot hook up with the database, then your database server (MySQL server) could also be down.

This could happen due to heavy traffic on a server. Basically, your host server just cannot handle the load (especially once you are on shared hosting).

Your site will get really slow and for a few users, it’s going to even output the error. Therefore the neatest thing you ought to do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

If you’ve other websites running on the same server, then you’ll check those sites to verify that your SQL server is down or not.

If you don’t have other sites on the same hosting, then simply log in to your hosting dashboard and access phpMyAdmin, and connect the database.

If you’ll connect, then you should verify if your database user has sufficient permission. Create a new file with the name testconnection.php and paste the subsequent code in it:

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

Make sure to change the username and password. You’ll now upload this file to your website and access it via browser.

If the script connected successfully, then it means your user has sufficient permission, and there’s something else that’s wrong.

Go back to your wp-config.php file to make sure that everything is correct.

5. Update WordPress Site URL

If you are still unable to resolve the error then try to change your website URL through phpMyAdmin. Access our hosting account using phpMyAdmin and select the website’s database.

Update WordPress Site URL - Sitepoint

Update WordPress Site URL – Sitepoint

After accessing the database click on the SQL menu and add the subsequent MySQL query.

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

Kept in mind to provide your own site URL and change wp_options to your own table name as you may have changed the WordPress table prefix.

6. Rebooting Web Server

Users on dedicated servers, local servers, and virtual private servers (VPS) can try rebooting their servers.

This will restart your web and database server which can fix some temporary problems that cause the error.

7. Ask for help

If everything else fails, then we will recommend you to contact your web hosting provider, good hosting providers always help you to troubleshoot the problems even guide you in the right direction.

You also hire a WordPress Expert, who can help you to resolve this error at a reasonable price.

How to Fix Error Establishing a Database Connection

Leave a Reply