Are you seeing the ‘Error establishing a database connection’ notice on your WordPress website? it’s a fatal error that creates your WordPress website inaccessible to the users.

This error occurs when WordPress is unable to form a connection to the database. variety of things can affect your WordPress database connection which makes it a touch difficult for beginners to troubleshoot.

In this article, we’ll show you ways to simply fix the 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, a corrupt database, or an irresponsive database server.

Database Connection

A database may be software that makes 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.

Let’s take a glance at the way to fix error establishing database connection issue in WordPress with step by step troubleshooting.

1. Firstly, Check Your WordPress Database Credentials

The most common reason for database connection errors in WordPress is the wrong database credentials. If you’ve got recently moved your WordPress site to a replacement 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.

If you’ve got not edited the wp-config.php file before, then take a glance at our guide on the way to edit wp-config.php enter WordPress.

You’ll be trying to find the subsequent lines within the wp-config.php file.

[php]
// ** 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’ );
[/php]

You need to form sure that the knowledge for the database name, username, password, and database host is correct.

You can confirm this information from your WordPress hosting account dashboard. Simply log in to your hosting account and click on MySQL databases under the database area.

Database Connection

This will take you to the management page in your hosting dashboard. From here, you’ll determine your database name and therefore the username.

Database Connection

Below that you simply will find the database users and links to vary the user password.

Database Connection - DB User

Once you’ve got confirmed your database name, username, and password, you’ll change that information in your wp-config.php file if needed.

After that, try visiting your website again to ascertain if the database connection error has gone.

If you’ll still see the error, then this suggests that something else is wrong. Continue reading for more troubleshooting steps.

2. Must Check Your Database Host Information

If you’re confident that your database name, username, and password information is correct, then you’ll want to form sure that you simply are using the right database host information.

Most WordPress hosting companies use localhost as your database host. However, some managed WordPress hosting companies use separate servers to host databases. therein 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 the 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. confirm to feature it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

[php]define(‘WP_ALLOW_REPAIR’, true);[/php]

Once you’ve got done that, you’ll see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

DB repair

Note: the user doesn’t get to be logged in to access the database repair page. Once you’re done repairing and optimizing your database, confirm to get rid of this code from your wp-config.php.

4. Check if Your DB 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 thanks 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 together with your hosting provider and ask them if your MySQL server is responsive. If you’ve got other websites running on an equivalent server, then you’ll check those sites to verify that your SQL server is down. If you are doing not have the other site on an equivalent hosting account, then simply attend your hosting dashboard and check out to access phpMyAdmin and connect to the database.

If you’ll connect, then we’d like to verify if your database user has sufficient permission. Create a replacement file called testconnection.php and paste the subsequent code in it:

[php]<?php
$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
}
echo ‘Connected successfully’;
mysqli_close($link);
?>[/php]

Make sure to exchange the username and password. you’ll now upload this file to your website and access it via the 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 file to form sure that everything there’s correct (re-scan for typos).

Other Best Solutions That working good for the Users

If the above-mentioned troubleshooting tips did not fix the database connection error on your website, then you’ll try these additional steps.

As reported by our users, these steps have helped some users resolve the database connection error on their websites.

1. Update the Site URL if you are facing Database Connection Error

Try updating the WordPress site URL using phpMyAdmin. Simply access phpMyAdmin from your hosting account dashboard, and choose your WordPress database.

SQL

After that click on the SQL menu on the highest and enter the subsequent MySQL query.

[php]UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl'[/php]

Don’t forget to supply your own site URL and alter wp_options to your own table name as you’ll have changed the WordPress table prefix.

2. Rebooting Web Hosting 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 glitches causing the error.

3. invite help

If everything else fails, then you’ll get to contact your web server company. All good WordPress hosting companies will assist you in troubleshooting the matter, point you in the right direction, or maybe fix it for you.

You can also hire WordPress developers from Codeable which will assist you to fix this issue for reasonable rates. the simplest part is that these developers are highly-vetted by Codeable team, so you recognize they will be trusted.