Did you receive a message on your WordPress website saying there was an "Error establishing a database connection"? It is a fatal error that restricts users from accessing your WordPress website.
This error happens When WordPress cannot establish a connection with the database. It might be challenging for beginners to diagnose problems with your WordPress database connection because many factors can impact it.
In this article, we will show you how to easily fix the error establishing a database connection in WordPress.
What Causes Error Establishing a Database Connection in WordPress?
UA faulty database, an unresponsive database server, or inaccurate database information in your WordPress settings may contribute to the "Error establishing a database connection problem.
A database is a piece of software that makes storing, arranging, and accessing data in other programs is simple.
WordPress utilizes a database to store all your content and other website data since it is a content management system. When someone visits your website, it establishes a connection with the database.
WordPress needs the following information for connecting to the database:
- Database name
- Database username
- Database password
- Database server
This information is stored in your WordPress configuration file called wp-config.php.
The 'issue establishing a database connection' issue will appear if any of these details are incorrect and WordPress cannot connect to your database server.
One of the most common WordPress errors is this one. The database server may be offline, or the database files may be faulty, in addition to invalid credentials, which may also cause this error to appear.
Let’s take a look at how to fix error establishing database connection issue in WordPress with step-by-step troubleshooting.
Check Your WordPress Database Credentials
Incorrect database credentials are the most common reason for database connection errors in WordPress. This may be the most probable explanation if you switched hosts for your WordPress website.
The wp-config.php file is where your WordPress database login information is kept. The WordPress configuration file contains important WordPress settings, including database information.
You’ll be looking for the following lines in the wp-config.php file.
// ** 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' );
You must confirm that the database name, username, password, and details are accurate.
You can verify this information from your WordPress hosting account dashboard. To access MySQL databases, log into your hosting account and choose the database option.
In our example, we're utilizing the Bluehost dashboard, but the procedure is the same regardless of the hosting control panel.
You will be on your hosting dashboard's database administration page by pressing here. The username and database name are available at this location.
The database users and links to update user passwords may be found below.
You may make any required changes to your database name, username, and password in your wp-config.php file once they have been confirmed.
Visit your website once again after that to determine whether the database connection error has been resolved.
If the error is still visible, then something else must be wrong. For further troubleshooting procedures, keep reading.
Check Your Database Host Information
If you have confidence that your database name, username, and password information is correct, ensure you use the correct database host information.
The majority of WordPress hosting providers store your database on localhost. However, some managed WordPress hosting providers maintain databases on different servers. Localhost won't be shown as your database host information in such a situation.
To verify your database host details, contact your WordPress hosting provider.
Repair WordPress Database
For example, if you see an error message such as "One or more database tables are unavailable" on the wp-admin. If this error message appears, your database has to be repaired.
The following line should be added to your wp-config.php file to do this. Be careful to insert it just before line wp-config.php.
define('WP_ALLOW_REPAIR', true);
Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php
Note: The database repair page may be accessed without the user being logged in. Delete this code from your wp-config.php after fixing and optimizing your database.
Check if Your Database Server is Down
If everything seems correct, but WordPress can still not connect to the database, your MySQL server may be down.
This can be due to an overloaded server. Basically (particularly if you are using shared hosting), your host server cannot manage the traffic.
Your website will become very slow and can even produce an error for certain visitors. Therefore, the best option is to contact your hosting company through phone or live chat and inquire whether your MySQL server is responsive.
You may check the other websites hosted on the same server to see whether your SQL server is down.
If you have no other websites hosted on the same account, go to your hosting panel, try to enter phpMyAdmin, and connect the database.
If you can connect, we must verify your database user's rights. Copy and paste the following code into a new testconnection.php file:
Change the username and password in care. You may now upload this file to your website and see it in a web browser.
If the script could connect, your user has the required rights, and another issue exists.
Check your wp-config file again for errors and to confirm everything is properly.
Other Solutions That Have Worked for Users
You may attempt these extra actions if your website's troubleshooting advice provided above did not fix the database connection error.
Our customers have reported that some users have used these approaches to help them fix the database connection error on their websites.
Update WordPress Site URL
Attempt to use phpMyAdmin to modify the WordPress site's URL. You may easily choose your WordPress database by going to phpMyAdmin from the hosting account dashboard.
After that click on the SQL menu on the top and enter the following MySQL query.
UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
If you modified the WordPress table prefix, remember to provide your site URL and replace wp_options with the name of your table.
Rebooting Web Server
Try restarting your server if you use a dedicated server, a local server, or a virtual private server (VPS).
Your web server and database server will restart as a result, which might resolve any short-term issues that were the cause of the error.
Ask for help
You may need to contact your web hosting firm if everything else is needed. Any reputable WordPress hosting provider will assist you in identifying the issue, send you the correct path, or even fix it for free.