原创MySQL运维脚本跟一些常用SQL的分享--持续更新

这篇博客分享了一组MySQL运维脚本,包括数据库状态收集、实例名修改、binlog清理和DB巡检,以及一些DBA常用的SQL查询。脚本详细介绍了其功能和使用方法,并提供了实例展示。
摘要由CSDN通过智能技术生成

数据库状态收集脚本:

该脚本动态收集实例状态数据,可前端展示,也可收集到后台日志,该脚本目前还处于完善阶段,还有很多状态可以收集,中途忙其他事情去了,后面有时间了继续更新。脚本效果展示:
前端展示
在这里插入图片描述
收集到后台日志:
在这里插入图片描述
帮助信息:
在这里插入图片描述
脚本内容:
vi dbck.sh

#!/bin/bash
db_host=
db_port=
db_user=
db_pwd=
dbexec=“mysql -h ${db_host} -P ${db_port} -u d b u s e r − p {db_user} -p dbuserp{db_pwd} information_schema -Ne”
sum1=0
sum2=0
js=0
rm -rf /tmp/m2019_kfdba_status1.txt /tmp/m2019_kfdba_status2.txt

#111
function f_main(){
${dbexec}“select lower(variable_name),variable_value from global_status” 2>/dev/null > /tmp/m2019_kfdba_status1.txt
vquerys1=grep -w 'com_select' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vupdate1=grep -w 'com_insert' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vinsert1=grep -w 'com_update' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vdelete1=grep -w 'com_delete' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vslowqs1=grep -w 'slow_queries' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vinnolockws1=grep -w 'innodb_row_lock_waits' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vrecivebytes1=grep -w 'bytes_received' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vsendbytes1=grep -w 'bytes_sent' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vphysicalread1=grep -w 'innodb_buffer_pool_reads' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vsyncdata1=grep -w 'innodb_data_fsyncs' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vsynclog1=grep -w 'innodb_os_log_fsyncs' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vdisktmps1=grep -w 'created_tmp_disk_tables' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'
vscan1=grep -w 'select_scan' /tmp/m2019_kfdba_status1.txt | awk '{print $2}'

function f_echo() #print head
{
echo “-----datetime------|-----------------------------------------------|-thread-|–lock—|-network(bytes)-|------innodb------|”
echo “-------------------|–qps–tps----len-slow–s10–s60-s300-scan-dtmp|–tr–tc|-wis-cwis|------in-----out|-pread-sdata–slog|”
}

f_echo
while [ 1 ]
do
if [ j s − e q 30 ] ; t h e n f e c h o j s = 0 f i s l e e p 1 l e t j s = j s + 1 v t i m e = ‘ d a t e + l e n = ‘ {js} -eq 30 ];then f_echo js=0 fi sleep 1 let js=js+1 vtime=`date +%Y-%m-%d" "%H:%M:%S` len=` jseq30];thenfechojs=0fisleep1letjs=js+1vtime=date+len={dbexec}“select count from information_schema.innodb_metrics where name=‘trx_rseg_history_len’” 2>/dev/null${dbexec}"select lower(variable_name),variable_value from global_status" 2>/dev/null > /tmp/m2019_kfdba_status2.txt vquerys2=grep -w ‘com_select’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vupdate2=grep -w ‘com_insert’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vinsert2=grep -w ‘com_update’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vdelete2=grep -w ‘com_delete’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vslowqs2=grep -w ‘slow_queries’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vinnolockws2=grep -w ‘innodb_row_lock_waits’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vrecivebytes2=grep -w ‘bytes_received’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vsendbytes2=grep -w ‘bytes_sent’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vphysicalread2=grep -w ‘innodb_buffer_pool_reads’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vsyncdata2=grep -w ‘innodb_data_fsyncs’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vsynclog2=grep -w ‘innodb_os_log_fsyncs’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vdisktmps2=grep -w ‘created_tmp_disk_tables’ /tmp/m2019_kfdba_status2.txt | awk '{print $2}'vscan2=grep -w ‘select_scan’ /tmp/m2019_kfdba_status2.txt | awk ‘{print $2}’`

cwis=grep -w 'innodb_row_lock_current_waits' /tmp/m2019_kfdba_status2.txt | awk '{print $2}'
tr=grep -w 'threads_running' /tmp/m2019_kfdba_status2.txt | awk '{print $2}'
tc=grep -w 'threads_connected' /tmp/m2019_kfdba_status2.txt | awk '{print $2}'
sall=${dbexec}"select count(*) from information_schema.processlist where command='Query' and time >300 union all select count(*) from information_schema.processlist where command='Query' and time >60 union all select count(*) from information_schema.processlist where command='Query' and time > 10;" 2>/dev/null
s10=echo ${sall} | awk '{print $3}'
s60=echo ${sall} | awk '{print $2}'
s300=echo ${sall} | awk '{print $1}'
let tps=vupdate2+vinsert2+vdelete2-vupdate1-vinsert1-vdelete1
let qps=vquerys2-vquerys1
let slow=vslowqs2-vslowqs1
let wis=vinnolockws2-vinnolockws1
let in=vrecivebytes2-vrecivebytes1
let out=vsendbytes2-vsendbytes1
let pread=vphysicalread2-vphysicalread1
let sdata=vsyncdata2-vsyncdata1
let slog=vsynclog2-vsynclog1
let dtmp=vdisktmps2-vdisktmps1
let scan=vscan2-vscan1
#echo ${vrecivebytes2} ${vsendbytes2} ${vrecivebytes1} ${vsendbytes1}

vquerys1= v q u e r y s 2 ; v u p d a t e 1 = {vquerys2};vupdate1= vquerys2;vupdate1={vupdate2};vdelete1= v d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值