How to Resolve Error establishing a Database connection

Written by admin

On July 18, 2021
Error establishing a Database connection

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.
Error establishing a Database connection
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.
error establishing a database connection
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:

  1. Database name
  2. Database username
  3. Database password
  4. 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.

define(‘WP_ALLOW_REPAIR’, true); Once you have done it, you can easily see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.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.

UPDATE wp_options SET option_value=’YOUR_SITE_URL’ WHERE option_name=’siteurl’

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.

You May Also Like…

How to Update wordpress php  version

How to Update wordpress php version

If you are looking to update WordPress PHP version then you come on the right place in this talk we will discuss why we need to Update PHP version of your website if you are not running the latest version of PHP may increase your site load time and the research shows...

How to add Divi inline buttons (Side By Side Button)

How to add Divi inline buttons (Side By Side Button)

Creating a beautiful website is not enough for your business. You must increase your website authority for better ranking. Google has provided a complete list of how you can increase your website authority. One of the most important points is interlinking. Buttons are...

How to create beautiful Carousel in wordpress

How to create beautiful Carousel in wordpress

Do you want your website more engaging and visually appealing for your website visitors? If your website is appealing that you offer on your website may lose your lot of business so the question is how we can make our website more attractive and visually appealing. I...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *