Error Establishing Database Connection

Page content

How to fix error establishing database connection

Wp database connection error

If you are facing an error establishing a database connection, it can be due to multiple reasons. The error occurs when Wordpress is unable to connect to the mysql database. The database sores all the content and information on your wordpress website. So, in case of an error Wordpress cannot connect with the database and cannot show any of the content. As a result you will see a blank screen except for a notice saying there was an error establishing a database connection.

If you are in a fix why this error happens, then the most common causes include invalid credentials in your wp-config.php file or your mysql server is not running. These two are the most common causes that Wordpress might be unable to establish a connection with the mysql database. However, sometimes it might also happen because of corrupted database files in which case you will need to try to repair the database. Whether you are using a mysql or Mariadb server, the fixes are the same.

Suppose you just installed wordpress on your server but when you went to login to your Wordpress admin dashboard you received the notice that the connection between Wordpress and the mysql database could not be established. In that case, you will need to verify that you have entered the right credentials since Wordpress will need them to connect to the database.

Check if your database credentials are valid

In case you face the error establishing database connection problem, you must first check that you have entered the right credentials inside your wp-config.php file. This is the first place where you should check out for any errors and fix it. If you have stored your database name, username and password in a safe place, you must first match them with the values entered in the wp-config.php file.

The wp-config.php file is located inside your root folder. You can easily check out and edit it using an FTP client or through ssh. Open the wp-config.php file and check if the username and password as well as the database name entered inside the file are correct. You should also check the hostname to see if you have the valid hostname.

You can find the database credentials right at the beginning inside the wp-config.php file. They look like the following:

/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

/** Database username */
define( 'DB_USER', 'username_here' );

/** Database password */
define( 'DB_PASSWORD', 'password_here' );

/** Database hostname */
define( 'DB_HOST', 'localhost' );

Check all the four values including database name, database username, password and the database hostname which is localhost in most cases. You can check and verify from your host whether the information is correct or not. Most hosting providers provide users access to their database credentials and you can check inside your hosting account to find the right credentials.

Once you have verified that all the information entered in the wp-config.php file is accurate, and fixed any errors in the values, you can try loading your site again to see if the error is gone. If there was an error in the credentials, entering the right credentials must have fixed the issues.

While most hosts use localhost for database hostname, in some cases the database may be installed on a separate server. In this case, you will need to check out the hostname by contacting your hosting provider.

It is recommended to take regular backups of your Wordpress database. So, if anything goes wrong with the database, you can upload the backup to the database and bring your website online again.

Repair Wordpress Database

In some cases, repairing the wordpress database may fix any connection issues you are facing related to the database. The issue may be slightly different than the error establishing a database connection and one or more tables in the database might be unavailable in this case. However, the fix is to repair the database. It is an easy process and you do not need much technical knowledge to do it.

You can make changes inside your wp-config,php file to be able to repair and optimize your wordpress database. You will not need to login to the database or access it directly but it can be done from your Wordpress installation or using the cpanel.

All you will need to do is to add one line of code near the end of the wp-config.php file where it says that ‘That’s all stop editing. Happy Blogging!’’ Add the following code before this line.

define('WP_ALLOW_REPAIR', true);

Repair wp database

Now, you can easily access the page in Wordpress from where you can repair and optimize your database. This page is accessible at the following location:

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

Replace example.com with your own domain name and then you can repair the wordpress database with just one click. Once the process is complete, visit your website to see if it is loading as expected.

If you do not want to make changes to the wp-config,php file, you can go to the cpanel and then to modify databases and then run repair databases.

Restart mysql server

In some cases, it is just the mysql server that might have stopped running due to heavy traffic or some other issue. You will need to check that your mysql server (or Mariadb server) is active and running. On Ubuntu, you can check it using:

sudo systemctl status mysql-server

If it shows that the mysql server is active and running, then the problem may lie somewhere else. However, if your mysql server has stopped, you can restart it.

sudo systemctl restart mysql-server

Once you have restarted the mysql server, it must fix the issue. Now, try reloading your website to check if the issue has been fixed.

There are also some other fixes you can try like updating the wordpress site url in phpmyadmin or just rebooting your server. Sometimes rebooting the server is sufficient to fix minor glitches. If the issue is not yet fixed, contact your hosting provider.

However, always take periodic backups of your database to make sure that if anything happens and the problem is not easily fixed, you can always upload a backup of the database to fix all database related issues and bring your website back online.