2

Something I'm confused about, trying to change the database in an existing WP install, for example:

I have a local WP site (in XAMPP) working fine off a DB called database-A, however if I change, in wp-config:

define('DB_NAME', 'database-A');

To:

define('DB_NAME', 'database-B'); (just another WP DB)

I get:

Error establishing a database connection

But if I export database-A into a .sql file, and empty out database-B and then import the database-A .sql file into database-B, the site works.

So what's validating the data to the WP install to make it work, and why won't database-B just work with the WP install?

Things I've tried:

  • Changing the Authentication Unique Keys and Salts in wp-config.php
  • Changing instances of the site host name in the database
  • Deleting cookies

closed as off-topic by Tom J Nowell Jul 23 '18 at 16:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Why do you need to do this? Have you considered a multisite install instead? – Tom J Nowell Jul 20 '18 at 15:39
  • This is a multisite install, it's more to gain an understanding of how the connection between DB and install is working – Jelnet Jul 20 '18 at 15:46
  • Is it just the Error establishing a database connection message? Or are there other things displayed? There is more than 1 location in the WP codebase with that error message, but without knowing more context it's hard to tell which one it is. Have you ran this with WP_DEBUG then checked the PHP error logs? Without WP_DEBUG the connection function that gets called is called with a @ operator and any messages are silenced – Tom J Nowell Jul 20 '18 at 15:55
  • Additionally, can you confirm that both databases contain the same tables with the same prefixes? – Tom J Nowell Jul 20 '18 at 16:00
0

Reasons this might happen:

  • When WP checks the tables exist and they don't
  • If the DB connection fails
  • The database is unreachable
  • A DESCRIBE query on the tables fails, be it permissions or access based
  • If there is an error recorded in the wpdb object while setting up initial variables in wp_set_wpdb_vars
  • When ms_load_current_site_and_network returns false
    • if get_networks returns false, so there is no current network
    • When if ( empty( $current_site ) ) { is true, indicating it could not figure out the current site
    • When } elseif ( 0 === strcasecmp( $current_site->domain, $domain ) ) { is true

So, you can't just swap out 1 multisite database for another, because:

  • They need the same table prefixes, salts, etc
  • All the sites in the sites tables need to be present or it won't be able to find the current site
  • It needs to be able to find the current network ( all multi-sites are also multi-networks, but there's no network UI )
  • Both databases need appropriate authentication details

Right now, it's near impossible to tell from your question which one is were you tripped up, and it's possible there are more places. If you manage to get it working though, my first recommendation would be to stop doing it.

It's a great way to gain technical debt, complicate things, make them more fragile, and stop standard deployment and migration tools from working properly.

I have a suspicion that your database names might be called production and staging in which case, this is not the appropriate path to take to solve switching between the two

  • Hi Tom, thanks very much for your detailed reply and help. The reasons for my experimenting with changing the DB on an install is testing while trying to debug an issue where I can't get a downloaded site from WPEngine running locally. Unfortunately the downloaded install won't give me a debug.log file despite me setting the WP_DEBUG and WP_DEBUG_LOG constants to true in the wp-config file, I don't know why this is, thus I'm just trying to eliminate factors in the DB connection. I shall try downloading the install again in case corruption has occurred and update later if I find a fix – Jelnet Jul 23 '18 at 8:46
  • @Jelnet that sounds like an issue with a local dev environment, WP_DEBUG and WP_DEBUG_LOG can only do so much to override what's in php.ini, it could even be that your local PHP version is much older than what's on WP Engine. Eitherway that's a different question – Tom J Nowell Jul 23 '18 at 11:45
  • Hi thanks again. What I've discovered so far is that if the DB connection fails due to say a bad password, then the WP error log gets created/updated. However one the connection details are in place I get the "Error establishing a database connection" message but no corresponding entry in the error log. This is using a recent WP build with a copy of the site from live. – Jelnet Jul 23 '18 at 13:40
  • @Jelnet I'm afraid then there's not much more that can be figured out just from what's posted here, have you considered using backups instead of multiple databases? It still sounds like what you're actually dealing with and what you asked as your question are not the same, and now you've switched from what you asked to your original actual problem – Tom J Nowell Jul 23 '18 at 16:41
  • Hi Tom yes you're right my problem has changed a bit, so maybe I should make a new post. My issue now is that I simply cannot get a local version of a site working. I checked on WP Engine and saw the version of PHP was 5.2.x and so have a similar version running locally. I've replaced every instance of the site url with the local equivalent but just get "Error establishing a database connection" with nothing in the debug. – Jelnet Jul 24 '18 at 14:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.