且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果两个数据库都具有相同的模式,我可以将两个数据库合并到一个Mysql中吗?

更新时间:2022-12-11 10:17:04

在每个数据库上运行 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

创建新数据库后,运行

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

这也可以正常工作。没有Windows框在ATM上测试

 键入schema.sql database1.sql database2.sql | mysql -uroot -p -Ddatabase3 


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:

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