You are currently viewing 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 displaying a ‘Error establishing a database connection’ message? It’s a fatal error that prevents people from accessing your WordPress site.

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 newbies.

We’ll teach you how to repair the error establishing a database connection in WordPress in this article.

What Causes Error Establishing a Database Connection in WordPress?

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

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

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.

For connecting to the database, WordPress requires the following information:

Database name
Database username
Database password
Database server

This data is saved in the wp-config.php file in your Folder.

If any of these elements are wrong, WordPress will be unable to connect to your database server, and you will see an error message stating “Error establishing a database connection.”

One of the most common WordPress mistakes is this. Error Establishing a Database Connection can also display if the database server is offline or the database files are faulty, in addition to invalid credentials.

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

Method 01: Check Your WordPress Database Credentials

Incorrect database credentials are the most frequent cause of “Error Establishing a Database Connection” in WordPress. This is most likely the case if you have just relocated 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 critical WordPress settings, including database information.

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 double-check that the database name, username, password, and database host are all valid.

You can modify your database name, username, and password in your wp-config.php file if necessary once you’ve checked your database name, username, and password.

After that, return to your website to verify whether the database connection problem has been resolved.

If the mistake is still visible, it suggests that something else is incorrect. For additional troubleshooting tips, keep reading.

Method 02: Check Your Database Host Information

If you’re positive that your database name, username, and password are accurate, you should double-check that you’re using the correct database host information.

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

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

Method 03: 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 must repair your database.

You can do this by adding the following line in your wp-config.php file. Make 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 that, you can see the settings by visiting this page:

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 restoring and optimizing your database.

Method 04: Check to see whether your database server is unavailable

If everything appears 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 demand on your host server is too much for it to handle (especially when you are on shared hosting).

Your site will become extremely laggy, 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 inquire if your MySQL server is responsive.

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

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

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

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

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

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

Return to your wp-config file and double-check that everything is in order (re-scan for typos).

Other Options That Have Proven Beneficial to Users

If the prior troubleshooting techniques failed to resolve the database connection problem on your website, you may attempt the following actions.

These techniques, according to our customers, have helped some people fix the database connection problem 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.

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'

Remember to specify your own site URL and update wp options to your own table name if the WordPress table prefix has changed.

2. Restarting the 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 resolve some of the error’s temporary issues.

If everything else fails, you might need to contact your site hosting provider. All reputable WordPress hosting companies will assist you in troubleshooting the issue, directing you in the proper path, or even resolving it for you.

If you liked this article, then please check out our other tutorials.

Leave a Reply