WordPress Multisite “Error establishing a database connection” in localhost

WordPress Multisite “Error establishing a database connection” in localhost

If WordPress displays an “Error establishing database connection” warning on a blank page and It’s a WordPress Multisite. Don’t worry! If you follow some steps, you can easily solve WordPress Multisite Database Error.

Most often, this issue occurs as a result of incorrect database login information, a mistake made during site installation or migration, or faulty database tables. You’ll be relieved to learn that it’s typically one of the easier WordPress mistakes to fix, though.

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.

What Is This Error, and How Does It Happen?

Your site’s files and database can be thought of as its two main components. The majority of the information displayed on your website, including usernames, passwords, posts, categories, tags, and other content, is stored in your MySQL database.

Your database transmits this information to your PHP files, which are designed to ask for it from your database. Then, both sets of data are combined so that your website can be shown when a browser requests the page to load.

PHP files can ask your database for information if something is incorrect, however, if your database doesn’t provide the needed data when asked, you’ll get the following error:

Beyond not being able to connect to your database, here are more specific reasons behind what may have caused this error:

  • You may have made an error installing WordPress or migrating it
  • The database may have been deleted by accident
  • You could have the wrong credentials entered
  • There’s a conflict with the database prefix
  • Your file permissions could be set incorrectly
  • Your site URL may not be correct
  • You may have plugins or themes installed that are causing the issue
  • Your files or database may be corrupted
  • There could be a malfunction with your server or it’s overloaded

Sometimes the error could come up after making some changes to your site that seem innocent enough until they take everything down. That’s why it may be helpful to mention that testing changes before you publish them could help you avoid this situation in the future.

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:


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

Go back to your wp-config.php file and look for this block of text that you can find toward the top of the document:

// ** 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. WordPress to a Multisite Config


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);


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]

You need to add to your wp-config.php:

define('MULTISITE', true);

I had the same problem. I skipped this line because I already had:

define('WP_ALLOW_MULTISITE', true);

 in my wp-config and it looked to me the same.

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

Golam Mostafa
Written by

Golam Mostafa

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

Table of Content

Table of Contents