最近寫了一個工具(比較兩套測試環境數據庫表、表字段、索引的差別)python
功能:能夠比較兩套環境中mysql指定庫中表、表字段及索引的差別,返回具體須要同步的執行sql mysql
A環境的數據庫db 做爲sourcedb, B環境的數據庫db targetdb ,代碼比較的是 sourcedb 與targetdb 的差別,執行完sql後,保證了sourcedb 包含於targetdb sql
結果包括新建表sql,修改、增長字段sql, 刪除、新增索引sql 數據庫
具體使用方法:工具
pip install -i https://pypi.python.org/pypi dbstructsync 測試
在代碼裏引入使用, from DbStructSync import cli 索引
# result=cli.db_sync(sourcedb, targetdb) ,sourcedb,targetdb是兩個dict的參數,具體參數看下面
# sourcedb = {'host':'10.1.1.31','port':33306,'user':'roo1','passwd':'roo2','db':'investment'},
# targetdb = {'host': '10.1.1.32', 'port': 33306, 'user': 'roo1', 'passwd': 'roo2', 'db': 'investment'}
# )
result是一個list,包含sourcedb 與targetdb中不一致的地方,須要將這些語句在targetdb中執行,從而保證兩個環境中的結構一致ip
同時還支持 cli.db_sync_commandline 操做,代碼寫入到aa.py代碼中get
result = cli.db_sync_commandline()
python aa.py --source host=10.1.1.32,port=33306,user=root,passwd=root,db=investment --target host=10.1.1.37,port=33306,user=root,passwd=root,db=investment同步
python x.py --only-index --only-fields --source xx --target xx 其中 --source , --target是必須的參數
[--only-index] [--only-fields] 只對索引比較,只對字段比較的配置,最終結果也只包含對應的配置項內容