WordPress Error Establishing Database Connection

There are lot of good articles on web with generic solutions to the problem of WordPress Error Establishing Database Connection, this post discuss about issue occurred during site migration to latest webhosting server.

WordPress Webserver Environment

This issue is related to wordpress site migration to new cloud hosting server. The old webhosting server has following configuration

The PHP version associated with wordpress site as PHP 5.3, WordPress version is 5.1.8 and MySQL version 5.1 which is also deprecated. The current website is running good from past few years, but with recent development it was required to upgrade. Since few plugins were not supported on PHP 5.3 and so we have decided to migrate the wordpress website to new cloud hosting server with PHP 7 and MySQL version 5.6.

Solution Approach

We have migrated to new webhosting server with old PHP 5.3 and MySQL 5.1 version. This is to test and verify the website works well on new environment.

Once this is tested, we have switched to new PHP version ie from PHP 5.3 to PHP 7.2.34. Please note before you do this change, don’t forget to take backup of your website either by using some good plugins like ‘updraftplus‘ or ‘export’ option form tools. Also take backup of your databse using phpMyAdmin tool provided by your webhosting company or take databse backup from command line.

Error Location

While most of the online sources will point you to wp-config.php file for verifying DB credentials. In our case DB details were correct since we were able to access DB using phpMyAdmin. Still we were getting following error message

mysqli_real_connect(): (HY000/2002): Permission denied in /home/yoursite-com/public_html/wp-includes/wp-db.php on line 1612

To view this error you need to add following lines in ‘wp-config.php’ file

/*define( 'WP_DEBUG', false );*/ 
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

This will enable logging and you can view the log file as ‘debug.log’ in ‘wp-content’ folder.

Simple Solution

While all the database details were correct and still it was giving database error. So in wp-config.php, Use the IP address of your local server instead of localhost . That means set 127.0.0.1 at DB_HOST in wp-config.php . So the solution will be like-

/** MySQL hostname */
define('DB_HOST', '127.0.0.1');

After this change reload your webpage, and you can login to admin page. First thing you should do here is upgrade wordpress to latest version. Please note wordpress shows latest PHP version now. For first time upgrade it will show error for databse needs upgrade. Click on upgrade and refresh the page. For some time your site will be in recovery mode. But it will load up once upgrade is complete. The site still needs MySQL latest version and don’t forget to disable debug mode from wp-config.php. I am sure there are various solution that works out for you on this WordPress Error Establishing Database Connection, for example entering correct databse details, deleting DB user and creating new DB user, changing database host from localhost to exact host provided by webhosting provider etc, so let us know which solution worked for you while connecting to database using wordpress wp-config file.

Leave a Reply