如果两个数据库具有相同的架构,我可以在 Mysql 中将它们合并为一个吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6526824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I merge two databases into one in Mysql if they both have the same schema?
提问by T3000
I have two databases in MySQL that already have data. they have exactly the same schema. I would like to merge those two databases into one only. I tried:
我在 MySQL 中有两个已经有数据的数据库。它们具有完全相同的架构。我想将这两个数据库合并为一个。我试过:
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?
但是,当我尝试打开 时database1_database2_da
,我只得到了其中一个数据库中的数据,而不是所有数据库中的数据。我还想提一下,这两个数据库的记录都是从 1 开始的,因为它们是自动生成的。您认为有没有办法合并这两个数据库而不会丢失其中任何一个的数据?
回答by Michael Mior
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
使用--no-create-info
避免写入架构信息的选项在每个数据库上运行。然后使用该--no-data
选项在一个数据库上运行一次。如果您将所有这些文件顺序加载到同一个目标数据库中,这应该可以工作,除非两个数据库之间的架构存在任何差异或重复的主键。
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
这也可能有效。没有可在 ATM 上测试的 Windows 设备
type schema.sql database1.sql database2.sql | mysql -uroot -p -Ddatabase3
回答by Alan Hickman
SQL-Hub (http://sql-hub.com) will let you merge multiple databases with the same schema in to a single database. There is a free licence that will let you do this from the UI though you might need to pay for a license if you want to schedule the process to run automatically. For a one off task with 2 databases you should just be able to do this with the free licence.
SQL-Hub ( http://sql-hub.com) 将允许您将具有相同架构的多个数据库合并到一个数据库中。有一个免费许可证可让您从 UI 执行此操作,但如果您想安排自动运行该过程,则可能需要支付许可证费用。对于具有 2 个数据库的一次性任务,您应该能够使用免费许可证来完成此任务。