Table of Contents [TOC]
If you getting error establishing a database connection after installing wordpress, your website is unable to connect to the WordPress database. Unable to establish a database connection is a serious error that prevents visitors from accessing your website.
Nothing can tremble a WordPress website owner than seeing a message like – Can’t login to wordpress Error establishing a Database Connection localhost . We all panic after noticing any type of failure that makes our WordPress website go down [eg: “This Account Has Been Suspended” – WordPress Down[Fix]]
If you’re having trouble with WordPress and are unable to authenticate, here are some ways to fix the error with simple explanations. In this article, you will learn ❌ How to Fix Error Establishing a Database Connection in WordPress website and we will also discuss the major reasons for getting this error and how can we prevent it.
Some other common wordpress errors you might want to read about are – Error 504 Gateway Timeout in WordPress, 405 Method Not Allowed Error, Error 404 Page Not Found In WordPress, White Screen of Death Error in WordPress, The link you followed has expired error in WordPress,Are you sure you want to do this Error WordPress, 503 Service Unavailable Error,
All relevant information about your WordPress sites such as post information, pages, plugin settings, and user information is kept in the database. WordPress connects to this database every time someone visits your website.
The database in question does not store media files (images and video) and basic files for plugins/themes such as index.php, wp-login.php, etc.
When someone visits your webpage, PHP executes the relevant code on the page and retrieves the information from the database, displaying it to the site visitor in the browser.
To successfully establish the connection, you need the following information:
This information can be found in the WordPress wp-config.php configuration file. If any of these settings are incorrect, WordPress will not be able to connect to the database.
In this case, the browser window will display the Database connection error notification.
Every bit of information present on your WordPress website is stored in your MYSQL database. This includes your posts, pages, layout design, widgets, plugins themes etc. In simple words, every bit of information about your website is stored in your database.
When you get an error like this, it means your website is no longer communicating or has access to your database, and thus your entire website goes down. As a result, no content is visible to the user. A blank white screen with a message ” connection is not working properly” is displayed whenever any user tries to connect to your website.
This is a kind of HTTP Error Code 500 which means ” Server error codes indicating that the request was accepted, but that an error on the server prevented the fulfillment of the request.”This same status code appears when there is an “internal server error“.
It essentially means that something went wrong on the server and the requested resource was not delivered. When everything is working properly your site will generate a 200 HTTP status code, which means everything is fine.
“200 Status Code which means “Everything is OK.” This is the code that is delivered when a web page or resource acts exactly the way it’s expected to.”
Related Issues – WordPress Stuck in Maintenance Mode | Parse Error Syntax Error Unexpected in WordPress
Here are a few common reasons explaining why this error exactly occurs on a WordPress website:
The most common reason for getting this kind of error is the incorrect database login credentials. There are no similar credentials for your WordPress website login and database login. A database login requires separate credentials which should be too strong to guess. If there is any change in username or password, then WordPress will not be able to fetch any data from the database.
What if your database is corrupted? Yes, this could be the reason behind the error. We all have some plugins and themes of older versions and are no longer in use. But no one takes out time to deactivate or delete them. Now, this is how loopholes occur and let the hackers easily hack your website.
Another possibility could be an unsuccessful update of your WordPress database which includes missed plugins, themes or even failure in WordPress software update.
This might be possible that your WordPress installation has certain corrupted files which are creating the error in connection with the database. Such files might be the loopholes or backdoors placed by the hackers in order to regain access to your website.
Your database server may have some issues which require quick resolving. A number of things may go wrong with your database server like
This type of problem generally occurs due to shared hosting where a number of users are hosted on the same server.
Before initiating how to fix this database connection error, we recommend our readers to take a backup of your entire site along with the database. Make this a habit whenever you try to fix any error on your WordPress website.
There are a few plugins which automatically take a backup of your site every day so that you don’t need to have a manual setup every time you update your website or make any changes to it.
You can use WordPress backup plugins such as UpdraftPlus or Duplicator to backup both your files and your database.
???? Now, let’s start with Troubleshooting.
The major reason discovered behind the occurrence of this message is incorrect login credentials used to log in the database. This happens for most of the people who have migrated to a new host. Make sure you are using right credentials.
The connection details for your WordPress site are stored in the wp-config.php file.
The file contains four important pieces of information that need to be correct for a successful database connection.
If any one of these values is incorrect, WordPress will not be able to connect to the database.
Database Name
// ** MySQL settings ** // /** The name of the database for WordPress */define('DB_NAME', 'xxxxxx');
MySQL database username
/** MySQL database username */define('DB_USER', 'xxxxxx');
MySQL database password
/** MySQL database password */define('DB_PASSWORD', 'xxxxxxxxx');
MySQL hostname (server)
/** MySQL hostname */define('DB_HOST', 'localhost');
You can easily locate the wp-config.php file in the cPanel. Simply click on “File Manager,” and browse to the root of your site, and right click to edit the file. There, you have to verify all the input values with the ones on the server to ensure that they are correct.
DB_Name: The first thing to verify is the database name. You can check it through cPanel dashboard. Login into phpMyAdmin and check out the name of your database.
When you click on your database name, a screen listing the names of the tables in the database will appear. Click on the “Browse” button next to the table named wp-options, and see if you find your site name and URL along with other settings.
If yes, and the name matches your wp-config entry, you have the right database. If the URL is right, but your wp-config file is wrong, change the name there to match the right database.
Next, verify the username and password. You can test it via two ways:
Create a PHP test file to verify whether the credentials from wp-config connect you to the database. Paste the following code:
<?php $test Connection = mysql_connect('localhost', 'root', 'password'); if (!$testConnection) { die('Error: ' . mysql_error()); } echo 'Database connection working!'; mysql_close($testConnection); ?>
Add database credentials to the new text file and save it.
Now, type the URL of the file into your browser and try it; you’ll either get the “Connected successfully” message or the “Could not connect” message.
If you didn’t find the username and password in the table, create a new one in the wp-options. Be sure to make changes in the wp-config file as well.
If you don’t know how to add a new user, follow these steps:
Check if now the error is resolved.
If the error still persists, the database might be corrupted. The worst thing that can appear in a WordPress database is missing tables which happens if hundreds of tables are added or removed by new plugins.
This may result in “error establishing a database connection” message displayed on front end whereas ” One or more database tables are unavailable” on the back end.
It is important to note that WordPress has repair mode which you can turn on Simply add the following to the bottom of your wp-config.php file.
define(‘WP_ALLOW_REPAIR’, true);
Later, browse to the following location on your WordPress site:
https://yourdomain.com/wp-admin/maint/repair.php.
You will then have the option to repair the database or repair and optimize the database. Since you are probably troubleshooting an interruption on your site, we recommend operating the repair database option as it is quicker.
Make sure to remove the line of code added to your wp-config.php file after executing the database repair.
You can also execute the repair of your database using cPanel.
In order to run the database repair from within phpMyAdmin, follow these steps:
The second probable cause of getting this error is that your files might be corrupted. This may happen while sharing files through FTP which is not so secure and thus, a hacker can easily gain access to your files. This issue is necessary to fix as quick as possible. Be sure to create a backup before cleaning up the corrupted files.
To do so, follow these steps:
We tried to fix every possibility leading to this error . But if nothing worked that means you need to check your hosting provider because it may be occurring due to an issue with the database server.
As we discussed earlier, if its the case where a number of concurrent connection are utilizing your database at the same time. This can slow down the speed or even lose connections with the database.
In order to minimize interaction with your WordPress database, you can use caching plugins like W3 Total Cache, WP Super Cache, Quick Cache etc.
And, if we talk about shared hosts utilizing same resources on servers, you can fix this problem with a high-performance best managed WordPress hosting, so that it is easy to handle large amounts of traffic to your WordPress sites.
This error means that your website files (on the webserver) cannot connect to your database (on the database server).
Navigate to http://localhost/phpmyadmin.
Click on ‘to create a new database’, and choose a name for your database.
Next, open up wp-config.php in the root of your WordPress folder, and update the four values shown below to reflect your new database.
Every website that connects to a database has a configuration file. This configuration file stores the following database credentials that allow you to connect.
/** The name of the database for WordPress */
define('DB_NAME', 'case sensitive chosen db name');
/** MySQL database username */
define('DB_USER', 'root');
/** MySQL database password */
define('DB_PASSWORD', '');
/** MySQL hostname */
define('DB_HOST', 'localhost');
If the database login information was changed in the panel without updating the connection information in the site configuration file, your site cannot connect to the database.
Although VestaCP is a younger panel than cPanel, it also puts at your disposal a simple and intuitive interface to work with databases. Using it you will be able to fix the “Error establishing a connection to the database” WordPress error.
As with cPanel, start by reviewing the first three possible causes of the error:
By default, VestaCP does not have a file manager like cPanel, but you have several options to access your web files and view the contents of wp-config.php. For example, if you have Optimized VPS you will have a file manager integrated into the panel; but even if that wasn’t the case, you could connect via FTP with a program like FileZilla, or use SSH
Whatever you do, access the contents of the wp-config.php file and check the database name, MySQL username, and password that are configured. For example, over SSH you would see something like this:
Now you have to compare them with the correct ones. Within VestaCP go to the databases section, and click on “ Edit ” for the database you are going to use:
On this screen, you can simultaneously check that both the database name and the MySQL user you use in WordPress are correct, as well as change the user’s password if necessary. If you see any data in the wp-config.php file that is not correct, correct it!
As in cPanel, in a VestaCP environment, it is normal that both the database and the application are on the same server, so the correct name for the database server would be “ localhost ”. But if you think that this is not your case, confirm it with the technical team of your hosting company, they will be able to guide you!
In VestaCP you will not encounter this problem! This panel does not allow you to create a MySQL user without assigning it to a database; This is a little less flexible than cPanel, but it also takes away headaches like this.
If you’re sure everything else is ok, it’s up to you to SSH into the server and runs some tests to make sure everything is ok.
In order to avoid any database connection error on your WordPress website, here are few steps:
Certainly. this error is a matter of concern for a WordPress site owner but we have outlined a few quick ways to fix this error. And, if this does not resolve the issue with your website or you need an expert help, we are always here to assist you.
To get started contact us here. We hope this article helped you learn how to fix error establishing database connection in WordPress easily.