The dreaded “Error establishing a database connection” in WordPress occurs when your website can’t communicate with its MySQL database. This can happen for several reasons, such as incorrect database credentials, a corrupted database, or an inactive MySQL server.
Causes and solutions to fix WordPress database connection error
To fix this error, you need to identify the root cause. Let’s explore some common causes and their solutions.
1. Check Hosting Server Storage
First, ensure your MySQL service is running smoothly. If your hosting storage is full, MySQL might stop working due to insufficient space. Check your storage usage and free up space if needed.
2. Check MySQL status on hosting
- Log in to your hosting control panel.
- Locate the database management section.
- Check the status of MySQL by viewing the operating status of the databases.
- Check the error message or information to understand and fix the problem.
In the database management section, you see a list of databases you have created. Check the status of MySQL by seeing if the databases are up and running. If one or more databases show an “active” or “running” status, the MySQL service is operating properly.
3. Check user and database permissions
You need to make sure that the user configured in wp-config.php
has full rights to operate the database. And in your hosting, update the permissions as follows:
You check the database permissions for users inside the mysql administration page.
You check whether the user is granted full access rights to the database or not through the “Add User To Database” section. Then, you select the “Full Privileges” option and make the changes.
4. Check the MySQL extensions used for PHP
Verify that the “mysqli” extension is enabled in your PHP environment. If you’re using cPanel hosting, you can typically manage PHP extensions through the PHP Selector tool (see screenshot below).
5. Check the connection configuration to the database
Wordpress has a configuration file called “wp-config.php” which will contain the necessary information to connect to the database, such as server name, username, password and database name.
define( 'DB_NAME', 'dev_itsmeit' );
/** Database username */
define( 'DB_USER', 'dev_itsmeit' );
/** Database password */
define( 'DB_PASSWORD', '!YouPassword!!#' );
/** Database hostname */
define( 'DB_HOST', 'localhost' );
Look at the configuration above, then you need to make sure you have configured the information correctly to allow Wordpress to connect to the database.
In addition, in the wp-config.php file there is another configuration that is also very important. By default, Wordpress has a table_prefix of “wp_”. So you also need to make sure table_prefix matches the current table you are connecting to.
$table_prefix = 'wp_';
When I encountered the “Error establishing a database connection” error, I noticed that the table_prefix in the database was set to “k2i7ua7nu_“, while the wp-config.php file in the WordPress installation was configured to use “wp_“.
This doesn’t match up and causes problems. However, after I adjusted the table_prefix to match “k2i7ua7nu_”, the system worked as expected.