Is your WordPress website displaying the 'Error establishing a database connection' message? It is a fatal error that means users can't get to your WordPress site. In this post, I'll teach you how to simply solve the issue when attempting to connect to a database in WordPress. When WordPress cannot connect to the database, this error occurs. So also, a variety of other factors also might disrupt your WordPress database connection, making it even harder for WordPress rookies to debug.
Why Am I Seeing Error Establishing a Database Connection on My WordPress Site?
The error message "Error establishing a database connection" can occur for a number of reasons, including a faulty database, inaccurate database information in your WordPress settings, or an unresponsive database server.
What is a WordPress database?
A database in general is a command - line tool that allows you to systematically store, organize, and retrieve data into other applications. All WordPress websites have databases, and stores all of your content and other website data in a database. It then connects to the database every time someone visits your website. WordPress requires the following information to connect to the database: Database name, Database username, Database server, and database password. You can find these data in WordPress configuration file with the file name wp-config.php.
Unlike other files, the wp-config.php file is generated particularly for your site during the initial wordpress installation process, rather from being included with the main WordPress package. If any of these things are wrong, WordPress will fail to connect to your database server and display the message 'error establishing a database connection' like I said earlier, a variety of other things could be responsible for this error aside from wrong credentials.
This error can also occur if the database server is unavailable or the database files are corrupted or damaged. Below is a step by step guide on how to troubleshoot
"error establishing database connection" in WordPress.
1. Examine the WordPress Database Credentials
Wrong database credentials are the most prevalent cause of database connection errors in WordPress. This might be the most palpable cause especially if you just transferred your Wordpress website to a new host. The wp-config.php file contains your WordPress database credentials. The WordPress configuration file contains vital WordPress settings, including database information. In the wp-config.php file, check for the following lines of 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', 'localhost' );
This information can be reviewed using your Cpanel control panel or the WordPress hosting account dashboard. We will be utilizing the Cpanel control panel for the purpose of this lesson. Simply log in to your hosting account's cpanel and go to the database area, where you will see MySQL databases.
This will direct you to the database administration page on your hosting dashboard. You will be able to find and retrieve your database name and username from this page.
Next find what is similar to the screenshot below to make changes to the password if need be.
Once you've verified your database name, username, and password, you may modify them in your wp-config.php file as well if necessary.
Make sure all details correspond and are concordant.
// ** 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', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
Above is an important snippet of code you will find in the wp-config.php file
But first, set up an entire WordPress backup. The wp-config.php file is so important to a WordPress site that even a little error can render your site unavailable.
After that, return to your website to verify whether the database error connection problem has been resolved. If the error message is still on display it suggests that something else is incorrect. Check Step 2 below to see if it gets resolved.
2. Verify the Database Host Information
If you are certain that your database name, username, and password are accurate, consistent and corcordant, you should double-check that you are using the proper database host information. A majority of WordPress hosting providers utilize localhost as your database host. However, some managed WordPress hosting firms host databases on multiple servers. Your database host information will not be localhost in that situation. To validate your database host details, contact your WordPress hosting provider for helpful solution.
3. Repair WordPress Database
If you are experiencing a different problem on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then surely you need to repair your database. You may achieve this by adding the following line of code below to your wp-config.php file.
define('WP_ALLOW_REPAIR', true);
Be careful to add it immediately before ‘That’s all, stop editing! Happy blogging’ line of code in wp-config.php
After that, go to this page to examine the settings:
http://www.yourwebsite.com/wp-admin/maint/repair.php
It should be noted that admin user does not have to be logged in to access the database repair page. Once you've finished fixing and optimizing your database, delete this code from the wp-config.php file
4. Confirm your database server if it is unavailable.
If you've double-checked everything and WordPress still won't connect to the database, the problem may lie with your database server (the MySQL server). This might occur as a result of high server traffic. Your server is overloaded and cannot take any more traffic (especially when you are on shared hosting). Your website will become very sluggish, and some visitors may see an error. So, the best thing you can do is speak with your hosting provider and inquire whether your MySQL server is functional.
If you have other websites operating on the same server, you may use them to determine if your SQL server is unavailable. If you have no other sites on the same hosting account, just go to your hosting Cpanel and try to launch phpMyAdmin and connect the database.
If you are able to connect, then check if your database user has necessary permissions. Make a new file named testconnection.php and insert the following line of code below into it:
Replace the username and password with yours as it is very necessary. This file may now be uploaded to your website and accessed via a web browser.
If the script connects successfully, that signals your user has just enough permissions and there could be something else is wrong.
You may refer back to the step 1 of this post and return to your wp-config.php file and double-check that everything inside is right.
5. Review the Web address of your WordPress Site
If after following the above troubleshooting procedures, and error is not yet resolved, you may try this extra action by changing the url of your website.
Using phpMyAdmin, try changing the URL of your WordPress site.
Simply go to your Cpanel or Wordpress hosting account's dashboard and choose your WordPress database.
Then, on the top, pick the SQL option and input the following MySQL query.
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
See image below for more clarity
This technique have assisted some persons in resolving the database connection problem on their websites. If you try all these and it doesn't work. please drop a comment in the comment section of this post or contact me to get this fixed. Please share this post if you find it helpful. Thanks +