How to solve WordPress ‘Error establishing database connection’
WordPress is the most commonly used CMS for websites. It is based on PHP and the website data is stored in a MySQL database, the details of which are mentioned in the configuration file.
As the information is stored in a MySQL database, it is common to encounter database-related errors during installation, migration, upgrade or occasional browsing of the WP site.
How to fix WordPress ‘Error establishing database connection’ message
If you notice WordPress ‘Error establishing database connection’ message in your site, it could be due to a number of reasons. Today we’ll discuss the 3 major causes and see how to fix them.
1. Incorrect details in the WordPress config file
WordPress stores the details such as the URL, permalinks, user account details, blog posts, categories, etc. in a database, which is specified in the config file.
During installation of WordPress, these following database parameters are provided by the user and WordPress stores it in the ‘wp-config.php’ configuration file.
Database Name - name of the database used by WordPress Database Username - name of the MySQL user used to access the database Database Password - password of the user used to access the database Database Host - hostname of the database Server, usually localhost
For a database connection to be established, these parameters should be correct and should work fine.
The MySQL user should be able to connect to the Database server with the password provided, and the user should be given adequate privileges to access the database.
Only if all these conditions are met, the database connection would be properly established and the WordPress would be able to fetch information from the database.
If there is any problem establishing the database connection during any website activity, the WordPress site would show the message ‘Error establishing database connection’.
1. The first thing to confirm when you see the database connection error, is to confirm the database connectivity. This can be done from the MySQL server.
Get the information from the wp-config.php file. Use the command ‘mysql -u username -p’ to connect. Provide the password and confirm the privilege for the user to the database using ‘use databasename’ command.
If the database connection is established fine, you should see a snippet like above. You can also test the database connectivity using ‘mysql_connect’ function in PHP.
If the connection is not established, you will see error similar to “Connect failed: Access denied for user ‘user’@’localhost’ (using password: YES)”. If so, you need to correct the database details in the config file with the valid ones and test the connection again.
2. If the user doesn’t have enough privilege to the database, grant the required rights. The error message ‘Access denied for user ‘username’@’localhost’ to database ‘DATABASE’‘ will be shown in the absence of enough privileges.
You can create MySQL users and grant privileges to them from the cPanel -> ‘MySQL Databases’ section or from the backend as the MySQL root user.
3. If you notice an error such as ‘Unknown database ‘db_name’’, it means that the database itself is non-existent and need to be created first.
You can create the database from cPanel -> ‘MySQL Databases’ section or from the backend as the MySQL root user. Also confirm that the name of the database that is given in the config file is correct.
Bobcares provides Outsourced Hosting Support and Outsourced Server Management for online businesses. Our services include Hosting Support Services, server support, help desk support, live chat support and phone support.