简体   繁体   中英

How to make a copy of database to itself in case of multi-tenancy

I have multi-tenant systems wtih mysql database. There is discriminator-based multi-tenancy. Each table contains column tenantId. I have default tenant with tenantId = -1. For this default tenant database contains initial data for all new tenants.

How can I copy all data with tenantId = -1 for a new tenantId in same database? There are a lot of tables in database, so it is very hard to write such script manually.

Main Problem: If I will make dump of db then I can not load this dump to the same db, because primary keys and foreign keys should be new.

You'll want to use the -w (or --where) option in mysqldump. Since all your tables have the tenantId this should work:

mysqldump myDB --where='tenandId=-1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM