MySQL效能分析指令碼

來源:互聯網
上載者:User

標籤:

"""目標       :       這個工具用於分析MySQL執行個體的效能問題作者       :       蔣樂興QQ         :       1721900707版本資訊   :       基於python3.4 MySQL 5.7.11  orzdba  MySQL使用者要用到的一些許可權:    create user [email protected]‘127.0.0.1‘ identified by ‘131417‘;     """#!/usr/bin/python#!coding:utf-8import mysql.connector as connectorimport jsonimport argparseimport sysshow_golbal_value="select variable_name,variable_value from performance_schema.global_variables where variable_name= %s"show_global_statu="select variable_name,variable_value from performance_schema.global_status where variable_name= %s"def anaylsis_query_cache(cursor,results):    """    本函數用於分析mysql執行個體的查詢快取、如果query_cache_type=0說明沒有開啟這個工能,那麼分析結束。    不然要分析查詢快取的剩餘記憶體,和命中率。把分析的結果封裝到results變數中。    """    analysis_var=("query_cache_type",)    cursor.execute(show_golbal_value,analysis_var)    key,value=cursor.fetchone()    #如果value的值等於OFF、說明本執行個體並沒有開啟查詢快取。    if value==‘OFF‘:        results[‘query_cache‘]=‘query cache function not in use for this instance‘    else:        #如果邏輯走到了這裡說明、執行個體開啟了查詢快取        #Qcache_free_memory 對應著剩餘的查詢快取記憶體。        cursor.execute(show_global_statu,("Qcache_free_memory",))        key,value = cursor.fetchone()        #由於這個是延時計算的;所以查出來就要把它用掉。********************        Qcache_free_memory=value        #query_cache_size   對應著查詢快取的記憶體大小。        cursor.execute(show_golbal_value,("query_cache_size",))        key,value = cursor.fetchone();        query_cache_size=value        #用於查詢快取的記憶體空閑率        if float(query_cache_size) != 0:            query_cache_memory_free_rate=float(Qcache_free_memory)/float(query_cache_size)        else:            query_cache_memory_free_rate=None        #Qcache_hits    對應著命中的次數        cursor.execute(show_global_statu,("Qcache_hits",))        key,value=cursor.fetchone()        Qcache_hits=value        #Qcache_inserts 對應的沒有命中的次數----由於沒有命中所以要插入。        cursor.execute(show_global_statu,("Qcache_inserts",))        key,value=cursor.fetchone()        Qcache_inserts=value        #查詢快取的命中率為        if float(Qcache_hits+Qcache_inserts) != 0:            query_cache_hit_rate=float(Qcache_hits)/float(Qcache_hits+Qcache_inserts)        else:            query_cache_hit_rate=None        #組織結果        tempResult={}        tempResult[‘Qcache_free_memory‘]=Qcache_free_memory        tempResult[‘query_cache_size‘]=query_cache_size        tempResult[‘query_cache_memory_free_rate‘]=query_cache_memory_free_rate        tempResult[‘Qcache_hits‘]=Qcache_hits        tempResult[‘Qcache_inserts‘]=Qcache_inserts        tempResult[‘query_cache_hit_rate‘]=query_cache_hit_rate        results[‘query_cache‘]=tempResultanalysis_function_sets={‘anaylsis_query_cache‘:anaylsis_query_cache}if __name__=="__main__":    cnx=None    cursor=None    config={        ‘host‘:‘127.0.0.1‘,        ‘port‘:3306,        ‘user‘:‘admin‘,        ‘password‘:‘131417‘        }    results={}    try:        cnx=connector.connect(**config)        cursor=cnx.cursor(buffered=True)        anaylsis_query_cache(cursor,results)        for key,function in analysis_function_sets.items():            function(cursor,results)        print(results)    except Exception as err:        print(err)    finally:        if cnx != None:            cnx.close()            cursor.close()        

 

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.