synchronize two tables stored in different databases - mysql

I have two tables in different databases. The tables are exactly alike (same name,same columns,etc). My question is, how can I retrieve new rows from parent table and store into the child table? I thought of using mysqldbcompare but it compares Two Databases and Identify Differences but i need to do it on a table.
mysqldbcompare --server1=root:root#192.100.0.0\
--server2=root:root#192.160.0.01 \
inteliviz:inteliviz\
--run-all-test --changes-for=server2 --difftype=sql
how to pass tables in here.
Thanks in advance.

mysqldbcompare can be made to generate the required sql to synchronise the two databases. My approach would be to use the --difftype=sql option to generate the sql required to generate the changed or missing rows. To reduce the surrounding "noise" in the mysqldbcompare report, you could limit the comparison to data consistency checks using --skip-... for all but the data-check option.
Something like:
mysqldbcompare --server1=... \
--server2=... \
--difftype=sql \
--skip-object-compare --skip-object-diff --skip-option-count > report.txt
Alternatively, you could use INSERT IGNORE or REPLACE as per this answer:
MySQL INSERT INTO WHERE NOT EXIST

Related

mysqldbcompare skip some tables

I need to compare 2 mysql databases. But i need to compare not all tables. Some tables must be skipped.
I wanted to use the tool mysqldbcompare . It looks what i need but i don't see an option to skip some tables. seems it will just compare all tables , which will not work for me (i know some tables are different, i want to skip them)
Is it possible to skip tables with this tool?
Maybe there is some alternative tool which can compare only selected tables?
In this manual there is nothing about tables skipping https://dev.mysql.com/doc/mysql-utilities/1.5/en/mysqldbcompare.html

selecting multiple tables in mysql and combine the results [duplicate]

The table names in my mysql database are dynamically generated. Is there some way to select data from tables which have a name matching a pattern? I guess it will look like:
select * from 'table_id_%'
No, you can't do that with MySQL. Tables in a query can't be dynamically specified - you have to build the list in your application (or do several single-table queries).
You can use INFORMATION_SCHEMA TABLES table to find tables you want, here is documentation: http://dev.mysql.com/doc/refman/5.0/en/tables-table.html . TABLES table has column NAME which represents names of tables. After finding table names you can run any sql queries you like.
That's not possible in the way you'd like to do it. However you could probably use prepared statements which are basically query-templates where you specificy the parameters (AFAIK also table names) that get replaced depending on your needs without copy and pasting the same query over and over again for different tables.

How to compare two databases' altered tables

I have inserted new columns in one database, and I now want to add the same type of columns to a second database. I need to know which columns are in the first database that are not in the second. I have many tables in each database that needs column difference comparing. I searched the web and I can only find ways to see the difference of the contents of columns in two tables. I don't need to compare the contents, just different columns in all the tables in each database. Each database has the same tables.
Thanks!
I found that you can do a database dump that just has the structure from phpmyadmin.
Are you doing this manually? You could just use SHOW CREATE to see the structure of the tables, and then something like the diff command in Linux to compare them.
For a commercial product answer: I use Red Gate's SQL Compare which works great. It can compare the entire schema of two databases. It can also update your target database to match your source database.
Use redgate SQL compare to comapre schema of two tables.
sql-dbdiff works well too. Its an open source.

MySQL Replace table from another table

I have 2 active database connections, I need to replace a number of tables from 'connection1' with that of connection2. The structures may, or may not be same, (depending if we make changes to the connection1 table.
I would assume I should do a complete table dump and replace keys where neccesary, but I really have no idea how to do this :)
Any help?
Have a look at Schema and Data sync tools in dbForge Studio for MySQL. It will help you to compare two databases on different servers, map tables and fields, generate and run synchronization script.
I ended up using the build in system command in PHP and mysqldump to first dump the data (export) to a file, then used system() again with mysql to import it into the new table and replace the old one.
Works like a charm :)

How do i dump all the tables from a single database in a sql file except for the ones mentioned?

I wanted to know if it was possible can do a db dump of all the tables in a database, but leaving a couple of tables specified on the command line. this is for mysql. i know there is one for adding specific tables on an sql, but i dont know if you can exclude specific tables. Thanks in advance.
Mysqldump has an --ignore-table option.