How to fix “Error establishing a database connection” WordPress Multisite

How to fix “Error establishing a database connection” WordPress Multisite

Suppose, you display an “Error establishing database connection” warning on a blank page. It’s a WordPress Multisite on the local server. You can solve the error by establishing database connections in the multisite WordPress follow anyway. Don’t worry! If you follow some steps, you can easily solve database connection errors. 

When WordPress cannot establish a connection with the database, this error occurs. Troubleshooting WordPress database connections can be challenging for novices due to the multitude of factors that might impact them.

Continue reading to learn more about this problem, how it occurs, and how to correct it. Once your site is back online, I advise working your way down the list of solutions, starting at the top. In this article, we will show you how to easily fix the error establishing a database connection in WordPress.

What Happens to WordPress Error Establishing Database Connection?

Incorrect database information in your WordPress settings, a damaged database, or an unavailable database server can all be the source of the “Error establishing a database connection” problem.

One piece of software that facilitates the storing, organizing, and retrieval of data into other software is a database.

As a content management system, WordPress uses a database to store all your content and other website data. It then connects to the database each time someone visits your website.

WordPress needs the following information for connecting to the database:

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

The wp-config.php file in your WordPress configuration is where this data is stored.

1. Before You Get Started: Back up and Restore!

It’s a good idea to backup your complete site as-is just in case before you make any modifications to it in an effort to fix this mistake. A backup of a somewhat malfunctioning website is preferable to having none at all. At least you can go back to where you started if things grow worse.

2. Deactivate Your Plugins

Sometimes, a plugin could make a change to your database that ends up breaking your site, such as if you have a security plugin that changes your database prefix or file permissions, for example. If your other plugins don’t expect this to happen, it could cause a malfunction and your site would go down.

2. Checking Your Credentials

Verifying Your Site’s URLs

If you haven’t been able to get your site back up yet, check the details you have written in your wp-config.php file and compare them to the information for your database. Fortunately, you don’t need to be able to access the backend of your site.

In cPanel, you can find your wp-config.php file by going to Files > File Manager and navigating to the directory where your site is located. Click on the wp-config.php file on the list, then click Edit at the top of the page.

There are a few things you need to look for, and you should also make a note of them, so you can compare what you find here with what should be the same details in your database.

First, take a look through the wp-config.php file and see if you can find something similar to the example below:

define('WP_HOME','http://your-site.com');
define('WP_SITEURL','http://your-site.com');

You may not see these two lines because you need to add them in yourself, but if you added them previously, then you need to make note of them and make sure they’re correct. If they’re not, correct them right away.

To correct the URLs, substitute http://your-site.com in the example above for the correct URLs of your site. Also, be sure it’s properly structured and doesn’t have any typos. If you have an SSL certificate enabled on your domain, you should replace http:// with https://

You also need to pay close attention to whether the URLs that you found have www. included. If your database includes a URL without it or vice versa, then this needs to be changed to match your database.

4. Checking Your Database Details

Incorrect database credentials are the most frequent cause of WordPress database connection errors. This is probably the case if you recently switched hosts for your WordPress website.

The wp-config.php file contains the database credentials for your WordPress site. Important WordPress settings, including database information, are contained in the WordPress configuration file.

Check out our tutorial on editing the wp-config.php file in WordPress if you haven’t done it previously.

// ** 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');

4. Check your multisite configuration for WordPress

Initially, we involve identifying whether or not our website is multisite.

We’re following two methods to check multisite is enabled and solutions:

  1. Open wp-config.php file
  2. Open .htaccess file

Method 1: wp-config.php file

Open the wp-config.php file editor once more, and make sure that “WP_ALLOW_MULTISITE” is defined. If it isn’t declared, you can view the code block below by pressing the keyboard shortcut ctr + f (or cmd + f on a Mac) and pasting the code “WP_ALLOW_MULTISITE.”

define('WP_ALLOW_MULTISITE', true ); 
define('SUBDOMAIN_INSTALL', false); 
define('DOMAIN_CURRENT_SITE', 'localhost');
define('PATH_CURRENT_SITE', '/wordpress_test4/');
define('SITE_ID_CURRENT_SITE', 1); 
define('BLOG_ID_CURRENT_SITE', 1);

After checking the multistep, we’ll disable this code “WP_ALLOW_MULTISITE” and add new line of code “MULTISITE”

Remove this code or disable it:

define('WP_ALLOW_MULTISITE', true);

Add a new line of code:

define('MULTISITE', true);

Method 2: .htaccess file

Check that the code below for multisite was enabled or not by opening the .htaccess file in our editor.

RewriteEngine On 
RewriteBase /wordpress_test4/ 
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin 
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^ - [L] 
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L] 
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L] 
RewriteRule . index.php [L]

If you follow this step for multisite, it will be solved.

5. Ask for help

In the event that all else fails, you might need to get in touch with your site host. Any reputable WordPress hosting provider will assist you in troubleshooting the issue, send you in the proper direction, or even resolve it for free.

For affordable prices, you can also employ WordPress developers through a platform like Seahawk to assist you in resolving this problem. The best aspect is that Seahawk has a committed group of knowledgeable professionals, so you can be sure they are reliable.

If you like this article, please subscribe to our YouTube channel. We have WordPress video tutorials. You can also connect to our Twitter and Facebook accounts to keep abreast of the latest updates, news, and more offers.

next3offload
Golam Mostafa
Written by

Golam Mostafa

I'm a senior plugin developer for WordPress. I have four years of experience working as a software engineer for ADB Bank, and in 2018, I started developing plugins and founded ThemeDev.

Table of Content

Table of Contents