How to Fix the Error Establishing a Database Connection in WordPress?

How to Fix the Error Establishing a Database Connection in WordPress?

Is your WordPress website showing the warning “Error establishing a database connection”? It’s a fatal error that blocks users from accessing your WordPress website.

When WordPress is unable to connect to the database, this error occurs. A variety of factors might influence your WordPress database connection, making it tough to troubleshoot for beginners.

In this article, we’ll teach you how to easily solve the issue in WordPress when looking to create a database connection.

What Causes is Error Establishing a Database Connection in WordPress?

Incorrect database information in your WordPress settings, a corrupt database, or an irresponsive database server can all cause the ‘Error establishing a database connection error.

How to Fix the Error Establishing a Database Connection in WordPress
Image Source-Google

A database is a component of software that makes it simple to store, organize, and retrieve data for use in other software.

WordPress utilizes a database to store all of your content and other website data as a content management system. Each time someone visits your website, it connects to the database.

WordPress needs the following information for connecting to the database:

  • Database name
  • Database username
  • Database password
  • Database server

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

If any of these items are incorrect, WordPress would fail to connect to your database server, and you’ll see the ‘Error establishing a database connection error.

One of the most common WordPress errors is this. This error can also display if the database server is offline or the database files are corrupt, in addition to incorrect credentials.

Let’s look at how to troubleshoot the problem establishing a database connection issue in WordPress step by step.

WordPress-Care-Maintenance

1. Check Your WordPress Database Credentials

Incorrect database credentials are the most common reason for database connection errors in WordPress. This is most likely the case if you have just moved your WordPress site to a new host.

The wp-config.php file stores your WordPress database credentials. It’s the WordPress configuration file, and it’s where you’ll find all of your important WordPress settings, including database information.

If you have not edited the wp-config.php file before, then take a look at our guide on how to edit the wp-config.php file in WordPress.

The following lines should be found in the wp-config.php file.

// ** 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 must be sure that the database name, username, password, and database host are all correct.

This information can be verified through the dashboard of your WordPress hosting account. Simply connect to your hosting account and go to the database area to find MySQL databases.

How to Fix the Error Establishing a Database Connection in WordPress
Image Source-Google

This will take you to your hosting dashboard’s database management page. You may get the name of your database and the username from here.

Image Source-Google

The database users are listed below, together with links to change the user password.

Image Source-Google

You can change your database name, username, and password in your wp-config.php file if needed once you’ve confirmed your database name, username, and password.

After that, go to your website to verify if the database connection problem has been resolved.

If the error is still visible, it means that something else is incorrect. For more troubleshooting tips, keep on reading.

2. Check Your Database Host Information

If you’re confident that your database name, username, and password are correct, you may want to make sure that you are using the correct database host information.

Localhost is the database host used by most of WordPress hosting companies. Some managed WordPress hosting companies, on the other side, host databases on different servers. Your database host information will not be localhost in that case.

To confirm your database host details, contact your WordPress hosting company.

3. Repair WordPress Database

If you see a different issue in wp-admin, such as “One or more database tables are unavailable,” here is what you should do. If you see the message “The database may need to be repaired,” you should repair your database.

Add the following line to your wp-config.php file to do this. Make sure it’s right before ‘That’s it, no more editing!’ wp-config.php wp-config.php wp-config.php wp-config.php wp

define('WP_ALLOW_REPAIR', true);

Once you’ve done that, go to this page: http://www.yoursite.com/wp-admin/maint/repair.php to see the settings.

How to Fix the Error Establishing a Database Connection in WordPress
Image Source-Google
Note:- To access the database repair page, the user does not need to be logged in. Remove this code from your wp-config.php once you've finished repairing and optimising your database.

4. Check if Your Database Server is Down

If everything seems to be in order but WordPress still can’t connect to the database, it’s possible that your database server (MySQL server) is down.

This might occur as a result of high traffic on a server. Basically, the load on your host server is too much for it to handle (especially when you are on shared hosting).

Your site will become extremely slow, and some users may see an error. As a result, the best thing you can do is call or chat with your hosting provider and ask if your MySQL server is responsive.

Whether you have other websites on the same server as your SQL server, you may check those sites to see if your SQL server is down.

If you don’t have any other websites on the same hosting account, go to your hosting dashboard and try to join the database with phpMyAdmin.

If you’re able to link, we’ll need to see if your database user has enough permissions. Make a new file named testconnection.php and paste the code following into it:

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

Replace the username and password if needed. This file may now be uploaded to your website and accessed with a web browser.

If the script is connected successfully, that means your user has enough permissions, and something else is wrong.

Go back to your wp-config file to make sure that everything there is correct (re-scan for typos).

Other Solutions That Have Worked for Users

If the above troubleshooting steps failed to resolve the database connection error on your website, you may attempt the following steps.

These steps, according to our users, have helped some people fix the database connection error on their websites.

1. Update WordPress Site URL

Using phpMyAdmin, try changing the URL of your WordPress site. Simply pick your WordPress database in phpMyAdmin from your hosting account dashboard.

How to Fix the Error Establishing a Database Connection in WordPress
Image Source-Google

Then, at the top of the page, select SQL and type the MySQL query below.

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

Ensure to supply your own site URL and change wp options to your own table name if the WordPress table prefix has changed.

2. Rebooting Web Server

Rebooting dedicated servers, local servers, and virtual private servers (VPS) is an option for users.

This will restart your web and database servers, which may solve some of the error’s temporary issues.

WordPress-Care-Maintenance

Buy Hosting For Website

Hostguid listed many hosting plans so you can easily compare many hosting companies hosting packages for your website.

Join Our Telegram Group

Upcoming hosting and SSL offers and news updates here. We can try to solve your doubt regarding any problem like Domain, Hosting, SSL certificate, etc. And News update here.

Share your love
Host Guid
Host Guid
Articles: 140

Leave a Reply

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