Fixing WordPress error establishing a database connection error

WordPress database tables

If the WP CMS cannot receive data from the DB server, your site won’t work at all. This is because every detail you enter in any form of a WordPress website is saved to the WP DB. If you’ve encountered the error “Error establishing a database connection” on your website and you’re wondering how to get rid of this error, go through the below paragraphs where we’ve shared the reasons why this issue occurs and its solution.

The WP error establishing a database connection might problem might occur on sites because of the following three reasons:

Invalid details in the wp-config file

One of the first things that users do after downloading the WP CMS and extracting the zip folder is edit the wp-config file to enter the DB name, username, password, and the table prefix. If any entry related to the DB is incorrect, WP won’t be able to communicate with the DB, and you will see an error instead of the WP installer. Hence, make sure that every detail you enter in the config file is correct.

The database server is down

The WordPress content management system stores and retrieves data from the MariaDB or MySQL database. If the DB server process has been killed or isn’t active, or has encountered an error, WordPress won’t be able to get data. To fix the problem on a shared server, contact the company’s technical support team right away.

If your website is hosted on an unmanaged server, you must first check if the MariaDB or MySQL process is running. To do so, enter this command ps aux | grep "mariadb" or ps aux | grep "mysqld" in the terminal and press the enter key. If the process is running and your WP site shows a DB-related error, you should check the MySQL/MariaDB error log file. This file will have details of the error the DB server has encountered. For instance, the error log file may have “out of memory” error traces. The temporary solution to the “out of memory” Mysql error is to kill the process and start the DB server again. You can kill the process using kill -9 pid command, where pid is the process id. The permanent solution to this problem is to add more RAM to the server.

User password is changed, or the user is deleted

When you change the DB username password and don’t update the same in the wp-config.php file, the WP CMS will show this message – error establishing a database connection instead of the website’s page.

If the DB user whose details are entered in the WP configuration file is deleted, WordPress’s attempt to access the tables will fail. Instead of checking whether the user is there in the user’s table or not or to update the user’s password, you can run the following query:

CREATE USER IF NOT EXISTS 'wp_user_name'@'localhost' IDENTIFIED BY 'user_password';

In the above command, wp_user_name is the DB user name you’ve defined in the wp-config.php file, and the user_passoword is the new password of your choice.

Permission is modified

When you create a DB user, you may grant permission to access the DB tables or make changes to it, or both. If the permission has been modified or revoked, the user won’t be able to access the table. To fix the DB permission problem, run the following SQL query:

GRANT ALL ON wp_db_name.* TO 'wp_user_name'@'localhost';

In the above query, wp_user_name is the user name of the DB you’ve defined in the WordPress config file. Once you run the above query, run the FLUSH PRIVILEGES query.

If the server is under your control, the above two problems won’t occur at all. If other people have access to the server, they may accidentally delete the user or change its password.

To make sure that this problem doesn’t occur again, never play with the WP database user or the configuration file. Ensure that only the people you trust have access to the config file and the WP DB or the server.

pramod
Pramod

Pramod is the founder of wptls. He has been using WordPress for more than nine years. He builds web applications, and writes about his experiences with various WP products on this site.

Leave a Reply

Your email address will not be published. Required fields are marked *