Is there an ‘Error establishing a database connection’ warning on your WordPress site? It’s a fatal error that prohibits your WordPress site from being accessed.
This error happens when WordPress is unable to connect to the database. A lot of circumstances can damage your WordPress database connection, making it difficult for newbies to diagnose.
In this article, we’ll show you how to fix the problem of establishing a database connection in WordPress.
What is the cause of the WordPress Database Connection Error?
The ‘Error establishing a database connection error might be caused by incorrect database information in your WordPress settings, a defective database, or an unavailable database server.
A database is a piece of software that makes storing, organizing, and retrieving data for use in other programs very straightforward.
As a content management system, WordPress employs a database to store all of your content and other website data. Your website connects to the database every time someone views it.
WordPress requires the following information to connect to the database:
- The database’s name
- And the database’s username
- Database’s password
- The database’s server
This information is stored in your WordPress configuration file called wp-config.php.
WordPress will be unable to connect to your database server if any of these elements are incorrect, and you will obtain an error message reading “Error establishing a database connection.”
This is one of the most typical WordPress blunders. In addition to invalid credentials, this error can appear if the database server is down or the database files are corrupt.
Let’s take a step-by-step look at how to fix the error by establishing a database connection in WordPress.
Verify that the database credentials for WordPress are accurate.
The most common reason of database connection problems in WordPress is incorrect database credentials. If you’ve recently moved your WordPress site to a new host, this is most certainly the case.
Your WordPress database credentials are stored in the wp-config.php file. It’s the WordPress configuration file, and it’s where all of your important WordPress settings, including database information, can be found.
If you have not edited the wp-config.php file before, then take a look at our guide on how to edit the wp-config.php file in WordPress.
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 should double-check the database name, username, password, and database host to ensure that they are all correct.
This information can be confirmed in your WordPress hosting account’s dashboard. To find MySQL databases, simply log into your hosting account and browse to the database section.
This will take you to the database management page of your hosting dashboard. Here you may find the name of your database as well as the username.
Below is a list of database users, along with links to change their passwords.
Once you have confirmed your database name, username, and password, you can change that information in your wp-config.php file if needed.
After that, check to see if the database connection error has been fixed on your website.
If the error can still be seen, it means that something else is wrong. Continue reading for additional troubleshooting advice.
Check your database’s host information.
You should double-check that you’re using the correct database host information if you’re confident that your database name, username, and password are correct.
The bulk of WordPress hosting companies use Localhost as their database host. On the other hand, some managed WordPress hosting companies maintain databases on different servers. In that case, the database host information will not be localhost.
Contact your WordPress hosting company to validate your database host details.
WordPress’ database should be restored.
Here’s what you should do if you notice a different error in wp-admin, such as “One or more database tables are unavailable.” You must repair your database if you receive the notice “The database may need to be repaired.”
You can do this by adding the following line in your wp-config.php file. Make sure it’s right before ‘That’s it, no more editing!’ Happy blogging’ line wp-config.php.
define( 'WP_ALLOW_REPAIR' , true); |
After that, go to this page to examine the settings: http://www.yoursite.com/wp-admin/maint/repair.php.
The user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.
Check to see whether your database server is down.
It’s conceivable that your database server (MySQL server) is down if everything looks to be in order but WordPress still can’t connect to the database.
This could happen as a result of a server’s heavy traffic. The load on your host server is simply too great for it to cope with (especially when you are on shared hosting).
Your website will become very slow, and some users may get an error. As a result, the best thing you can do is contact your hosting company and inquire about the responsiveness of your MySQL server.
Whether you have other websites hosted on the same server as your SQL server, you can look at those to see if it’s down.
Go to your hosting dashboard and use phpMyAdmin to connect the database if you don’t have any other sites on the same account.
If you can connect, we’ll need to check to determine if your database user has sufficient permissions. Create a testconnection.php file and paste the following code into it:
<?php
$link
= mysqli_connect(
'localhost'
,
'username'
,
'password'
);
if
(!
$link
) {
die
(
'Could not connect: '
. mysqli_error());
}
echo
'Connected successfully'
;
mysqli_close(
$link
);
?>
If necessary, change the username and password. This file is now ready to be uploaded to your website and browsed through a web browser.
If the script is successfully connected, that means your user has sufficient permissions and something else is wrong.
Return to your wp-config file and make sure everything is in order (re-scan for typos).
Other Alternatives That Have Proven to Be Beneficial to Users
If the previous troubleshooting procedures failed to resolve your website’s database connection error, you can try the following steps.
According to our customers, these strategies have assisted some people in resolving database connection errors on their websites.
Your WordPress site’s URL should be changed
Change the URL of your WordPress site with phpMyAdmin. From your hosting account dashboard, select your WordPress database in phpMyAdmin.
Then pick SQL at the top of the page and input the MySQL query below.
UPDATE wp_options SET option_value= 'YOUR_SITE_URL' WHERE option_name= 'siteurl' |
If the WordPress table prefix has changed, remember to enter your own site URL and edit wp options to your own table name.
The Web Server is being restarted.
Users can restart dedicated servers, local servers, and virtual private servers (VPS).
This will restart your web and database servers, which may temporarily address some of the error issues.
Look for help.
If everything else fails, you may need to contact your site hosting company. All trustworthy WordPress hosting companies will help you troubleshoot the problem, either by pointing you on the right path or by resolving it for you.
You can also hire WordPress developers from Codeable to help you solve this problem for a reasonable price. The best part is that the Codeable staff has thoroughly vetted these developers, so you can trust them.
We hope this article proved useful in fixing the WordPress database connection issue. Check out our WordPress troubleshooting page for tips on how to fix WordPress issues on your own.
Discussion about this post