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.

Advanced-cache.php Already Exists Warning

Everything was broken when we tried to install yet another WP cache plugin. All the tabs and pages from admin panel are showing advanced-cache.php already exists warning.

Warning!
The file ../../wp-content/advanced-cache.php already exists. Please manually delete it before using this plugin.

Origin

Most of the wordpress installation done from dreamhost will have wp super cache plugin installed. This plugin will speed up wordpress blog, and so is recommended. If you do not have cache plugin installed then your wordpress blog will eat up lot of RAM(memory). There are multiple cache plugins available. We were trying out different plugins other than wp super cache. We tried to install hyper cache. Other than hyper cache there are tons of cache plugins, like, wp rocket, w3 total cache, WP fastest cache, Hummingbird page cache, & PageSpeed Ninja and so on.

Well, If you try to install another cache plugin without fist disabling current plugin then you will start to see this warning.

Solution for advanced-cache.php already exists warning

There are multiple solutions available for this warning. Like do FTP or ssh login to your server and remove folder from ‘plugins’ directory. or manually copy the file from the plugin folder ‘wp-content/plugins/wp-super-cache/advanced-cache.php’ to ‘wp-content folder’. And last one suggested to define ‘WP_CACHE’ constant to ‘false’ in ‘wp-config.php’. But none of the solution worked out in our case.

So, we have to do it hard way by editing database table. Login to dreamhost panel, navigate to ‘more’->’MySQL Database’ Click on phpMyadmin link in front of your database. Please note your database username and password will be different from your wordpress credentials. Login to your database and select ‘wp_3cajzx_options’ table. Search for ‘active_plugins’ in option_name as shown in below image.

Under ‘option_value’ you can see that, WP Super cache and Hyper Cache entries are there. First delete ‘hyper cache’ entry and re-order the indexing for ‘i’.

i:5;s:20:"wp-super-cache/plugin.php";i:6;s:22:"hyper-cache/plugin.php";i:7;s:19:"jetpack/jetpack.php";

Corrected entry after removing hyper cache will look like as below.

i:5;s:20:"wp-super-cache/plugin.php";i:6;s:19:"jetpack/jetpack.php";

After doing this change click on ‘go’ with ‘save’ option for this table. This will save the plugins table with removed entry for hyper cache plugin.

With this trick our admin panel is working, and we could see that all the plugins are available, only change was all the plugins were deactivated so we have to activate them. Hope this trick will solve your problem with this warning.