--如何用PYTHON 定時打印 MYSQL FREE 使用率,與自動創建測試數據庫表

源數據庫匯中,PYTHON 的使用不是一個可選項,主要在很多地方,監控,處理一些DEVOPS的事情,或者與業務有關的處理的工作都是需要PYTHON 來進行的。下面就用PTYHON 來完成一個很小的打印MYSQL 系統的內存佔用率的小腳本來開始 PYTHON travel。(由於是初級水平有待提高,部分代碼的有待進步)

在學習PYTHON 的過程中,(很菜)領會到PYTHON 本身的語法是一回事,你使用的各種包的熟悉又是另一回事。所以下面先得說說程序中使用的mysql 的 python connector.

PYTHON 連接到MYSQL 的包有很多 PYMYSQL , MYSQLAB, 這裏沒有使用而是使用了官方的  Connector/Python 的方式進行連接

下面相關的代碼的初衷主要在分析一段時間INNODB BUFFER 的使用率,查看相關的變動情況,當然這樣的監控也有各種圖形化的監控軟件,但靈活性不高

#!/usr/bin/env python3
# coding: utf-8
import mysql.connector
from mysql.connector import errorcode
import re
import time
import datetime
import sys
class DBFREEMEMORY:
    def __init__(self, user=None, passwd=None, host=None, db=None):
        self.user = user
        self.passwd = passwd
        self.host = host
        self.db = db


    def mysql_connect(self):
        remotedb = {
          'host': self.host ,
          'user': self.user,
          'passwd': self.passwd,
          'database': self.db,
          'charset': 'utf8',
          'connection_timeout': 30,
          'use_pure': True
        }

        try:
          connect = mysql.connector.connect(**remotedb)
          mycursor = connect.cursor(dictionary=True)

         
          sql = "select substring_index(substring_index(event_name,'/',2),'/',-1) as event_type, round(sum(current_number_of_bytes_used) / 1024/1024, 2) as MB_CURRENTLY_USED from performance_schema.memory_summary_global_by_event_name group by event_type having mb_currently_used >0"
          ) as event_type, round(sum(current_number_of_bytes_used) / 1024/1024, 2) as MB_CURRENTLY_USED from performance_schema.memory_summary_global_by_event_name group by event_type having mb_currently_used >0"
          mycursor.execute(sql)
memory = mycursor.fetchall()
#收集當前使用的內存數
sql_1 = "show global variables  like 'innodb_buffer_pool_size';"
          mycursor.execute(sql_1)
          full_memory = mycursor.fetchall()
          #收集當前整體數據庫佔有的內存
          for t in full_memory:
          if t['Value'] != None:
              fmem = float(t['Value']) / 1024 / 1024
            else:
              t['Value'] = 1
          for i in memory:
            if i['MB_CURRENTLY_USED'] != None:
               mem = i['MB_CURRENTLY_USED']
            else:
              i['MB_CURRENTLY_USED'] = 1
          result = format(float(mem) / float(fmem) * 100, '.2f')
          print(str(result) + '%' + '  ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
          #將當前的內存使用數的百分比進行比較,並和當前時間一起打印

        except mysql.connector.Error as err:
          if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user name or password")
          elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Database does not exist")
          else:
            print(err)
        finally:
          mycursor.close()
          connect.close()


if __name__ == '__main__':
   info = DBFREEMEMORY(user='admin', passwd='1234.Com', host='192.168.198.9', db='performance_schema')
   info.mysql_connect()



          'host': self.host ,
          'user': self.user,
          'passwd': self.passwd,
          'database': self.db,
          'charset': 'utf8',
          'connection_timeout': 30,
          'use_pure': True
        }

        try:
          connect = mysql.connector.connect(**remotedb)
          mycursor = connect.cursor(dictionary=True)

          sql = "select substring_index(substring_index(event_name,'/',2),'/',-1) as event_type, round(sum(current_number_of_bytes_used) / 1024/1024, 2) as MB_CURRENTLY_USED from performance_schema.memory_summary_global_by_event_name group by event_type having mb_currently_used >0"
          mycursor.execute(sql)
          memory = mycursor.fetchall()
          sql_1 = "show global variables  like 'innodb_buffer_pool_size';"
          mycursor.execute(sql_1)
          full_memory = mycursor.fetchall()

          for t in full_memory:
            if t['Value'] != None:
              fmem = float(t['Value']) / 1024 / 1024
            else:
              t['Value'] = 1
          for i in memory:
            if i['MB_CURRENTLY_USED'] != None:
               mem = i['MB_CURRENTLY_USED']
            else:
              i['MB_CURRENTLY_USED'] = 1
          result = format(float(mem) / float(fmem) * 100, '.2f')
          print(str(result) + '%' + '  ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))


        except mysql.connector.Error as err:
          if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
            print("Something is wrong with your user name or password")
          elif err.errno == errorcode.ER_BAD_DB_ERROR:
            print("Database does not exist")
          else:
            print(err)


if __name__ == '__main__':
   info = DBFREEMEMORY(user='admin', passwd='1234.Com', host='192.168.198.9', db='performance_schema')
   info.mysql_connect()

下面一個程序是針對自動生成測試數據庫表,下面會在數據庫層面自動生成test 庫 以及 test1表,並插入隨機數 150萬

#!/usr/bin/env python3
# coding: utf-8
from __future__ import print_function
import mysql.connector
from mysql.connector import errorcode
from datetime import date, datetime, timedelta
import re
import time
import datetime
import sys
import random


class DBFREEMEMORY:
    def __init__(self, user=None, passwd=None, host=None, db=None):
        self.user = user
        self.passwd = passwd
        self.host = host
        self.db = db

    def gen_random_string(self):  #產生隨機內容的方法

        char_list = list('1234567890' + '0123456789')
        random.shuffle(char_list)
        return ''.join(char_list)

    def mysql_connect(self):
        remotedb = {
            'host': self.host,
            'user': self.user,
            'passwd': self.passwd,
            'database': self.db,
            'charset': 'utf8',
            'connection_timeout': 30,
            'use_pure': True
        }

        try:
            connect = mysql.connector.connect(**remotedb)
            mycursor = connect.cursor(dictionary=True)
            #判斷當前的服務器是否已經存在test數據庫
            mycursor.execute("show databases")
            database = [mycursor.fetchall()]
            # print (tables)
            database_list = re.findall('(\'.*?\')', str(database))
            database_list = [re.sub("'", '', each) for each in database_list]
            print(database_list)
            #如果存在test 數據庫就直接退出
            if 'test' in database_list:
                print('The database of test has existed,it has deleted it,please run the job again')
               

            else:
#創建相關               
          mycursor.execute("create database test")
          print('You have test database')

            DB_NAME = 'test'
            mycursor.execute("USE test".format(DB_NAME))
            #建表
            TABLES = {}
            TABLES['test'] = (
                "CREATE TABLE `test1` ("
                "  `id` int(11) NOT NULL AUTO_INCREMENT,"
                "  `content` varchar(200)  NULL,"
                "  `hash` varchar(200)  NULL,"
                "  `insert_date` date  NULL,"
                "  PRIMARY KEY (`id`)"
                ") ENGINE=InnoDB")

            table_name = TABLES['test']
            # mycursor.execute(table_name)
            mycursor.execute("show tables")
            table = [mycursor.fetchall()]
            # print (tables)
            table_list = re.findall('(\'.*?\')', str(table))
            table_list = [re.sub("'", '', each) for each in table_list]
            print(table_list)
            #判斷如果沒有
            if 'test1' in table_list:
                print('The table of test has existed,please delete it')

            else:
                try:  #執行並開始插入數據 10000條一提交
                    mycursor.execute(table_name)
                    #print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                    s_time = time.time()
                    i = 0
                    while i < 150000:
                        content = self.gen_random_string()
                        sql = "INSERT INTO test1 (content, hash,insert_date) VALUES ('%s', '%d',now())" \
                              % (content, hash(content))
                        mycursor.execute(sql)
                        i += 1
                        if i % 10000 == 0:
                           connect.commit()
                           print(i)
                    connect.close()
                    print('You have test table')
                    en_time = time.time()
                    print(en_time-s_time)
                    #print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
                except Exception as e:
                    print(e)



        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                print("Something is wrong with your user name or password")
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                print("Database does not exist")
            else:
                print(err)
        finally:
            mycursor.close()
            connect.close()


if __name__ == '__main__':
    info = DBFREEMEMORY(user='admin', passwd='1234.Com', host='192.168.198.9', db='performance_schema')
info.mysql_connect()
由於微信中的PYTHON 格式有問題,如需下載學習可以到QQ 羣下載文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章