开发者

Can I merge two databases into one in Mysql if they both have the same schema?

I have two databases in MySQL that already have data. they have exactly the same schema. I wo开发者_开发问答uld like to merge those two databases into one only. I tried:

mysqldump -u root -p --databases database1 database2 database3 > database1_database2_da

However, when I try to open database1_database2_da, I only end up with data from one of the database, but not all of them. I also want to mention that the two databases have their records starting at 1, since they are automatically generated. Do you think there is a way to merge those two databases without losing data from any of them?


Run mysqldump on each database with the --no-create-info option to avoid writing schema information. Then run once on one database with the --no-data option. If you load all of these files sequentially into the same target database, this should work, barring any differences in schema between the two databases or duplicate primary keys.

mysqldump -u root -p --no-create-info database1 > database1.sql
mysqldump -u root -p --no-create-info database2 > database2.sql
mysqldump -u root -p --no-data database1 > schema.sql

After creating a new database, run

mysql -uroot -p -Ddatabase3 < schema.sql
mysql -uroot -p -Ddatabase3 < database1.sql
mysql -uroot -p -Ddatabase3 < database2.sql

This may also work. Don't have a Windows box to test on ATM

type schema.sql database1.sql database2.sql | mysql -uroot -p -Ddatabase3
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜