Are you installing a fresh copy of WordPress or upgrading the existing one and getting this message error establishing a database connection? I know this can be very frustrating situation but don’t worry, here you will learn how to fix this database error quickly.
The reasons behind this error can ranges from system breaks, user errors, someone at the back-end failing to keep up and even natural calamities. Sometimes, favorable things like a huge surge in website traffic too can bring about this error message when the server is unable to handle the high demand.
Whatever be the reason, the user is always put in a strenuous situation. However, there are proven ways to get around this backlog if you are strong in your server and web development basics.
Let’s first understand why this database connection error message shows up.
Every post, page and media item in WordPress is saved in its own database rather than in specific files. Now every time, the end user calls for these posts, the reference path is carried forward to the server where you wordpress installation is hosted. WordPress will search for the database host, connect to the table of files and subsequently looks up for the requested content.
[Tweet “Put simply, “error establishing a database connection” would mean that WordPress has been unable to find this database on its server.”]This become a serious issue because it isn’t just the posts that are called via the URL but also the system settings, user data, themes, widgets, plugin data and a whole lot of information that needs to accompany the content in WordPress. If WordPress doesn’t find any one of these, it will be unable to show your requested pages and thus the message will be displayed.
There are a number of reasons for this database error message to show up, but the common reasons are:
- Corrupt files
- Corrupt database
- Incorrect credentials of the database
- Database server overload and/or malfunctioning
These are the reasons why expert WordPress developers always choose to backup their files using tools like VaultPress. Further, a lot many WP plugins are available to back things at your end. However, if you are already locked, things get a little more difficult. The next step will be to use your cPanel or alternative hosting management tools to create a complete backup.
Here is the ways to fix Error Establishing a Database Connection
As already mentioned above, there could be a lot of reasons why you have been unable to access your database. Let’s take a deeper look at each of them and the ways to fix it easily.
#1 Corrupt files
Several WordPress users have found that re-uploading core WP files seems to fix issues related to corrupted files. While the exact reasoning for this is still mysterious, but it has to do with replacing corrupt files and resetting the file permissions. Here, it is important to note that you do not touch the config.php file or wp-content directory. The directory will have crucial information like the themes, media and plugins.
Also Read – 6 Premium WordPress Themes for Premium Buyer
The recommended way to go about this will be to download the latest version of WordPress and unzip in on your system. Delete the new config.php and the wp-content directory to ensure that the original files doesn’t get replaced. Now, an FTP client will enable you to upload fresh files to your website’s root folder.
It is always recommended to take backup of your database, wp-config and wp-content files before you install a fresh copy of wordpress.
#2 Corrupt database
A database corruption can be assumed when your WP website’s front end works fine but it becomes difficult to access the backend or vice versa. Sometimes, you may also get error messages stating the unavailability of database tables. Thankfully, WordPress itself has its own tools to resolve the issue. Download the wp-config.php file and add the line:
1 | define( ‘WP_ALLOW_REPAIR’, true );
Once re-uploaded, you may visit http://yoursite.com/wp-admin/maint/repair.php (yoursite.com would need to be replaced with the desired address). You will find a screen showing option to either “Repair Database” and/or “Repair + Optimize Database”.
Once repaired, make sure that you go back to wp-admin.php and remove the added line of code. If you will not remove the code, anyone will be able to access the repair database page and can play with it.
#3 Incorrect database credentials
This is the most common reason for database connection errors in wordpress. To solve this issue; it is necessary to understand 4 important attributes that is require to establish database connection from your wordpress host to server mentioned below:
Database name: This is the name of database you or your hosting provided is created for your website.
Database username: This is database user you / your hosting provider will generate at your server. Make sure that users assigned to specific database must have sufficient rights to access the database it associated with.
Database password: Make sure the password you use is exactly the one which is provided in your host manager.
Database host: Host name of your web server (If you are working on local environment, localhost can be used. Better you ask your hosting providers to provide exact host name to use for wordpress installation).
The best bet for fix the issue will be to copy the credentials given to you and paste them directly. In case, you have done a database switching, double check your “database host” credentials. Note that several hosts would be using local hosts like HostGator or Bluehost and even IP addresses in some cases like SiteGround. Another case could be a different URL altogether, like DreamHost.
If you are having cPanel access of your hosting server, you can create database, user, password and can get host details by accessing MySQL database section.
Don’t forget to add a user to a Database!
Once you have specified information, the most convenient way to access wp-config.php file is via ftp client. Download the file, edit and re-upload the same and its done. Use only a text editor for this purpose. The information should look like this:
define('DB_NAME', 'database_name');
/** MySQL database username */
define('DB_USER', 'database_user');
/** MySQL database password */
define('DB_PASSWORD', 'database_password');
/** MySQL hostname */
define('DB_HOST', 'database_host');
Just change the values and you should be up and running. Ensure that you have done a double-check of the information you uploaded.
If the above steps still shows you database connection error then check the database user permissions. Most users failed to notice this thing while adding user in database and it causes error even if they follow all recommended steps correctly.
#4 Database server overload and/or malfunctioning
If you are looking to differentiate between a malfunction and overload, the hosting company will be the right help. Ask them directly as to what’s going on. The differentiation gets even more difficult if you are on a shared server. In this case, you server might be malfunctioning because another website hosted on the same server is experiencing an overload.
The good thing however is that both issues can be resolved easily and it doesn’t even take long. In case of an overload, the server automatically reboots and all will be reset in few minutes. If the problem continues, you can be better off with catching plugins like W3 Total Cache or WP Rocket or WP Super Cache.
This been said, it is always recommended that you host with a managed server rather than a shared one. Managed hosts are much more flexible and give you a lot of option when it comes to maintenance. “Error Establishing a Database Connection” in most cases is however reasoned best by user generated errors. Take help, talk to the host and review your site. WP is easy as long as you understand it.