Error establishing a database connection is a most common error in WordPress which occurs for many reasons. Commonly this error occurs while you update, install, migrate WordPress one server to another server. It is most frustrating when you did not change anything and this error occurs. In this article, we will show you all possible reasons and solutions of error establishing a database connection.

How to Fix the Error Establishing a Database Connection in WordPress (Updated in 2023)

Error Establishing a Database Connection

Common reason and solutions of Error Establishing a Database Connection in WordPress

These are the main cause of an error establishing a database connection:

  • Wrong MYSQL database login details
  • WordPress database is corrupted
  • Database server is unresponsive
  • MYSQL Database User Has Limited Privileges

1. Wrong Mysql Login details:

This is the most common cause of the error establishing a database connection. Check your wp-config.php file. It contains all details to establish a connection to the database. Open your wp-config.php file via FTP or default file manager. Check the following details are correct or not.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

If you change your host and migrate the WordPress to another server and successfully edit the database name, user and password correct, this error commonly occurs due to “define DB_HOST” Because Different hosts use different hosting environment and this value is different in many Hosting. Netzole Hosting uses localhost and if you face problem, please open the ticket at support. Mostly Hosting uses localhost and some uses the different value. Many times this error fixed by changing the hostname to the IP address by defining as follow:

define('DB_HOST', '176.0.0.5:22');

2. WordPress database is corrupted

Many times you did not change anything and this error occurs. It may be due to the corrupt database and need to repair the database. WordPress provides a functionality to repair the database from its backend. For this follow the following steps:

How to repair corrupted Database in WordPress

First add the following line to the wp-config.php file.

define('WP_ALLOW_REPAIR', true);

After adding the above line, type the given link to the browser and a main setting page will appear to repair the database.

http://www.yoursite.com/wp-admin/maint/repair.php

fix corrupt database wordpress

Click on “Repair” or “Repair and Optimize” and your database will be repaired in a few minutes. Do remember after repairing the database, delete the above function line from the wp-config.php file because this function does not require the login and due to this anyone can go to repair setting page to mess up with your database.

3. MYSQL Server is down

If your site has heavy traffic, you can get this error during the peak hours due to high database queries and some of your users may see this error. In this situation, contact your hosting provider to know your Mysql server is down or not.

If you have others site on the same server, both sites should be down due to server down. In this situation, it will be fixed after restarting the mysql service.

If one site is up and the other is down then your database server is not down. You can also test the server status by creating a new file in your installation root directory and name it testconnection.php and paste the following code into this file.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Congratulation Connected successfully';
mysql_close($link);
?>

Make sure to change mysql_user and password value with username and password respectively into line 2. Type file address in the browser and test. You can see page “”Congratulation Connected successfully”” if it connected successfully. If not connected use value ‘root’  in place of ‘mysql_user’ If you get the access denied error through testconnection.php results or connecting to your phpMyAdmin, contact to your hosting provider.

4. MYSQL Database User Has Limited Privileges

If any above method is not working, then apply this method. Go to MySQL database via Hosting Cpanel. Delete the current user that is connected to the database. After deleting the current user, Create the new user and give the full privileges to the new created User. Now open wp-config.php file and replace old database username with the new username and save it.

If you are still facing the error, you can hire us for fixing it.

Pin It on Pinterest

Shares
Share This