mysqldiff隻能比較兩個資料庫都有的表之間的差異,也就是說它不會生成create語句。
格式:
`mysqldiff --server1=user:[email protected]:port:socket --server2=user:[email protected]:port:socket db1.object1:db2.object1 db3:db4`
選項說明
--server1:MySQL伺服器1配置。
--server2:MySQL伺服器2配置。如果是同一伺服器,--server2可以省略。
db1:db2:要比較的兩個資料庫。如果比較不同伺服器上的同名資料庫,可以省略:db2。
--all:比較所有兩伺服器上所有的同名資料庫。--exclude排除無需比較的資料庫。
--run-all-tests:運作完整比較,遇到第一次差異時不停止。
--changes-for=:修改對象。例如--changes-for=server2,那麼對比以sever1為主,生成的差異的修改也是針對server2的對象的修改。
-d DIFFTYPE,--difftype=DIFFTYPE:差異的資訊顯示的方式,有[unified|context|differ|sql],預設是unified。如果使用sql,那麼就直接生成差異的SQL,這樣非常友善。
--show-reverse:在生成的差異修改裡面,同時會包含server2和server1的修改。
--skip-table-options:保持表的選項不變,即對比的差異裡面不包括表名、AUTO_INCREMENT、ENGINE、CHARSET等差異。
--skip-diff:跳過對象定義比較檢查。所謂對象定義,就是CREATE語句()裡面的部分,--skip-table-options是()外面的部分。
--skip-object-compare:預設情況下,先檢查兩個資料庫中互相缺失的對象,再對都存在對象間的差異。這個參數的作用就是,跳過第一步,不檢查互相缺失的對象。
--skip-checksum-table:資料一緻性驗證時跳過CHECKSUM TABLE。
--skip-data-check:跳過資料一緻性驗證。
--skip-row-count:跳過字段數量檢查。
舉例
目标沒有對應資料庫
mysqldiff --skip-table-options --difftype=sql --changes-for=server2 --server1=root:[email protected] --server2=root:[email protected] test:test
結果:
# WARNING: Using a password on the command line interface can be insecure.
# server1 on 10.63.2.212: ... connected.
# server2 on 192.168.154.129: ... connected.
# WARNING: Objects in server1.test but not in server2.test:
# TABLE: category
# Compare failed. One or more differences found.
比對整個庫
mysqldiff --skip-table-options --difftype=sql --changes-for=server2 --server1=root:[email protected] --server2=root:[email protected] test:test
mysqldbcompare --skip-table-options --difftype=sql --changes-for=server2 --server1=root:[email protected] --server2=root:[email protected] test:test
結果:
# WARNING: Using a password on the command line interface can be insecure.
# server1 on 10.63.2.212: ... connected.
# server2 on 192.168.154.129: ... connected.
# WARNING: Objects in server1.test but not in server2.test:
# TABLE: category
# Compare failed. One or more differences found.
比對指定對象
mysqldiff --skip-table-options --difftype=sql --changes-for=server2 --server1=root:[email protected] --server2=root:[email protected] test.category:test.category
結果:
# server1 on 10.63.2.212: ... connected.
# server2 on 192.168.154.129: ... connected.
# Comparing test.category to test.category [FAIL]
# Transformation for --changes-for=server2:
#
ALTER TABLE `test`.`category`
CHANGE COLUMN id id int(11) unsigned NOT NULL,
ADD COLUMN name varchar(30) NOT NULL AFTER id,
ENGINE=MyISAM;
# Compare failed. One or more differences found.