Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

mysql

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?

like image 389
T3000 Avatar asked Jun 29 '11 20:06

T3000


People also ask

How to merge two databases with different schemas?

If you have two database with same schema then you may want to use MySql database with —no-create-info option. Then you can use it to load it into the target database. Also you can try Sql-Hub to merge databases. If you have database with different schema then you can simply use mysqldump and then go ahead with surgical merge.

How do I merge two tables in MySQL?

In MySQL, merge can be performed with UNION, INSERT, or even using JOINS on two or more tables to combine the data values on the basis of UNIQUE key or PRIMARY key. We will create two tables in the database named Products and Products_Info that will contain information of products.

How to merge two databases in phpMyAdmin?

In the Databases section, click the phpMyAdmin icon. Click the SQL tab at the top. You will see where it says, Run SQL query/queries on server "localhost":. Insert the following code in the text box below, but replace DB1 and DB2 with the database names. Also, replace TABLE1 with the table name you are trying to merge. Click the Go button.

How to merge two MySQL databases using SSH?

From SSH, you need to type the command to access MySQL. Here is the format, but replace MYNAME with your username and PASS with your password. Now type the following code, but replace DB1 and DB2 with the database names. Also, replace TABLE1 with the table name you are trying to merge.


1 Answers

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
like image 174
Michael Mior Avatar answered Nov 03 '22 22:11

Michael Mior