Error establishing a Database connection
Are you facing ‘error establishing a database connection’ on your WordPress website? It is a fatal error and can make your website useless and inaccessible to the users.
The “error establishing a database connection” is one of the most common and alarming errors that WordPress users can experience. This Error means that your website has no longer access to the WordPress database, and thus your whole website goes down.
This error mainly occurs when your WordPress is unable to access or to make connections with your database. Many numbers of things can affect these types of connection failures.
It is a bit difficult for beginners to troubleshoot this error. Today, we’ll discuss some scenarios and find how to easily fix the error establishing a database connection in WordPress.
Some easy ways to get your website back up and running in no time are as follows:
What are the main Causes of Error Establishing a Database Connection in WordPress?
The “error establishing a database connection” can be caused due to incorrect use of database information in your WordPress settings, corrupt database, or an unresponsive database server.
As a content management system, WordPress uses a Database (is a software to store, organize, and retrieve data into other software) to store all your content and other website data.
WordPress needs following information to connect your site with database:
- Database name
- Database username
- Database password
- Database server
This information is always stored in a WordPress configuration file called wp-config.php.
If any of these items remain incorrect, your website will be unable to connect with your database server.
Apart from wrong database credentials, This error may also appear if the database server is down. Or may be due to database files being corrupt.
Let’s discuss how to fix this error establishing database connection.
1. Check Your WordPress Database Credentials
A common cause of WordPress database connection errors is invalid credentials for the database. Your WordPress website may use separate login information to connect to its MySQL database.
Your WordPress database credentials are stored in the file named as wp-config.php. It is the WordPress configuration file that mainly contains important WordPress settings and most importantly including database information.
You have to edit this file first and then start your WordPress site. You see the following format in the configuration file as.
// ** 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 always make sure that the information for the database name, username, password, and database host is correct. You can also confirm this information from your wordpress hosting account dashboard. Simply log in to your hosting account, go to dashboard and click on MySQL databases below the database section.
This will take you to the database management system page in your hosting. From here, you can find out your name and the username of your database. you will also find the database users and links to change the user password.
Once you confirm your database name, username, and password, you can also change that information in your wp-config.php file if you need.
Now, try to visit your website to see if the database connection error has gone or not.
If you still see the error,then it means that there is another issue that you are facing failure in database connection.
Continue reading the next step to find a solution to that issue.
2. Check Your Database Host Information:
If you are confirmed that your database name, username, and password information is totally correct, then you should have to make sure that you are using the correct database host information.
Most hosting companies use localhost as your database host. However, some hosting companies use separate servers to host databases. In that case, your separate database hosting information will not be the same as localhost.
You just need to contact your WordPress hosting company to confirm your database host information.
3. Repair WordPress Database
First we just need to check if our database is okay or not. If you see your website normally and get an error when trying to access the wp-admin page or get a different error saying like “One or more database tables are unavailable.The database may need to be repaired”. Whenever you try to access your website you usually face these messages, these are the clear signs.
Now, you need to repair your database. You can only do this by simply adding the following lines in your wp-config.php file. Make it sure to add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.
4. Check if Your Database Server is Down
If everything seems to be correct, and WordPress continuously shows the same error and cannot connect to the database, then your database server (MySQL server) may be down.
This can usually happen due to heavy traffic on the server. Basically, it is just because your host server can not handle the load, especially when you are using shared hosting.
Your site goes down and cannot be able to respond to some users and also causes some errors. So the most important and best thing you should do is get on the phone or send detailed email to your hosting provider and ask them if your MySQL server is responsive or not.
If you don’t have any other of your websites on the same hosting account, then simply go to your hosting dashboard and you have to try to access phpMyAdmin and connect the database with WordPress site.
If you can connect it, then we have to verify it by creating a new file named as testconnection.php and paste the following code in it:
Replace the username and password. You can upload this file to your website now and access it via web browser.
If the script connects successfully, then it really means that your user has sufficient permission at all, and there is something else that is wrong with it.
Go back to your wp-config file to make sure that everything is correct here or not.
Other Solutions That May Help for Users
Some other solutions are discussed below to resolve that problem.
Update WordPress Site URL
Try to update the WordPress website URL using phpMyAdmin. You can simply access phpMyAdmin from your hosting account dashboard, and select your WordPress database from it.
You should use your own hosting name which you are currently using for your website.
Rebooting Web Server
Users mainly on dedicated servers, local servers, and virtual private servers (VPS) can try rebooting their servers to make them work properly.
This will also restart your website and database server which may also fix some temporary glitches causing that error.
Ask for help
If everything fails, then you have to contact your web hosting company. All good hosting companies will help you troubleshoot the problem that you are facing, point you in the right direction, or even fix it easily for you.
One thought on “How to Resolve Error establishing a Database connection”
Thanks for the good article, I hope you continue to work as well.