You will find it weird when you get an error establishing database connection while trying to set up your WordPress and you’ve made sure that your database name, username, and password is exactly the same as your wp-config.php file. Well, assume that you have your database setup correctly, your username and password is correct, you have also linked up your database & username together and the privilege to access the WordPress database is correct. Once refreshing your WordPress page you will get an error “Error establishing a database connection“.
How to debug your WordPress database connection
One way to debug your WordPress database connect is to go to your wp-config.php look for
define( ‘WP_DEBUG’, false );
and change it into “true”.
Usually you will get the follow error,
Warning: mysqli_real_connect(): (28000/1045): Access denied for user ‘username’@’localhost’ (using password: YES) in /home/username/public_html/wp-includes/wp-db.php on line 1531
I totally understand your frustration right now. The devil lies under your password’s character. When you open up your wp-config.php, look for
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘your-password-here’ );
Solution for WordPress database connection error even database setting is correct
Most of us are using complicated passwords with the combination of special characters such as underscore, line, slash, backslash, single quote or apostrophe, double quote, etc. You can generate your complicated password from this website – https://passwordsgenerator.net/.
With all these combination characters of password, you will need to avoid single quote or apostrophe – ( ‘ ). Apostrophe will end up messing up your DB_PASSWORD defined in the config setting. As you can see,
This password will return syntax error and 500 to your website.
This password will return Database Connect Error.
This password will work fine.