zoukankan      html  css  js  c++  java
  • Python简单巡检MySQL

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    #----------------------------------------------------------#
    # Date    : xxxx-xx-xx                                     #
    # Author  : Created by zhouwanchun.                        #
    # Wechat  : lovemysql3306                                  #
    # Function: This scripts function is ...                   #
    # Version : 1.1                                            #
    #----------------------------------------------------------#
    
    # 导入模块
    import os
    import subprocess
    import mysql.connector
    import myloginpath
    
    # Linux终端清屏
    os.system('clear')
    
    # 注释信息
    print("""33[1;36m
    ############################################################
    # Date    : 2020-05-22                                     #
    # Author  : Created by zhouwanchun.                        #
    # Wechat  : lovemysql3306                                  #
    # Function: This scripts function is ...                   #
    # Version : v1.1                                           #
    ############################################################
    33[0m""")
    
    
    # 连接数据库
    conn_user = 'zhouwanchun'
    mylogin = myloginpath.parse(conn_user)
    #print(mylogin, type(mylogin))
    conn = mysql.connector.connect(**mylogin)
    
    # 创建SQL命令通道
    sql_cmd = conn.cursor()
    
    # SQL语句
    ### 检查实例参数
    sql1 = "show global variables;"
    
    ### 当前实例所有数据库容量
    sql2 = '''
    select  table_schema                                                                       as "数据库",
            count(table_name)                                                                  as "表个数",
            sum(table_rows)                                                                    as "记录数",
            truncate(sum(data_length)/1024/1024, 2)                                            as "数据容量(MB)",
            truncate(sum(index_length)/1024/1024, 2)                                           as "索引容量(MB)",
            truncate(sum(data_length)/1024/1024, 2) + truncate(sum(index_length)/1024/1024, 2) as "总容量(MB)"
    from information_schema.tables
    where TABLE_SCHEMA not in ("mysql", "information_schema", "performance_schema", "sys")
    group by table_schema
    order by count(table_name) desc, sum(data_length) desc, sum(index_length) desc;
    '''
    
    ### 当前实例所有表容量
    sql3 = '''
    select  table_schema                                                             as "数据库",
            table_name                                                               as "表名",
            table_rows                                                               as "记录数",
            truncate(data_length/1024/1024, 2)                                       as "数据容量(MB)",
            truncate(index_length/1024/1024, 2)                                      as "索引容量(MB)",
            truncate(data_length/1024/1024, 2) + truncate(index_length/1024/1024, 2) as "总容量(MB)"
    from information_schema.tables
    where TABLE_SCHEMA not in ("mysql", "information_schema", "performance_schema", "sys")
    order by table_schema asc;
    '''
    
    ### 检查所有表自增id的使用情况
    sql4 = '''
    select  table_schema    as "数据库",
            table_name      as "表名",
            engine          as "存储引擎",
            auto_increment  as "自增id"
    from information_schema.tables
    where table_schema not in ("mysql", "information_schema", "performance_schema", "sys")
    order by table_schema asc;
    '''
    
    ### 检查是否有非innodb存储引擎的表
    sql5 = '''
    select  table_schema    as "数据库",
            table_name      as "表名",
            engine          as "存储引擎"
    from information_schema.tables
    where engine != 'InnoDB';
    '''
    
    # 查看哪些索引采用部分索引(前缀索引)
    sql6 = '''
    SELECT TABLE_SCHEMA, TABLE_NAME, INDEX_NAME, SEQ_IN_INDEX, COLUMN_NAME, CARDINALITY, SUB_PART
    FROM INFORMATION_SCHEMA.STATISTICS
    WHERE SUB_PART > 10 ORDER BY SUB_PART DESC;
    '''
    
    # 查看哪些索引长度超过30字节,重点查CHAR/VARCHAR/TEXT/BLOB等类型
    sql7 = '''
    select c.table_schema as `db`,
           c.table_name as `tbl`,
           c.COLUMN_NAME as `col`,
           c.DATA_TYPE as `col_type`,
           c.CHARACTER_MAXIMUM_LENGTH as `col_len`,
           c.CHARACTER_OCTET_LENGTH as `col_len_bytes`,
           s.NON_UNIQUE as `isuniq`,
           s.INDEX_NAME,
           s.CARDINALITY,
           s.SUB_PART,
           s.NULLABLE
    from information_schema.COLUMNS c inner join information_schema.STATISTICS s
     using(table_schema, table_name, COLUMN_NAME)
    where
     c.table_schema not in ("mysql", "sys", "performance_schema", "information_schema", "test") and
     c.DATA_TYPE in ("varchar", "char", "text", "blob") and
     ((CHARACTER_OCTET_LENGTH > 20 and SUB_PART is null) or
     SUB_PART * CHARACTER_OCTET_LENGTH/CHARACTER_MAXIMUM_LENGTH >20);
    '''
    
    # 查看未完成的事务列表
    sql8 = '''
    select b.host, b.user, b.db, b.time, b.COMMAND, a.trx_id, a. trx_state
    from 
     information_schema.innodb_trx a left join 
     information_schema.PROCESSLIST b on a.trx_mysql_thread_id = b.id;
    '''
    
    # 查看当前有无行锁等待事件
    sql9 = '''
    SELECT lw.requesting_trx_id AS request_XID,
     trx.trx_mysql_thread_id as request_mysql_PID,
     trx.trx_query AS request_query,
     lw.blocking_trx_id AS blocking_XID,
     trx1.trx_mysql_thread_id as blocking_mysql_PID,
     trx1.trx_query AS blocking_query, lo.lock_index AS lock_index
    FROM
     information_schema.innodb_lock_waits lw INNER JOIN
     information_schema.innodb_locks lo
     ON lw.requesting_trx_id = lo.lock_trx_id INNER JOIN
     information_schema.innodb_locks lo1
     ON lw.blocking_trx_id = lo1.lock_trx_id INNER JOIN
     information_schema.innodb_trx trx
     ON lo.lock_trx_id = trx.trx_id INNER JOIN
     information_schema.innodb_trx trx1
     ON lo1.lock_trx_id = trx1.trx_id;
    '''
    # 其实,在MySQL 5.7下,也可以直接查看sys.innodb_lock_waits视图
    # SELECT * FROM sys.innodb_lock_waitsG
    
    # 检查哪些表没有显式创建主键索引
    sql10 = '''
    SELECT
    a.TABLE_SCHEMA as `db`,
    a.TABLE_NAME as `tbl`
    FROM
    (
    SELECT
    TABLE_SCHEMA,
    TABLE_NAME
    FROM
    information_schema.TABLES
    WHERE
    TABLE_SCHEMA NOT IN (
    "mysql",
    "sys",
    "information_schema",
    "performance_schema"
    ) AND
    TABLE_TYPE = "BASE TABLE"
    ) AS a
    LEFT JOIN (
    SELECT
    TABLE_SCHEMA,
    TABLE_NAME
    FROM
    information_schema.TABLE_CONSTRAINTS
    WHERE
    CONSTRAINT_TYPE = "PRIMARY KEY"
    ) AS b ON a.TABLE_SCHEMA = b.TABLE_SCHEMA
    AND a.TABLE_NAME = b.TABLE_NAME
    WHERE
    b.TABLE_NAME IS NULL;
    '''
    
    # 查看InnoDB表碎片率
    sql11 = '''
    SELECT TABLE_SCHEMA as `db`, TABLE_NAME as `tbl`,
    1-(TABLE_ROWS*AVG_ROW_LENGTH)/(DATA_LENGTH + INDEX_LENGTH + DATA_FREE) AS `fragment_pct`
    FROM information_schema.TABLES WHERE
    TABLE_SCHEMA NOT IN ("mysql", "information_schema", "performance_schema", "sys")
    ORDER BY fragment_pct DESC;
    '''
    
    
    sql_cmd.execute(sql1)
    sql1_result = dict(sql_cmd.fetchall())
    
    # 检查MySQL版本
    print("33[1;32mmysql_version = 33[0m" + sql1_result['version'])
    
    # 检查MySQL端口
    print("33[1;32mport = 33[0m" + sql1_result['port'])
    
    # 检查server_id
    print("33[1;32mserver_id = 33[0m" + sql1_result['server_id'])
    
    # 检查basedir目录
    print("33[1;32mbasedir = 33[0m" + sql1_result['basedir'])
    
    # 检查datadir目录
    print("33[1;32mdatadir = 33[0m" + sql1_result['datadir'])
    
    # 检查tmpdir目录
    print("33[1;32mtmpdir = 33[0m" + sql1_result['tmpdir'])
    
    # 检查pid_file
    print("33[1;32mpid_file = 33[0m" + sql1_result['pid_file'])
    
    # 检查socket
    print("33[1;32msocket = 33[0m" + sql1_result['socket'])
    
    # 检查log_error
    print("33[1;32mlog_error = 33[0m" + sql1_result['log_error'])
    
    # 检查binlog
    print("33[1;35mlog_slave_updates = 33[0m" + sql1_result['log_slave_updates'])
    print("33[1;35mbinlog_format = 33[0m" + sql1_result['binlog_format'])
    print("33[1;35mlog_bin = 33[0m" + sql1_result['log_bin'])
    print("33[1;35mexpire_logs_days = 33[0m" + sql1_result['expire_logs_days'])
    print("33[1;35msync_binlog = 33[0m" + sql1_result['sync_binlog'])
    
    # 检查gtid
    print("33[1;35mgtid_mode = 33[0m" + sql1_result['gtid_mode'])
    print("33[1;35menforce_gtid_consistency = 33[0m" + sql1_result['enforce_gtid_consistency'])
    
    # 检查relay_log
    print("33[1;32mrelay_log = 33[0m" + sql1_result['relay_log'])
    print("33[1;32mrelay_log_recovery = 33[0m" + sql1_result['relay_log_recovery'])
    print("33[1;32mrelay_log_purge = 33[0m" + sql1_result['relay_log_purge'])
    
    # 检查slow_query_log
    print("33[1;32mslow_query_log = 33[0m" + sql1_result['slow_query_log'])
    print("33[1;32mlong_query_time = 33[0m" + sql1_result['long_query_time'])
    print("33[1;32mslow_query_log_file = 33[0m" + sql1_result['slow_query_log_file'])
    print("33[1;32mlog_queries_not_using_indexes = 33[0m" + sql1_result['log_queries_not_using_indexes'])
    print("33[1;32mlog_throttle_queries_not_using_indexes = 33[0m" + sql1_result['log_throttle_queries_not_using_indexes'])
    
    # 检查general_log
    print("33[1;32mgeneral_log = 33[0m" + sql1_result['general_log'])
    print("33[1;32mgeneral_log_file = 33[0m" + sql1_result['general_log_file'])
    
    # 检查数据库实例字符集
    print("33[1;33mcharacter_set_server = 33[0m" + sql1_result['character_set_server'])
    print("33[1;33mcharacter_set_database = 33[0m" + sql1_result['character_set_database'])
    
    print("33[1;33mcharacter_set_client = 33[0m" + sql1_result['character_set_client'])
    print("33[1;33mcharacter_set_connection = 33[0m" + sql1_result['character_set_connection'])
    print("33[1;33mcharacter_set_results = 33[0m" + sql1_result['character_set_results'])
    
    print("33[1;33mcharacter_set_system = 33[0m" + sql1_result['character_set_system'])
    print("33[1;33mcharacter_set_filesystem = 33[0m" + sql1_result['character_set_filesystem'])
    
    # 检查skip_name_resolve
    print("33[1;32mskip_name_resolve = 33[0m" + sql1_result['skip_name_resolve'])
    
    # 检查只读模式
    print("33[1;35mread_only = 33[0m" + sql1_result['read_only'])
    print("33[1;35msuper_read_only = 33[0m" + sql1_result['super_read_only'])
    
    # 检查时区设置
    print("33[1;32mtime_zone = 33[0m" + sql1_result['time_zone'])
    
    # 检查load data导入导出目录
    print("33[1;32msecure_file_priv = 33[0m" + sql1_result['secure_file_priv'])
    
    # 检查连接数
    print("33[1;33mmax_connections = 33[0m" + sql1_result['max_connections'])
    print("33[1;33mmax_user_connections = 33[0m" + sql1_result['max_user_connections'])
    print("33[1;33mmax_connect_errors = 33[0m" + sql1_result['max_connect_errors'])
    
    # 检查超时连接
    print("33[1;33minteractive_timeout = 33[0m" + sql1_result['interactive_timeout'])
    print("33[1;33mwait_timeout = 33[0m" + sql1_result['wait_timeout'])
    
    # 检查InnoDB
    print("33[1;35mdefault_storage_engine = 33[0m" + sql1_result['default_storage_engine'])
    
    # 检查隔离级别
    print("33[1;35mtransaction_isolation = 33[0m" + sql1_result['transaction_isolation'])
    
    # 检查系统表空间
    print("33[1;32minnodb_data_home_dir = 33[0m" + sql1_result['innodb_data_home_dir'])
    print("33[1;32minnodb_data_file_path = 33[0m" + sql1_result['innodb_data_file_path'])
    print("33[1;32minnodb_temp_data_file_path = 33[0m" + sql1_result['innodb_temp_data_file_path'])
    print("33[1;32minnodb_buffer_pool_filename = 33[0m" + sql1_result['innodb_buffer_pool_filename'])
    print("33[1;32minnodb_file_per_table = 33[0m" + sql1_result['innodb_file_per_table'])
    print("33[1;32minnodb_buffer_pool_size = 33[0m" + str(int(sql1_result['innodb_buffer_pool_size'])/1024/1024))
    print("33[1;32minnodb_buffer_pool_instances = 33[0m" + sql1_result['innodb_buffer_pool_instances'])
    print("33[1;32minnodb_io_capacity = 33[0m" + sql1_result['innodb_io_capacity'])
    print("33[1;32minnodb_io_capacity_max = 33[0m" + sql1_result['innodb_io_capacity_max'])
    
    # 检查redo_log
    print("33[1;33minnodb_log_group_home_dir = 33[0m" + sql1_result['innodb_log_group_home_dir'])
    print("33[1;33minnodb_log_files_in_group = 33[0m" + sql1_result['innodb_log_files_in_group'])
    print("33[1;33minnodb_log_file_size = 33[0m" + str(int(sql1_result['innodb_log_file_size'])/1024/1024))
    print("33[1;33minnodb_log_buffer_size = 33[0m" + str(int(sql1_result['innodb_log_buffer_size'])/1024/1024))
    print("33[1;33minnodb_flush_log_at_trx_commit = 33[0m" + sql1_result['innodb_flush_log_at_trx_commit'])
    print("33[1;33minnodb_flush_method = 33[0m" + sql1_result['innodb_flush_method'])
    
    # 检查undo log
    print("33[1;32minnodb_undo_directory = 33[0m" + sql1_result['innodb_undo_directory'])
    print("33[1;32minnodb_undo_tablespaces = 33[0m" + sql1_result['innodb_undo_tablespaces'])
    
    # 检查查询缓存
    print("33[1;32mquery_cache_type = 33[0m" + sql1_result['query_cache_type'])
    
    # lock
    print("33[1;32mlock_wait_timeout = 33[0m" + sql1_result['lock_wait_timeout'])
    print("33[1;32minnodb_lock_wait_timeout = 33[0m" + sql1_result['innodb_lock_wait_timeout'])
    
    
    print('
    ')
    print("33[1;32m当前实例所有数据库容量33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql2 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m当前实例所有表容量33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql3 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m检查所有表自增id的使用情况33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql4 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m检查是否有非innodb存储引擎的表33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql5 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m查看哪些索引采用部分索引(前缀索引)33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql6 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m查看哪些索引长度超过30字节,重点查CHAR/VARCHAR/TEXT/BLOB等类型33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql7 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m查看未完成的事务列表33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql8 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m查看当前有无行锁等待事件33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql9 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m检查哪些表没有显式创建主键索引33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql10 + "'"], shell=True)
    
    print('
    ')
    print("33[1;32m查看InnoDB表碎片率33[0m")
    subprocess.run(['/usr/local/mysql/bin/mysql --login-path=' + conn_user + ' -e '  + "'" + sql11 + "'"], shell=True)
    
    
    sql_cmd.close()
    conn.commit()
    conn.close()
  • 相关阅读:
    121. Best Time to Buy and Sell Stock
    分页查询
    ViewPager
    SharedPreferences
    android 动画
    display~
    stringBuffer拼接有规律字符串
    修改placehosder
    this Activity.this Activity.class
    Windows基础编程SDK复习知识点
  • 原文地址:https://www.cnblogs.com/zhouwanchun/p/12989186.html
Copyright © 2011-2022 走看看