Mysql – How to merge two databases (each with different updates) on the same server

MySQLWordpress

I have an almost regular WordPress setup including a mySQL database (Server A). On March 1st 2018 this installation and mySQL database was copied to a new server (Server B). On the new server (B) a lot of things got improved and a new domain name was assigned. Also the prefix of the database has changed.

Between March 1st and now this database (B) did not get any user registrations, however as said above it did get several updates and improvements for pages and general behaviour.

I have successfully used phpmyadmin to export the database from the current installation (A) again (on May 5th) and import it into the new server (B) as a second database.

What should happen now, is that the latest changes of (B) [March 1st + improvements etc.] should be merged together with the latest changes of (A) [March 1st + user registrations and user activities that happend on the current page between March 1st and May 5th].

  1. Can I merge the two databases as a whole (with one command), if the tables are the same (I have not really compared each of the tables and it's columns, but as I have not changed any plugins and all the table names are identical – no table has been added or deleted – I guess I can say the structure is identical?)?
  2. Is there a way of using phpmyadmin to do that?
  3. Should I be using "REPLACE" or "INSERT" or "INSERT … ON DUPLICATE KEY UPDATE" or something completely different? I do not understand what would be correct here.
  4. Once I have managed to mergen the data, what is a good way of updating all URL's to the new domain? In the new installation both databases (A) & (B) still have the wrong URL's.

Many thanks for your help.

On the new server (B) I'm running:

  • Server: Localhost via UNIX socket
  • Server-Typ: MySQL
  • Server-Version: 5.6.39 – MySQL Community Server
    (GPL)
  • Protokoll-Version: 10

  • nginx/1.12.2

  • Datenbank-Client Version: libmysql – mysqlnd 5.0.12-dev – 20150407 – $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $
  • PHP-Erweiterung: mysqli curl mbstring
  • PHP-Version: 7.1.13

Best Answer

(Disclaimer: I'm not familiar with WordPress and I haven't used phpmyadmin very much.)

If there is new data in each database since the "split", i.e. each database has some unique new data not found in the other one, then I don't think there is an easy path for merging them just using standard MySQL commands or tools. There will be records in one database with the same primary key values as in the other database, but with different data in the other columns in those records. If there aren't a lot of such records, then you may be able to merge this "manually", record by record, but it will likely be labour intensive and you need to know what you're doing. E.g. when you give a record a new primary key value to avoid a collision with the other database, you also need to make sure to update every foreign key that refers to it.

That said, there are special WordPress plugins for exporting and importing WP databases, and I'd be surprised if these didn't allow you to merge two databases. See e.g. wp-exporter and wordpress-importer. But that is probably outside of the scope for this site. See also this question on stackoverflow: How do I merge two Wordpress SQL databases in phpmyadmin?