Tutorials for web designers and developers
Tutorials for Developers_
Search
Close this search box.

How to fix Establishing a Database Connection in WordPress?

Shabnam Rafique Bajwa

Shabnam Rafique Bajwa

Shabnam Rafique Bajwa is working as a Content writer at HFarazm. Her educational background is in Software Engineering from the University of Lahore and Masters in Software Engineering from Superior University.
database error

You suffer from multiple web errors in WordPress due to a large number of plugins and theme conflicts, you might have faced this error of “establishing a database connection in WordPress”. If you are a WordPress beginner this could extremely frustrate you, when you don’t make any changes in your site. In this article, we are going to discuss what are the main reasons for error establishing database connections in WordPress and how we can solve this issue. But first, see how it looks when it occurs on your screen(screenshot below)

error establishing database connection wordpress

Why we get establishing a database connection error in WordPress?

You get this error when WordPress is unable to establish a connection with the database. There are several reasons why a database connection error occurs We can resolve them by the following methods:

  1. Checking the wp-config file
  2. Check Your Database Host Information
  3. Repair WordPress Database
  4. Check your Web Host (MySQL Server)
  5. The issue with the site URL.
  6. Server temporarily unavailable

Let’s discuss them in detail.

1. Checking the WP-Config file

One of the most important files in your complete WordPress installation is wp_config.php. This is where you add all the details of the WordPress connection to the database. If you have made any changes to your root password or the database user password then you have to change this file too. WordPress needs the following information for connecting to the database:

  • Name
  • Username
  • Password
  • Database Server

This information is stored in your WordPress configuration wp-config.php  file.

You can check your name, username, and passwords and verify them:

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

Verify all the things and if everything is correctly mentioned there, then maybe its an issue with the database host.

2. Check Your Database Host Information

If you checked your database name, username, and password then make sure that you are using the correct database host information. There are most WordPress hosting companies that use localhost as database host. But some other WordPress companies use separate servers to host databases. In that case, you need to connect your WordPress hosting company to confirm your database host information.

3. Repair WordPress Database

You may get a different error on wp-admin, something like one or more database tables are unavailable the database may not be repaired, then you need to repair your database. You can repair your database by adding the following line to your wp-config.php file

define('WP_ALLOW_REPAIR', true);

After adding the above line in wp-config you have to visit your site homepage and WordPress will prompt you to repair the database.

If this doesn’t work for you may be a problem is somewhere else. Move to the next solution.

4. Check your Web Host (MySQL Server)

If you have checked all the above things and they are all ok and still you can’t connect the database, then your database server may be down. This could happen when there is heavy traffic on a server or when your host server cannot handle the load. Your site will get slow and some of your users may get this error. The best solution is, to call your hosting provider and ask them is their MySQL server is responsive or not. If you don’t have any other site running on the same hosting account, then simply open your hosting dashboard and access phpMyAdmin and connect to the database. If you get connected then there is a need to verify that your database users have enough permissions. Just create a file testconnection.php in the root directory and add the following code to it:

$link = mysqli_connect('localhost', 'username', 'password'); 
if (!$link) { die('Could not connect: ' . mysqli_error()); } 
echo 'Connected successfully'; 
mysqli_close($link);

Make sure to replace the username and password. You can now upload this file to your website and access it via a web browser.

Note: If you are trying all these methods to solve the problem make sure that you have a backup. So, if any problem occurs you can get your actual site back.

5. The issue with the site URL.

If you face a problem of establishing database connection it might possible that it is because of the issue in site URL. You can resolve this by running the following SQL query in PHPMYADMIN:

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

This will fix your issue of establishing a database connection in WordPress.

6. Server temporarily unavailable

Most of the time it happens when your server is temporarily down. In this case, you need to contact your hosting or wait for the database to reappear automatically. This is only applicable when you have made no changes to the site and it automatically gave an error of establishing a database connection.

I hope this article helped you in fixing the issue of establishing a database connection error in WordPress. If you feel any difficulty in resolving the issue or you have any questions regarding this article you may comment below.

Thanks.

Leave a Reply

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