最近写了一个工具(比较两套测试环境数据库表、表字段、索引的差别)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] 只对索引比较,只对字段比较的配置,最终结果也只包含对应的配置项内容