This article provides you with steps to solve the infamous "Error connecting to Database" issue in WordPress. This guide give you the steps on what to check in cPanel but the same things can be checked in other Control panels.



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

This error occurs when WordPress is unable to make a connection to the database. A number of things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.

In this article, we will show you how to easily fix the error establishing a database connection in WordPress.



Have you checked your WordPress Database Credentials?


The most common reason for database connection error in WordPress is incorrect database credentials. If you have recently moved your WordPress site to a new host, then this could be the most likely reason.

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

You’ll be looking for the following lines in the wp-config.php file.

1
2
3
4
5
6
7
8
9
// ** 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 need to make sure that the information for the database name, username, password, and database host is correct.


Have you checked Your Database Host Information ?

If you are confident that your database name, username, and password information is correct, then you may want to make sure that you are using the correct database host name.

Most WordPress hosting companies use localhost as database host. If you migrated your WordPress to us from a different please make sure to update the database host to localhost.



Have you repaired WordPress Database?

If you are getting a different error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following 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 that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

Repair WordPress database

INFO: the user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.