Have you experienced an error when establishing a database connection in WordPress?
You are not alone. Unfortunately, many WordPress users face one of the most recurring errors, “Error establishing a database connection,” regularly.
This error indicates that the website cannot communicate with the WordPress database. Consequently, the website is unavailable.
As a WordPress site owner, you should not ignore this issue at any cost. It can negatively affect your website performance from the traffic, conversion, and sales perspective.
Read this guide thoroughly to bypass the issue and help your WordPress website up and running quickly.
What Do You Know About “Error Establishing a Database Connection”?
When WordPress does not connect to the database, a message saying “Error establishing a database connection” pops up on your screen.
When a user visits your website, the specific PHP commands fetch the website information. As a result, the user’s browser shows this website information.
The message “Error establishing a database connection” appears on the blank page as there is no data to retrieve and display on the browser. Website admins cannot access the WordPress dashboard.
Luckily, the error does not affect the front end of the website, provided the website’s cache is available.
What Are the Causes Of The WordPress Database Connection Error?
The WordPress database connection error may occur due to the following reasons:
- Your database login credentials (username and password) are incorrect.
- You have installed a poorly coded plugin, so your database has been corrupted. Thus, you should fix the WordPress database connection error after the plugin update.
- Your website needs more server capacity to manage a heavy spike in traffic.
- Your WordPress web hosting is having issues, and the database is not working correctly.
Before we go into the methods of fixing the establishing a database connection error, here are some key concepts that you need to keep in mind:
What is a Database?
A database acts like software that helps users store, manage, and retrieve data into other software without issues.
WordPress connects to the database and heavily relies on it when storing a website’s content and other valuable data. WordPress requires the below-mentioned data when connecting to the database:
- Database name
- Database username and password
- Database server
A WordPress configuration file named wp-config.php stores this information.
You can include this error establishing a database connection issue in the most common WordPress errors list.
What is phpMyAdmin?
phpMyAdmin is a software you can use without any cost. The tool, primarily written in PHP, allows users to manage MySQL or MariaDB database servers smartly.
Through phpMyAdmin, users can conduct numerous administration tasks like database creation, user account addition, and running queries.
How to Fix the Error Establishing a Database Connection in WordPress?
Here are different ways or methods to help you overcome the problem of errors in establishing a database connection. These methods are:
1. Verify Your Database Login Credentials
The WordPress database credentials are stored in the wp-config.php file. If you need to learn about the wp-config.php file, we explain its function.
In the wp-config.php file, crucial information, including WordPress settings and database information, is stored.
You need to look for the below-mentioned lines in the wp-config.php file:
- MySQL settings (obtain this information using 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’).
In short, the information such as database name, username, and host must be correct. For that reason, you can navigate to your WordPress hosting account dashboard and log into your hosting account.
In our case, we have used the WP Engine dashboard as a reference.
Through the database section, click MySQL databases. Consequently, you will see a database management page on your screen, allowing you to discover your database name and username.
Furthermore, you can find the database users and links to change the user password. After confirming the database name, username, and password, you can modify this information in the wp-config.php file if required.
You can visit your website to see if this error exists. If the problem persists, the problem lies elsewhere.
2. Review Database Host Information
It is important to review your database host information. You will frequently encounter the WordPress database error if your database host information is incorrect.
You must contact your WordPress hosting provider for your database host information. That’s because, in some cases, WordPress hosting companies use separate servers when hosting databases.
However, various WordPress hosting firms utilize localhost as a database host. If this is the case, your database host information will be localhost.
You must check your database host information to successfully restore your WordPress database connection.
3. WordPress Database Needs to be Repaired
You may likely experience a different error, for example, “One or more database tables are unavailable. The database may need to be repaired”.
Therefore, you should repair your database as early as you can.
Instruction: Add the following line into the wp-config.php file just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php:
- Define (‘WP_ALLOW_REPAIR’, true);
After adding this line, you should visit the following page to check the settings:
http://www.yoursite.com/wp-admin/maint/repair.php
You do not need to log in to access the database repair page. Don’t forget to remove this code from your wp-config.php once you repair and optimize the database.
4. Check Your Database Server
There is a chance you might see an “Error establishing a database connection” message on your screen if your database server (MySQL server) does not work correctly.
If your host server cannot manage the load due to heavy traffic, you will see the above-described error. You must contact your hosting service company to see if your MySQL server works fine.
You can check your MySQL server by visiting websites on the same server.
If the server is not hosting other websites, you can access your hosting dashboard, access phpMyAdmin, and connect to the database.
Once connected, ensure that your database user has sufficient permission. You should create a new file named testconnection.php and paste the following in the same file:
<?php $link = mysqli_connect('localhost', 'username_here', 'password_here'); if (!$link) { die('Could not connect: ' . mysqli_error()); } echo 'Connected successfully'; mysqli_close($link); ?>
You must remember to change the username and password with your actual MySQL username and password. You can then upload the file to your site.
Once you have uploaded the file, you can access it through the web browser. Your user does have sufficient permission if the script is successfully connected. You can recheck your wp-config settings to confirm everything is fine.
5. Refresh Web Server
You can refresh or reboot your server using a dedicated server, local server, or virtual private server (VPS).
This step will allow you to restart your web and database server. Hence, you can bypass the temporary glitches that result in the WordPress database connection error.
6. Contact Your Hosting Service Company
At times, contacting the hosting service company may help you appropriately eliminate the WordPress database connection error.
They can provide timely solutions to your queries and guide you regarding issues like heavy traffic, restrictions on the database server, etc.
Reliable and results-driven web hosting services may even resolve the error of establishing a database connection in WordPress for their customers.
Wrapping Up
We hope you can now resolve the error by establishing a hassle-free database connection issue in WordPress. Once you follow the suggestions mentioned above, your existing and potential website visitor will not see this error on their screens.
Lastly, create a website backup before fixing the WordPress database connection error.