阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL数据库实例参数对比脚本

115次阅读
没有评论

共计 2530 个字符,预计需要花费 7 分钟才能阅读完成。

如何对比两个 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 = ‘123456’
    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) 是将字典转化为集合,这样对于字典的比较可以转为为集合来操作。

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-05/131245.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计2530字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中