How to Fix Error Creating Database Connection in WordPress

Error Establishing a Database Connection or 'error establishing a connection to the database'’ for beginners can be very frustrating and annoying, especially when you don't realize what's going on and you feel like you're not doing any editing. Well, you can actually detect what's happening and fix this problem in several ways.

Why can an Establishing a Database Connection Error occur?

In simple terms, this error occurs because WordPress is unable to establish a database connection. The reasons why WordPress can't establish this database connection vary. Things that can cause the Establishing a Database Connection error include:

- Your login credentials are incorrect or have been changed

– Server database tidak responsif

- Database has been corrupted

- Errors can also occur due to server errors, and can occur due to other factors on the server as well. 

Fixed Error Establishing a Database Connection in WordPress

There are a few ways you can try to fix this database connection error in WordPress, namely :

1. Does the problem also occur when you access /wp-admin as well?

The first thing you need to determine is whether this error only occurs when visitors access your website or if it also occurs when you access /wp-admin. If the error does occur in both cases, then you can continue this tutorial to the next step.

But if you get a different error message in /wp-admin, such as 'one or more database tables are unavailable. The database may need to be repaired' (one or more database tables are unavailable. The database may need to be repaired), then you need to repair your database first.

You can do this by adding the following line to your wp-config.php file (open the file via your FTP account or file manager via cPanel). Add it right before the line 'That's all, stop editing! Happy blogging' in wp-config.php

 define(‘WP_ALLOW_REPAIR’, true); 

Save the file, and close it. Furthermore, you can view the settings by accessing the page:

Repair database di WordPress

To do this, the user does not need to log in to access this functionality. Because when the database is corrupted, users often complain that they can't log in. When you're done fixing and optimizing your database, be sure to delete the file from wp-config.php.

If this doesn't work and you're still experiencing Error Establishing a Database Connection in WordPress, try the next tip to resolve this issue.

2. Checking the WP-Config file

WP-Config.pgp is an important file in your WordPress installation. This file is where you specify the details for WordPress to connect to your database. If you want to change your root password or database password, you'll need to change this file as well. So the first thing you should always check is whether all your wp-config.php files are the same or not:

 define(‘DB_NAME’, ‘database-name’);

define(‘DB_USER’, ‘database-username’);

define(‘DB_PASSWORD’, ‘database-password’);

define(‘DB_HOST’, ‘localhost’); 

Note that the value of 'DB_HOST' is not always 'localhost'. Each hosting can have a different value. Popular hosts such as HostGator, BlueHost and Site5 usually use the value 'localhost'. As for other hosting, please cross check first.

Some expert bloggers also suggest that users replace this 'localhost' value with IP because this often happens in WordPress when running in a local server environment. For example, in MAMP, DB_Host became normal again when they changed their IP.

 define(‘DB_HOST’, ‘’); 

Keep in mind, the IP address of each hosting service varies.

If this still doesn't work as expected, then the problem might be a server error.

3. Check MySQL Server

It's not uncommon for WordPress to fail to establish a database connection when website traffic is high. This happens because the host server can no longer handle the load, especially when you're using shared hosting. As a result, your site becomes very slow, and some users experience 'Error Establishing a Database Connection'. When this happens, it's best to contact your hosting provider and ask whether or not the MySQL server is responsive.

If you want to test it to be sure, you can try going to other sites that are on the same server. If they also have the same problem, then there is an error with their MySQL server. But if you don't have any other sites hosted on the same server, try going to cPanel and access phpMyAdmin, and connect to the database. If you can connect, then you need to verify that your database has enough permissions. Create a new file called testconnection.php and paste the following code:


$link = mysql_connect(‘localhost’, ‘root’, ‘password’);

if (!$link) {

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


echo ‘Connected successfully’;



Make sure you change the username and password according to your website. If the connection is established, this means your user has sufficient permissions. Go back to your /wp-config to make sure everything is correct.

If you've done this step and you can't connect to phpMyAdmin, it means something is wrong with your server. It doesn't mean the MySQL server is down. It could mean that your users are not properly authorized.

To resolve this, you can contact your hosting provider to have them investigate further. And if it is true that the error is in the user permissions, ask them to reset your permissions. That way, the 'Error Establishing a Database Connection' issue will be resolved.


The problem of 'Error Establishing a Database Connection' is different from one another. Not surprisingly, the solution to fixing the database connection error can also differ from one another. The important thing to remember is that you should always backup your website before editing files on your website. This will help you to anticipate unwanted things happening.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.