How to fix Error Establishing a Database Connection Error in WordPress

How to fix Error Establishing a Database Connection Error in WordPress


 

You might have seen this database error when you accessed your wordpress website in the browser. Error shows that your site is unable to establish a connection to your wordpress database (PHP is unable to access the MySQL database). There are many reasons why you see this error, it might be due to corrupted wordpress database tables, incorrect database login details, mysql service is down etc. Follow the below steps to fix this database connection error.
 
Error establishing a database connection
 
IMPORTANT : You must take a full website backup before doing the below steps. (Download backup of wordpress files and database via FTP to your PC before doing the steps).
 

1. Check MySQL service status

 
The issue might be related to MySQL service, if you haven’t made any recent changes in the wordpress website. Log into your linux server via SSH as root user and check the status of mySQL service. You will see database connection error on website if the mySQL service on the server is in stopped status (service is down).

Run the below command to check the status of mySQL service and to start the service if it is not running.

Command : /etc/init.d/mysql status

OR

Command : service mysql status
 

root@server:~# service mysql status
mysql.service – MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: en
Active: inactive (dead) since Sat 2017-08-26 14:45:26 UTC; 18s ago

 
On the above server you can see that mySQL service is dead (stopped). Start the mySQL service by running the below command :

/etc/init.d/mysql start

OR

service mysql start

Check the status after restarting the MySQL service.
 

root@server:~# /etc/init.d/mysql status
? mysql.service – MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2017-08-26 05:07:17 UTC; 10s ago
Main PID: 9992 (mysqld)
Tasks: 29
Memory: 67.3M
CPU: 8.455s
CGroup: /system.slice/mysql.service
+-9992 /usr/sbin/mysqld

 
On the above server mySQL service shows active “Active: active (running)”.

The above commands will work on CentOS 6 and RHEL 6 servers. Try the below commands if your server OS is CentOS 7/RHEL 7.

systemctl status mysql.service –> Command to check the status of mySQL service

systemctl start mysql.service –> Command to start the mySQL service if it is not running.

Check the mySQL logs and the current disk usage if you are unable to start the mySQL service on the server. MySQL service will fail to start if you have reached disk or inode usage limit. Run the below commands to check the disk and inode usage.

#df -h

#df -i

If the usage is 100% then you must delete some unwanted files on the server to free some space.

Check the mysql logs for the exact error message. On cPanel servers mySQL log file is in location /var/lib/mysql/Hostname.err

Contact your webhosting provider if you do not have access to view the server error log and restart the service.

You will see MySQL connection error frequently if your website is getting lot of visitors and the server is running out of resources. MySQL server will be frequently down if there any attack targeting any websites on your server. You must check the access_logs of your website to check whether it is a legitimate traffic or an attack.

Do the below step to check whether the issue is related to your web hosting provider or it is related to your wordpress site.

Create a new test database, database user and grant all privileges. Create a testmysql.php file in the document root of your website. Enter the new database login details in the below code (Check image below).
 
Mysql Connect Testing
 
Access testmysql.php file in the browser www.yourdomain.com/testmysql.php. If you are still getting database connection error even after using the new test database login, then the issue is related to mySQL service on the server. Contact the hosting company and ask them to check the MySQL service.
 

2. Check the Database login details

 
You will see database connection error on the website if the database login details in the wordpress configuration file (wp-config.php) is incorrect. Check the database login details in the wp-config.php file.

DB_NAME = This is the wordpress database name.

DB_USER = This is the database user.

DB_PASSWORD = Database password.

You will see database error on the website if the above details are wrong.
 

cat wp-config.php

/** The name of the database for WordPress */
define(‘DB_NAME’, ‘martin_database’);

/** MySQL database username */
define(‘DB_USER’, ‘martin_dbuser’);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘DBPassword’);

/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

 
How to check whether the above details are correct ?

Copy the wordpress database login details in wp-config.php file. Try connecting to the database either through server backend or by creating a PHP file with mysql_connect() function. Checking database connection using mysql_connect function is already discussed in first step.

Steps to connect mySQL from the command line

You must login to the server via SSH as ‘root’ user before doing the below steps. Try mysql_connect method if you do not have SSH access to the server.

Syntax :

mysql -u database_username -p

OR

mysql -u database_username -p’database_password’

Example :

mysql -u martin_dbuser -p
Enter password: <-- Enter database password here mysql>

If you are connected to MySQL prompt then it means the wordpress database login details are correct.

OR

# mysql -u martin_dbuser -p’DBPassword’

OR

Try the below method if you do have SSH access to your server.

www.yourdomain.com/testmysql.php
 
mysql_connect script
 
If you are able to connect then it means the login details in your wordpress configuration file is correct.

If you see the error “ERROR 1045 (28000): Access denied for user ‘username’@’localhost’ (using password: YES)” then it means the database login details in wp-config.php file is incorrect. Enter the correct database name and database user in the wp-config.php file and reset the database password.

If there is no webhosting control panel installed on your server then you can try resetting the mysql database password from the server backend.

Example :

mysql>
mysql> SET PASSWORD FOR ‘martin_dbuser’@’localhost’ = PASSWORD(‘newDBpassword’);

After resetting the password, you must enter the new database password in the wp-config.php file. Open wp-config.php file in vi editor and update the DB_PASSWORD.
 

vi wp-config.php

/** MySQL database password */
define(‘DB_PASSWORD’, ‘newDBpassword’); #Enter the new password in this line.

 
Save the file and exist after entering the new password.

You can reset the database password from frontend if you have any control panel (like cPanel, plesk, directadmin, webmin etc) installed on your server. Steps to reset the site database password on a cPanel server is given below.
 
Reset mySQL database password through cPanel

1. Open a new web browser and go to http://YourDomain:2082 to access cPanel

2. Click on “MySQL Databases” Under “Databases”

3. Scroll down to end and here you can see mySQL users under “Current Users”

4. Click on “Change Password” under “Actions” to change the mySQL database password

5. Enter the MySQL password twice and click on “Change Password”

6. Go back to mySQL databases page

7. Under “Add User To Database”, select the username and password. Click on “Add”

8. Tick “ALL PRIVILEGES” and click on “Make Changes”
 

3. Database tables might be corrupted

 
The site will shows database connection error if the wordpress database tables are corrupted. Do the below steps to check whether the database tables are corrupted.

1. Log into your linux server via SSH as ‘root’ user

2. Run the below command to check the MySQL tables for any errors

mysqlcheck -c database_name

mysqlcheck –check database_name

3. Repair the mySQL table if the above commands shows any corrupted tables

mysqlcheck -r database_name

mysqlcheck –repair database_name

Do the below steps to check and repair the database if you have cPanel control panel installed on your server.

1. Go to http://YourDomain.com:2082 to access cPanel

2. Enter cPanel username and password to login

3. Click on “MySQL Databases” Under “Databases”

4. Under “Modify Databases” you can see option to “Check Database” and “Repair Database”

5. Select the Database from the dropdown and click on “Check Database” to check the database tables.

6. Select the Database from the dropdown and click on “Repair Database” to repair the corrupted database tables.

METHOD 2:

You can also repair wordpress MySQL database by adding the below line in the wordpress configuration file wp-config.php

Edit wp-config.php using vi editor and add “WP_ALLOW_REPAIR” at the end of the file like shown in the picture.
 

vi wp-config.php

define(‘WP_ALLOW_REPAIR’, true);

 
WP_Allow_repair
 
Now access the below URL in the browser to repair the wordpress database tables.

http://yourwebsite.com/wp-admin/maint/repair.php
 
Repair WordPress Database
 
Click on “Repair Database” and it will start repairing all the wordpress database tables (wp_users, wp_usermeta, wp_posts, wp_comments, wp_links, wp_options, wp_postmeta, wp_terms, wp_termmeta etc).

Click on “Repair and Optimize Database” if you want to repair and optimize the wordpress database. Optimizing the wordpress database will improve the performance. This option might take few more minutes to compared compared to “Repair Database” option.

Don’t forget to remove the “WP_ALLOW_REPAIR” line from the wp-config.php file after repairing the mySQL database tables.

Edit wp-config.php file and delete the last line that we have added : “define(‘WP_ALLOW_REPAIR’, true);”
 

4. Missing Database Tables

 
You will see database connection error if some of the tables in your wordpress database are missing. When you run Database repair using WP_ALLOW_REPAIR it will show if there is any missing database tables in your database.

ERROR:
wp_users: Table ‘database.wp_users’ doesn’t exist
wp_usermeta: Table ‘database.wp_usermeta’ doesn’t exist
wp_posts: Table ‘database.wp_posts’ doesn’t exist
wp_comments: Table ‘database.wp_comments’ doesn’t exist
wp_links: Table ‘database.wp_links’ doesn’t exist
wp_options: Table ‘database.wp_options’ doesn’t exist
wp_postmeta: Table ‘database.wp_postmeta’ doesn’t exist
wp_terms: Table ‘database.wp_terms’ doesn’t exist
wp_term_taxonomy: Table ‘database.wp_term_taxonomy’ doesn’t exist
wp_term_relationships: Table ‘database.wp_term_relationships’ doesn’t exist
wp_termmeta: Table ‘database.wp_termmeta’ doesn’t exist
wp_commentmeta: Table ‘database.wp_commentmeta’ doesn’t exist

You can restore these missing tables from the recent wordpress mySQL database backup. Most of the hosting providers keep daily, weekly and monthly backup of your website. Ask your web hosting provider to restore the tables/database if it shows tables doesn’t exit.
 

5. Check DB_HOST in wp-config.php

 
By default the DB_HOST value in wp-config.php file will be ‘localhost’. The DB_HOST value might be different on some hosting providers. Contact your hosting provider and ask what you should enter in this field.

/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);