MySQL資料庫執行個體參數對比指令碼

來源:互聯網
上載者:User

標籤:

如何對比兩個MySQL執行個體的參數情況,生產中常會有這樣的需求,最近寫了個python指令碼,可基本實現該需求。

 

指令碼

#!/usr/bin/pythonimport MySQLdb,sysdef 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 dictdef 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為空白,表示比較所有參數

帶執行個體參數時,執行結果如下:

[[email protected] ~]# python diff_parameters.py 192.168.244.145:3306 192.168.244.146:3306 general_log_filegeneral_log_file : /var/lib/mysql/mysql-server1.log | /var/lib/mysql/keepalived01.log

 

不帶執行個體參數時,執行結果如下:

[[email protected] ~]# python diff_parameters.py 192.168.244.145:3306 192.168.244.146:3306version : 5.6.26-log | 5.6.26log_bin_index : /var/lib/mysql/mysql-bin.index | log_bin_basename : /var/lib/mysql/mysql-bin | pseudo_thread_id : 9 | 104slow_query_log_file : /var/lib/mysql/mysql-server1-slow.log | /var/lib/mysql/keepalived01-slow.logserver_id : 1 | 2hostname : mysql-server1 | keepalived01timestamp : 1462931171.666154 | 1462931171.957681log_bin : ON | OFFgeneral_log_file : /var/lib/mysql/mysql-server1.log | /var/lib/mysql/keepalived01.logmax_binlog_size : 134217728 | 1073741824server_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資料庫執行個體參數對比指令碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.