分享一个实用的MySQL一键巡检脚本

2024-05-31 16:12

本文主要是介绍分享一个实用的MySQL一键巡检脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

今日分享一个实用的MySQL一键巡检脚本,脚本内容还不是很完善,后续会继续进行优化。大家可以先在测试环境执行,确认执行没问题后可以在生产环境进行操作,问题的可以私信我。

MySQL一键巡检脚本的作用主要是帮助数据库管理员快速且系统性地检查和评估MySQL数据库实例的运行状态和健康状况。通过定期执行这样的巡检脚本,可以及时发现潜在问题,优化性能,确保数据库的高可用性和稳定性。以下是MySQL一键巡检脚本的一些主要作用和目标:

1. 性能监测

  • CPU使用率:检查MySQL实例及其主机的CPU使用情况,识别是否存在过高的CPU占用。

  • 内存使用情况:监测内存使用情况,判断是否存在内存泄露或内存不足。

  • I/O性能:检查磁盘I/O性能,评估读写速度和延迟,确保磁盘性能不会成为瓶颈。

2. 配置检查

  • 重要参数检查:审查MySQL配置文件中的关键参数(如max_connections、innodb_buffer_pool_size等),根据最佳实践进行调整。

  • 安全设置:检查用户权限、安全配置和日志等,确保数据库的安全性。

3. 数据库状态

  • 连接数监控:检查当前连接数及最大连接数设置,预防由于连接数过多导致的资源耗尽问题。

  • 线程状态:统计各类线程的状态,识别锁等待、慢查询等问题。

4. 存储和容量监控

  • 表空间使用:统计数据库和表的大小,监测表空间使用情况,规划存储扩容。

  • 数据文件和日志文件:检查数据文件和日志文件的增长情况,确保磁盘空间充足。

5. 日志和慢查询分析

  • 错误日志:分析MySQL错误日志,获取近期发生的错误信息。

  • 慢查询日志:捕获并分析慢查询日志,找出执行时间较长的SQL语句,优化查询性能。

6. 备份和恢复状态

  • 备份状态检查:检查最近的备份时间和备份文件的完整性,确保备份策略有效执行。

  • 恢复测试:定期测试备份文件的恢复情况,验证备份的可用性。

7. 高可用性状态

  • 主从复制状态:检查主从复制的同步状态,确保数据一致性和复制链路畅通。

  • 自动故障切换:检查高可用配置和故障切换机制的正常运作,确保数据库在故障时能够快速恢复。

    #!/bin/bashhost="192.168.59.128"port="3306"userName="root"password="123456"dbname="mysql"base='/usr/local/mysql'##数据文件位置##echo "================= mysql配置信息 ==============================="echo "========= 基本配置信息 ==========="lower_case_table_names="show variables like 'lower_case_table_names';"lower_case_table_names_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${lower_case_table_names}")echo "不区分大小写:" `echo ${lower_case_table_names_val} | cut -d' ' -f4`_port="show variables like 'port';"_port_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${_port}")echo "端口:" `echo ${_port_val} | cut -d' ' -f4`socket="show variables like 'socket';"socket_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${socket}")echo "socket的值:" `echo ${socket_val} | cut -d' ' -f4`skip_name_resolve="show variables like 'skip_name_resolve';"skip_name_resolve_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${skip_name_resolve}")echo "域名解析skip_name_resolve:" `echo ${skip_name_resolve_val} | cut -d' ' -f4`character_set_server="show variables like 'character_set_server';"character_set_server_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${character_set_server}")echo "数据库字符集character_set_server:" `echo ${character_set_server_val} | cut -d' ' -f4`interactive_timeout="show variables like 'interactive_timeout';"interactive_timeout_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${interactive_timeout}")echo "交互式连接超时时间(mysql工具、mysqldump等)interactive_timeout(秒):" `echo ${interactive_timeout_val} | cut -d' ' -f4`wait_timeout="show variables like 'wait_timeout';"wait_timeout_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${wait_timeout}")echo "非交互式连接超时时间,默认的连接mysql api程序,jdbc连接数据库等wait_timeout(秒):" `echo ${wait_timeout_val} | cut -d' ' -f4`query_cache_type="show variables like 'query_cache_type';"query_cache_type_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${query_cache_type}")echo "查询缓存query_cache_type:" `echo ${query_cache_type_val} | cut -d' ' -f4`innodb_version="show variables like 'innodb_version';"innodb_version_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_version}")echo "数据库版本:" `echo ${innodb_version_val} | cut -d' ' -f4`trx_isolation="show variables like 'tx_isolation';"trx_isolation_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${trx_isolation}")echo "mysql5.6隔离级别trx_isolation:" `echo ${trx_isolation_val} | cut -d' ' -f4`transaction_isolation="show variables like 'transaction_isolation';"transaction_isolation_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${transaction_isolation}")echo "隔离级别transaction_isolation:" `echo ${transaction_isolation_val} | cut -d' ' -f4`datadir="show variables like '%datadir%';"datadir_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${datadir}")echo "mysql 数据文件存放位置:" `echo ${datadir_val} | cut -d' ' -f4`echo "========= 连接数配置信息 ==========="max_connections="show variables like 'max_connections';"max_connections_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${max_connections}")echo "最大连接数(max_connections):" `echo ${max_connections_val} | cut -d' ' -f4`Max_used_connections="show status like 'Max_used_connections';"Max_used_connections_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${Max_used_connections}")echo "当前连接数(Max_used_connections):" `echo ${Max_used_connections_val} | cut -d' ' -f4`max_connect_errors="show variables like 'max_connect_errors';"max_connect_errors_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${max_connect_errors}")echo "最大错误连接数(max_connect_errors):" `echo ${max_connect_errors_val} | cut -d' ' -f4`echo "========= binlog配置信息 ==========="sync_binlog="show variables like 'sync_binlog';"sync_binlog_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${sync_binlog}")echo "sync_binlog(0|1|n,查看是否采用双1模式):" `echo ${sync_binlog_val} | cut -d' ' -f4`binlog_format="show variables like 'binlog_format';"binlog_format_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${binlog_format}")echo "binlog格式:" `echo ${binlog_format_val} | cut -d' ' -f4`log_bin="show variables like 'log-bin';"log_bin_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${log-bin}")echo "binlog文件(log-bin):" `echo ${log_bin_val} | cut -d' ' -f4`expire_logs_days="show variables like 'expire_logs_days';"expire_logs_days_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${expire_logs_days}")echo "binlog文件过期时间:" `echo ${expire_logs_days_val} | cut -d' ' -f4`binlog_cache_size="show variables like 'binlog_cache_size';"binlog_cache_size_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${binlog_cache_size}")echo "binlog_cache_size:" `echo ${binlog_cache_size_val} | cut -d' ' -f4`max_binlog_cache_size="show variables like 'max_binlog_cache_size';"max_binlog_cache_size_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${max_binlog_cache_size}")echo "max_binlog_cache_size:" `echo ${max_binlog_cache_size_val} | cut -d' ' -f4`max_binlog_size="show variables like 'max_binlog_size';"max_binlog_size_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${max_binlog_size}")echo "binlog文件大小:" `echo ${max_binlog_size_val} | cut -d' ' -f4`master_info_repository="show variables like 'master_info_repository';"master_info_repository_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${master_info_repository}")echo "master_info_repository(table|file,建议用table):" `echo ${master_info_repository_val} | cut -d' ' -f4`relay_log_info_repository="show variables like 'relay_log_info_repository';"relay_log_info_repository_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${relay_log_info_repository}")echo "relay_log_info_repository(table|file,建议用table):" `echo ${relay_log_info_repository_val} | cut -d' ' -f4`relay_log_recovery="show variables like 'relay_log_recovery';"relay_log_recovery_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${relay_log_recovery}")echo "relay_log_info_repository(建议开启):" `echo ${relay_log_recovery_val} | cut -d' ' -f4`echo "========= GTID配置信息 ==========="gtid_mode="show variables like 'gtid_mode';"gtid_mode_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${gtid_mode}")echo "是否开启gtid_mode:" `echo ${gtid_mode_val} | cut -d' ' -f4`enforce_gtid_consistency="show variables like 'enforce_gtid_consistency';"enforce_gtid_consistency_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${enforce_gtid_consistency}")echo "enforce_gtid_consistency是否开启:" `echo ${enforce_gtid_consistency_val} | cut -d' ' -f4`echo "(MySQL官方解释说当启用enforce_gtid_consistency功能的时候,MySQL只允许能够保障事务安全,并且能够被日志记录的SQL语句被执行,像create table … select 和 create temporary table语句,以及同时更新事务表和非事务表的SQL语句或事务都不允许执行)"log_slave_updates="show variables like 'log_slave_updates';"log_slave_updates_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${log_slave_updates}")echo "级联复制是否开启log_slave_updates:" `echo ${log_slave_updates_val} | cut -d' ' -f4`echo "======== innodb配置信息 ========="innodb_data_home_dir="show variables like 'innodb_data_home_dir';"innodb_data_home_dir_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_data_home_dir}")echo "innodb_data_home_dir:" `echo ${innodb_data_home_dir_val} | cut -d' ' -f4`innodb_buffer_pool_size="show variables like 'innodb_buffer_pool_size';"innodb_buffer_pool_size_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_buffer_pool_size}")echo "innodb_buffer_pool_size(不超过内存的75%):" `echo ${innodb_buffer_pool_size_val} | cut -d' ' -f4`innodb_buffer_pool_instances="show variables like 'innodb_buffer_pool_instances';"innodb_buffer_pool_instances_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_buffer_pool_instances}")echo "innodb_buffer_pool_instances(innodb_buffer_pool_size小于8G实例个数建议为1):" `echo ${innodb_buffer_pool_instances_val} | cut -d' ' -f4`innodb_log_file_size="show variables like 'innodb_log_file_size';"innodb_log_file_size_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_log_file_size}")echo "redo文件的大小innodb_log_file_size:" `echo ${innodb_log_file_size_val} | cut -d' ' -f4`innodb_log_files_in_group="show variables like 'innodb_log_files_in_group';"innodb_log_files_in_group_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_log_files_in_group}")echo "redo文件的个数innodb_log_files_in_group:" `echo ${innodb_log_files_in_group_val} | cut -d' ' -f4`innodb_flush_log_at_trx_commit="show variables like 'innodb_flush_log_at_trx_commit';"innodb_flush_log_at_trx_commit_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_flush_log_at_trx_commit}")echo "innodb_flush_log_at_trx_commit(0|1|2,跟sync_binlog双1):" `echo ${innodb_flush_log_at_trx_commit_val} | cut -d' ' -f4`innodb_io_capacity="show variables like 'innodb_io_capacity';"innodb_io_capacity_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_io_capacity}")echo "innodb_io_capacity(机械硬盘200,固态2000,闪存20000):" `echo ${innodb_io_capacity_val} | cut -d' ' -f4`transaction_isolation="show variables like 'transaction_isolation';"transaction_isolation_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${transaction_isolation}")echo "隔离级别transaction_isolation:" `echo ${transaction_isolation_val} | cut -d' ' -f4`trx_isolation="show variables like 'tx_isolation';"trx_isolation_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${trx_isolation}")echo "mysql5.6隔离级别trx_isolation:" `echo ${trx_isolation_val} | cut -d' ' -f4`innodb_max_undo_log_size="show variables like 'innodb_max_undo_log_size';"innodb_max_undo_log_size_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_max_undo_log_size}")echo "undo大小innodb_max_undo_log_size:" `echo ${innodb_max_undo_log_size_val} | cut -d' ' -f4`innodb_undo_tablespaces="show variables like 'innodb_undo_tablespaces';"innodb_undo_tablespaces_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${innodb_max_undo_log_size}")echo "undo个数innodb_undo_tablespaces:" `echo ${innodb_undo_tablespaces_val} | cut -d' ' -f4`echo "========= rep配置信息 ==========="slave_parallel_type="show variables like 'slave-parallel-type';"slave_parallel_type_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${slave_parallel_type}")echo "slave复制模式:" `echo ${slave_parallel_type_val} | cut -d' ' -f4`slave_parallel_workers="show variables like 'slave-parallel-workers';"slave_parallel_workers_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${slave_parallel_workers}")echo "slave并发复制:" `echo ${slave_parallel_workers_val} | cut -d' ' -f4`echo "================= 内存配置情况 ==============================="mem_dis_1="show variables like 'innodb_buffer_pool_size';"mem_dis_1_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_1}")mem_dis_1_val_1=`echo ${mem_dis_1_val} | cut -d' ' -f4`mem_dis_1_val_2=`echo | awk "{print $mem_dis_1_val_1/1024/1024}"`echo "InnoDB 数据和索引缓存:" $mem_dis_1_val_1mem_dis_2="show variables like 'innodb_log_buffer_size';"mem_dis_2_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_2}")mem_dis_2_val_1=`echo ${mem_dis_2_val} | cut -d' ' -f4`mem_dis_2_val_2=`echo | awk "{print $mem_dis_2_val_1/1024/1024}"`echo "InnoDB 日志缓冲区:" $mem_dis_2_val_1mem_dis_3="show variables like 'binlog_cache_size';"mem_dis_3_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_3}")mem_dis_3_val_1=`echo ${mem_dis_3_val} | cut -d' ' -f4`mem_dis_3_val_2=`echo | awk "{print $mem_dis_3_val_1/1024/1024}"`echo "二进制日志缓冲区:" $mem_dis_3_val_1mem_dis_4="show variables like 'thread_cache_size';"mem_dis_4_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_4}")echo "连接线程缓存:" `echo $mem_dis_4_val | cut -d' ' -f4`mem_dis_5="show variables like 'query_cache_size';"mem_dis_5_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_5}")echo "查询缓存:" `echo ${mem_dis_5_val} | cut -d' ' -f4`mem_dis_6="show variables like 'table_open_cache';"mem_dis_6_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_6}")echo "表缓存:" `echo ${mem_dis_6_val} | cut -d' ' -f4`mem_dis_7="show variables like 'table_definition_cache';"mem_dis_7_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_7}")echo "表定义缓存:" `echo ${mem_dis_7_val} | cut -d' ' -f4`mem_dis_8="show variables like 'max_connections';"mem_dis_8_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_8}")echo "最大线程数:" `echo ${mem_dis_8_val} | cut -d' ' -f4`mem_dis_9="show variables like 'thread_stack';"mem_dis_9_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_9}")echo "线程栈信息使用内存:" `echo ${mem_dis_9_val} | cut -d' ' -f4`mem_dis_10="show variables like 'sort_buffer_size';"mem_dis_10_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_10}")echo "排序使用内存:" `echo ${mem_dis_10_val} | cut -d' ' -f4`mem_dis_11="show variables like 'join_buffer_size';"mem_dis_11_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_11}")echo "Join操作使用内存:" `echo ${mem_dis_11_val} | cut -d' ' -f4`mem_dis_12="show variables like 'read_buffer_size';"mem_dis_12_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_12}")echo "顺序读取数据缓冲区使用内存:" `echo ${mem_dis_12_val} | cut -d' ' -f4`mem_dis_13="show variables like 'read_rnd_buffer_size';"mem_dis_13_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_13}")echo "随机读取数据缓冲区使用内存:" `echo ${mem_dis_13_val} | cut -d' ' -f4`mem_dis_14="show variables like 'tmp_table_size';"mem_dis_14_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${mem_dis_14}")echo "临时表使用内存:" `echo ${mem_dis_14_val} | cut -d' ' -f4`echo "================= QPS ==============================="Questions1="show global status like 'Questions';"Questions1_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${Questions1}")sleep 1Questions2="show global status like 'Questions';"Questions2_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${Questions2}")echo "QPS:$((`echo ${Questions2_val} | cut -d' ' -f4`-`echo ${Questions1_val} | cut -d' ' -f4`))"echo "================= TPS ==============================="Com_commit="show  global status like 'Com_commit';"Com_commit_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${Com_commit}")Com_rollback="show global status like 'Com_rollback';"Com_rollback_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${Com_rollback}")echo "TPS:" $((`echo ${Com_commit_val} | cut -d' ' -f4` + `echo ${Com_rollback_val} | cut -d' ' -f4`))##缓存命中率##echo "================= 缓存命中情况 ==============================="cache_hits="show global status like 'QCache_hits';"hits=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${cache_hits}")hits_val=`echo ${hits} | cut -d' ' -f4`echo "缓存命中次数:" ${hits_val}cache_not_hits="show global status like  'Qcache_inserts';"not_hits=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${cache_not_hits}")not_hits_val=`echo ${not_hits} | cut -d' ' -f4`echo "缓存未命中次数:" ${not_hits_val}cache_hits_rate_1=$(($hits_val - $not_hits_val))cache_hits_rate_2=`echo | awk "{print $cache_hits_rate_1/$hits_val * 100}"`echo "缓存命中率:" ${cache_hits_rate_2} "%"echo "================= 主从复制 ============================="slave_parallel_type="show variables like 'slave-parallel-type';"slave_parallel_type_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${slave_parallel_type}")echo "slave复制模式:" `echo ${slave_parallel_type_val} | cut -d' ' -f4`slave_parallel_workers="show variables like 'slave-parallel-workers';"slave_parallel_workers_val=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${slave_parallel_workers}")echo "slave并发复制:" `echo ${slave_parallel_workers_val} | cut -d' ' -f4`${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show master status\G;"${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show slave status\G;"echo "================= 半同步复制 ==============================="${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show variables like '%semi%';"echo "================= 慢查询 ==============================="slow_query_log_file=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show variables like 'slow_query_log_file';"|grep 'slow'|awk '{print $2}')slow_query_log=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show variables like 'slow_query_log';"|grep 'slow'|awk '{print $2}')long_query_time=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show variables like 'long_query_time';"|grep 'long_query_time'|awk '{print $2}')log_queries_not_using_indexes=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show variables like 'log_queries_not_using_indexes';"|grep 'log_queries_not_using_indexes'|awk '{print $2}')if [ ${slow_query_log} == "ON" ];thenecho "慢查询状态(slow_query_log):${slow_query_log} ;long_query_time(s) : ${long_query_time};log_queries_not_using_indexes: ${log_queries_not_using_indexes};慢查询top10,如下:"mysqldumpslow -s c -t 10 ${slow_query_log_file};elseecho "慢查询状态(slow_query_log):${slow_query_log} ,未开启慢查询。"fi##等待事件##echo "================= 数据库大小 ==============================="${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "SELECTtable_schema,sum( data_length )/ 1024 / 1024 / 1024 AS data_length,sum( index_length )/ 1024 / 1024 / 1024 AS index_length,sum( data_length + index_length )/ 1024 / 1024 / 1024 AS sum_data_indexFROMinformation_schema.TABLESWHEREtable_schema NOT IN ( 'mysql', 'information_schema', 'performance_schema', 'sys' )GROUP BYtable_schema;"echo "================= 数据碎片 ==============================="${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "SELECTTABLE_SCHEMA,TABLE_NAME,ENGINE,concat( splinter, 'G' ) '碎片(G)'FROM(SELECTTABLE_SCHEMA,TABLE_NAME,ENGINE,ROUND(( DATA_LENGTH + INDEX_LENGTH - TABLE_ROWS * AVG_ROW_LENGTH )/ 1024 / 1024 / 1024 ) splinterFROMinformation_schema.TABLESWHERETABLE_TYPE = 'BASE TABLE') aWHEREsplinter > 1ORDER BYsplinter DESC;"echo "================= 锁查询 ==============================="${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "SELECTr.trx_isolation_level,r.trx_id waiting_trx_id,r.trx_mysql_thread_id waiting_trx_thread,r.trx_state waiting_trx_state,lr.lock_mode waiting_trx_lock_mode,lr.lock_type waiting_trx_lock_type,lr.lock_table waiting_trx_lock_table,lr.lock_index waiting_trx_lock_index,r.trx_query waiting_trx_query,b.trx_id blocking_trx_id,b.trx_mysql_thread_id blocking_trx_thread,b.trx_state blocking_trx_state,lb.lock_mode blocking_trx_lock_mode,lb.lock_type blocking_trx_lock_type,lb.lock_table blocking_trx_lock_table,lb.lock_index blocking_trx_lock_index,b.trx_query blocking_queryFROMinformation_schema.innodb_lock_waits wINNER JOIN information_schema.innodb_trx b ON b.trx_id = w.blocking_trx_idINNER JOIN information_schema.innodb_trx r ON r.trx_id = w.requesting_trx_idINNER JOIN information_schema.innodb_locks lb ON lb.lock_trx_id = w.blocking_trx_idINNER JOIN information_schema.innodb_locks lr ON lr.lock_trx_id = w.requesting_trx_id \G;"echo "================= 等待事件 ==============================="top_event_10="select event_name, count_star, sum_timer_wait from performance_schema.events_waits_summary_global_by_event_name where count_star > 0 order by sum_timer_wait desc limit 10;"echo "等待事件 TOP 10:"${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "${top_event_10}"echo "==================最近一周的错误日志 =========================="_time=$(date -d '6 days ago' +%Y-%m-%d)\|$(date -d '5 days ago' +%Y-%m-%d)\|$(date -d '4 days ago' +%Y-%m-%d)\|$(date -d '3 days ago' +%Y-%m-%d)\|$(date -d '2 days ago' +%Y-%m-%d)\|$(date -d '1 days ago' +%Y-%m-%d)\|$(date -d '0 days ago' +%Y-%m-%d)log_error=$(${base}/bin/mysql -h${host} -u${userName} -p${password} ${dbname} -P${port} -e "show variables like 'log_error';"|grep  'log_error'|awk '{print $2}')#grep -i -E 'error' /home/logs/mysql/mysqld.err* | grep -E '2019-03-28|2019-06-14'grep -i -E "error" ${log_error}| grep -E "${_time}"

    该脚本可以放在任意位置,然后赋予X执行权限进行一键巡检,同时也可以设置定时任务,定时进行巡检并输出相关报告。

    ./mysql_check.sh>>/data/check.txt 2>&1

这篇关于分享一个实用的MySQL一键巡检脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1018407

相关文章

微信公众号脚本-获取热搜自动新建草稿并发布文章

《微信公众号脚本-获取热搜自动新建草稿并发布文章》本来想写一个自动化发布微信公众号的小绿书的脚本,但是微信公众号官网没有小绿书的接口,那就写一个获取热搜微信普通文章的脚本吧,:本文主要介绍微信公众... 目录介绍思路前期准备环境要求获取接口token获取热搜获取热搜数据下载热搜图片给图片加上标题文字上传图片

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

MySQL大表数据的分区与分库分表的实现

《MySQL大表数据的分区与分库分表的实现》数据库的分区和分库分表是两种常用的技术方案,本文主要介绍了MySQL大表数据的分区与分库分表的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有... 目录1. mysql大表数据的分区1.1 什么是分区?1.2 分区的类型1.3 分区的优点1.4 分

MySQL错误代码2058和2059的解决办法

《MySQL错误代码2058和2059的解决办法》:本文主要介绍MySQL错误代码2058和2059的解决办法,2058和2059的错误码核心都是你用的客户端工具和mysql版本的密码插件不匹配,... 目录1. 前置理解2.报错现象3.解决办法(敲重点!!!)1. php前置理解2058和2059的错误

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE