1. 程式人生 > >mysql比對兩個資料庫表結構的方法

mysql比對兩個資料庫表結構的方法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

                       

在開發及除錯的過程中,需要比對新舊程式碼的差異,我們可以使用git/svn等版本控制工具進行比對。而不同版本的資料庫表結構也存在差異,我們同樣需要比對差異及獲取更新結構的sql語句。

例如同一套程式碼,在開發環境正常,在測試環境出現問題,這時除了檢查伺服器設定,還需要比對開發環境與測試環境的資料庫表結構是否存在差異。找到差異後需要更新測試環境資料庫表結構直到開發與測試環境的資料庫表結構一致。

我們可以使用mysqldiff工具來實現比對資料庫表結構及獲取更新結構的sql語句。

1.mysqldiff安裝方法

mysqldiff工具在mysql-utilities軟體包中,而執行mysql-utilities需要安裝依賴mysql-connector-python
 

mysql-connector-python 安裝

下載地址:https://dev.mysql.com/downloads/connector/python/


 

mysql-utilities 安裝

下載地址:https://downloads.mysql.com/archives/utilities/

因本人使用的是mac系統,可以直接使用brew安裝即可。

brew install caskroom/cask/mysql-connector-pythonbrew install caskroom/cask/mysql-utilities
   
  • 1
  • 2

安裝以後執行檢視版本命令,如果能顯示版本表示安裝成功

mysqldiff --versionMySQL Utilities mysqldiff version 1.6
.5 License type: GPLv2
  • 1
  • 2
  • 3


2.mysqldiff使用方法

命令:

mysqldiff --[email protected] --[email protected] --difftype=sql db1.table1:dbx.table3
   
  • 1

 
引數說明:

--server1 指定資料庫1--server2 指定資料庫2
   
  • 1
  • 2

比對可以針對單個數據庫,僅指定server1選項可以比較同一個庫中的不同表結構。
 

--difftype 差異資訊的顯示方式
   
  • 1

unified (default)
顯示統一格式輸出

context
顯示上下文格式輸出

differ
顯示不同樣式的格式輸出

sql
顯示SQL轉換語句輸出

如果要獲取sql轉換語句,使用sql這種顯示方式顯示最適合。

--character-set 指定字符集--changes-for 用於指定要轉換的物件,也就是生成差異的方向,預設是server1--changes-for=server1 表示server1要轉為server2的結構,server2為主。--changes-for=server2 表示server2要轉為server1的結構,server1為主。--skip-table-options 忽略AUTO_INCREMENT, ENGINE, CHARSET的差異。--version 檢視版本
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

更多mysqldiff的引數使用方法可參考官方文件: 
https://dev.mysql.com/doc/mysql-utilities/1.5/en/mysqldiff.html

3.例項

建立測試資料庫表及資料

create database testa;create database testb;use testa;CREATE TABLE `tba``id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(25) NOT NULL`age` int(10) unsigned NOT NULL`addtime` int(10) unsigned NOT NULLPRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8;insert into `tba`(name,age,addtime) values('fdipzone',18,1514089188);use testb;CREATE TABLE `tbb``id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL`age` int(10) NOT NULL`addtime` int(10) NOT NULLPRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;insert into `tbb`(name,age,addtime) values('fdipzone',19,1514089188);
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

 
執行差異比對,設定server1為主,server2要轉為server1資料庫表結構

mysqldiff [email protected] [email protected] --changes-for=server2 --difftype=sql testa.tba:testb.tbb;# server1 on localhost: ... connected.# server2 on localhost: ... connected.# Comparing testa.tba to testb.tbb                                 [FAIL]# Transformation for --changes-for=server2:#ALTER TABLE `testb`.`tbb`   CHANGE COLUMN addtime addtime int(10) unsigned NOT NULL,   CHANGE COLUMN age age int(10) unsigned NOT NULL,   CHANGE COLUMN name name varchar(25) NOT NULL, RENAME TO testa.tba , AUTO_INCREMENT=1002;# Compare failed. One or more differences found.
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

 
執行mysqldiff返回的更新sql語句

mysql> ALTER TABLE `testb`.`tbb`     ->   CHANGE COLUMN addtime addtime int(10) unsigned NOT NULL,     ->   CHANGE COLUMN age age int(10) unsigned NOT NULL,     ->   CHANGE COLUMN name name varchar(25) NOT NULL;Query OK, 0 rows affected (0.03 sec)
   
  • 1
  • 2
  • 3
  • 4
  • 5

 
再次執行mysqldiff進行比對,結構沒有差異,只有AUTO_INCREMENT存在差異

mysqldiff [email protected] [email protected] --changes-for=server2 --difftype=sql testa.tba:testb.tbb;# server1 on localhost: ... connected.# server2 on localhost: ... connected.# Comparing testa.tba to testb.tbb                                 [FAIL]# Transformation for --changes-for=server2:#ALTER TABLE `testb`.`tbb` RENAME TO testa.tba , AUTO_INCREMENT=1002;# Compare failed. One or more differences found.
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

 
設定忽略AUTO_INCREMENT再進行差異比對,比對通過

mysqldiff [email protected] [email protected] --changes-for=server2 --skip-table-options --difftype=sql testa.tba:testb.tbb;# server1 on localhost: ... connected.# server2 on localhost: ... connected.# Comparing testa.tba to testb.tbb                                 [PASS]# Success. All objects are the same.
   
  • 1
  • 2
  • 3
  • 4
  • 5
           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述