如何对比两个MySQL实例的参数情况,生产中常会有这样的需求,最近写了个python脚本,可基本实现该需求。

脚本

#!/usr/bin/python
import MySQLdb,sys
def fetch_variables(ip,user,passwd,port,variable=False):
# Open database connection
try:
db = MySQLdb.connect(host=ip, user=user,passwd=passwd,port=port)
except Exception,e:
print e
exit()
# prepare a cursor object using cursor() method
cursor = db.cursor()
if not variable:
sql = 'show variables'
else:
sql= "show variables like '%"+variable+"%'"
try:
# execute SQL query using execute() method.
cursor.execute(sql)
# Fetch all the rows in a list of lists.
results = cursor.fetchall()
dict={}
for row in results:
dict[row[0]]=row[1]
except:
print "Error: unable to fecth data"
return dict
def dict_to_set(dict):
return set(dict.keys()) def main():
if len(sys.argv)!=3 and len(sys.argv)!=4:
print 'Usage:',sys.argv[0],'ip:port nip:nport var'
exit()
user = 'root'
password = ''
ip, port = sys.argv[1].split(':')
nip,nport=sys.argv[2].split(':')
if len(sys.argv)==3:
variable=False
else:
variable=sys.argv[3]
dict = fetch_variables(ip, user, password, int(port),variable)
ndict = fetch_variables(nip, user, password, int(nport),variable)
set=dict_to_set(dict)
nset=dict_to_set(ndict)
same_variables=set.intersection(nset)
for variable in same_variables:
if dict[variable] != ndict[variable]:
print variable,':',dict[variable],'|',ndict[variable]
if __name__=='__main__':
main()

执行方式

输入:ip:port nip:nport var
功能:如果var为空,表示比较所有参数

带实例参数时,执行结果如下:

[root@mysql-server1 ~]# python diff_parameters.py 192.168.244.145:3306 192.168.244.146:3306 general_log_file
general_log_file : /var/lib/mysql/mysql-server1.log | /var/lib/mysql/keepalived01.log

不带实例参数时,执行结果如下:

[root@mysql-server1 ~]# python diff_parameters.py 192.168.244.145:3306 192.168.244.146:3306
version : 5.6.26-log | 5.6.26
log_bin_index : /var/lib/mysql/mysql-bin.index |
log_bin_basename : /var/lib/mysql/mysql-bin |
pseudo_thread_id : 9 | 104
slow_query_log_file : /var/lib/mysql/mysql-server1-slow.log | /var/lib/mysql/keepalived01-slow.log
server_id : 1 | 2
hostname : mysql-server1 | keepalived01
timestamp : 1462931171.666154 | 1462931171.957681
log_bin : ON | OFF
general_log_file : /var/lib/mysql/mysql-server1.log | /var/lib/mysql/keepalived01.log
max_binlog_size : 134217728 | 1073741824
server_uuid : c063ba6f-aee7-11e5-820e-000c29b05336 | 959bf641-b9e7-11e5-89c7-000c294c5ed4

输出结果解读:

第一列是实例参数,第二列和第三列用“|”隔开,其中第二列是第一个实例的参数值,第三列是第二个实例的参数值。

脚本解读

函数def fetch_variables(ip,user,passwd,port,variable=False)是从数据库中获取实例参数及值。variable=False的作用是来处理var为空和var不为空时的情况。

函数def dict_to_set(dict)是将字典转化为集合,这样对于字典的比较可以转为为集合来操作。

MySQL数据库实例参数对比脚本的更多相关文章

  1. 批量获取mysql数据库实例指定参数的值

    需求:需要对比所有mysql数据库实例上面的指定参数配置情况,同时需要需要能看到如ip,端口,master or slave,毕竟主和从参数不一样还是有可能的. 说明:必须要有个数据库存储所有是数据库 ...

  2. CentOS6.7下使用非root用户(普通用户)编译安装与配置mysql数据库并使用shell脚本定时任务方式实现mysql数据库服务随机自动启动

    CentOS6.7下使用非root用户(普通用户)编译安装与配置mysql数据库并使用shell脚本定时任务方式实现mysql数据库服务随机自动启动1.关于mysql?MySQL是一个关系型数据库管理 ...

  3. Python MySQLdb模块连接操作mysql数据库实例_python

    mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...

  4. PHP、jQuery、AJAX和MySQL 数据库实例

    index.html页面 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  5. 在一台机子上,安装,运行两mysql数据库实例

    为了方便测试,想要用到两个mysql数据库实例.以windows系统为例 当然安装第一个mysql数据库,很简单没什么说的.但是再要安装,运行mysql安装程序时,会提示,修复,卸载,重新安装. 这时 ...

  6. shell脚本连接、读写、操作mysql数据库实例

    本文介绍了如何在shell中读写mysql数据库.主要介绍了如何在shell 中连接mysql数据库,如何在shell中创建数据库,创建表,插入csv文件,读取mysql数据库,导出mysql数据库为 ...

  7. python3连接MySQL数据库实例

    #python3连接MySQL实例 import pymysql """导入连接MySQL需要的包,没有安装pymysql需要先安装 使用命令行切换到python的安装路 ...

  8. MYSQL数据库的参数文件

    参数文件:告诉MySQL实例启动时在哪里可以找到数据库文件,并且指定某些初始化参数,这些参数定义了某种内存结构的大小等设置,还会介绍各种参数的类型. 参数文件 当MySQL实例启动时,MySQL会先去 ...

  9. 一分钟在云端快速创建MySQL数据库实例

    本教程将帮助您了解如何使用Azure管理门户迅速创建,连接,配置MySQL 数据库 on Azure.完成本教程后,您将在Azure上拥有一个示例MySQL数据库服务器,并了解如何使用管理门户执行基本 ...

随机推荐

  1. libCURL开源库在VS2010环境下编译安装,配置详解

    libCURL开源库在VS2010环境下编译安装,配置详解 转自:http://my.oschina.net/u/1420791/blog/198247 http://blog.csdn.net/su ...

  2. Codeforces Round #361 (Div. 2) A

    A - Mike and Cellphone Description While swimming at the beach, Mike has accidentally dropped his ce ...

  3. [LintCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. java并发编程(十八)阻塞队列和阻塞栈

    阻塞队列 阻塞队列是Java 5并发新特性中的内容,阻塞队列的接口是java.util.concurrent.BlockingQueue,它有多个实现类:ArrayBlockingQueue.Dela ...

  5. ZXing二维码生成在Unity3D中出错,数组超出界限的解决办法

    错误截图: IndexOutOfRangeException: Array index is out of range.ZXing.Color32Renderer.Render (ZXing.Comm ...

  6. 下载判断Android和iOS

    //下载 function down(){ var url = ''; var u = navigator.userAgent, app = navigator.appVersion; var isA ...

  7. iOS 开发快速导引:TableView 和 CoreData【草】

    所有列表式的数据都是用 TableView 显示的 预览 待补充 原料 NSFetchedResultsController 用来操作 NSFetchRequst,有执行查询,监听变化,数据缓存等功能 ...

  8. 搭建Linux+Jexus+MariaDB+ASP.NET[LJMA]环境

    备注:,将我的博客内容整理成册,首先会在博客里优先发布,后续可能的话整理成电子书,主要从linux的最基础内容开始进入Linux的Mono开发方面的话题.本文是我整理博客内容的一篇文章. LJMA 是 ...

  9. 日志系统实战(一)—AOP静态注入

    背景 近期在写日志系统,需要在运行时在函数内注入日志记录,并附带函数信息,这时就想到用Aop注入的方式. AOP分动态注入和静态注入两种注入的方式. 动态注入方式 利用Remoting的Context ...

  10. LVS原理与使用(1)

    负载均衡,无论是否真正了解过,但我相信所有跟编程打交道的读者都有听说.同时,它(负载均衡)也是被认为一个大型网站的标识性技术之一(但负载均衡的作用肯定不止这点用途).虽然网上也有不少关于LVS配置实用 ...