Hi friends, today i am sharing the solution of database connection error in wordpress. One of my client got “Error Establishing a Database Connection” in his wordpress website intermittently. While doing R&D to find the root causes of this error, i realized that there are many ways to fix this error. Bellow is the lists of few possible solutions.
Important Note: Please take sufficient database backups before make any database changes.
1. Check Your wp-config file
Navigate root directory of your website and open wp-config.php file and make sure all database configuration details are correct in bellow code.
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */
define( 'DB_USER', 'username_here' );
/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */
define( 'DB_HOST', 'hostname_here' );
Please note that your database hostname will be different. It is depending on the hosting server.
2: Create A New Database User And Password
Open your website cpanel and click on “MySQL® Databases.” then Add New User with strong password and assign that database with new created user. After done above open your wp-config file and update database details with the new username and password.
3. Due to Corrupted Database
In this case you must be repaire your database by using bellow steps. Navigate the root directory of your website and open wp-config.php file. Add the bellow code at the end of wp-config.php file and save.
define( 'WP_ALLOW_REPAIR', true );
After doing above you will be able to access a wordpress database repair page on your website by open this url
http://yoursiteurl.com/wp-admin/maint/repair.php and click on Repaire Database button. WordPress can automatically look for common database errors and repair them.
If you are still getting the same error message, then check with your hosting company.