企业面试题1:(生产实战案例):监控MySQL主从同步是否异常,若是异常,则发送短信或者邮件给管理员。提示:若是没主从同步环境,能够用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:若是同步出现以下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)mysql
1 [root@mysql01 shell]# mysql -uroot -poldboy123 -S /data/3307/mysql.sock -e "show slave status\G" 2 Warning: Using a password on the command line interface can be insecure. 3 *************************** 1. row *************************** 4 Slave_IO_State: Waiting for master to send event 5 Master_Host: 172.16.1.52 6 Master_User: rep 7 Master_Port: 3306 8 Connect_Retry: 60 9 Master_Log_File: mysql-bin.000002 10 Read_Master_Log_Pos: 120 11 Relay_Log_File: relay-bin.000002 12 Relay_Log_Pos: 283 13 Relay_Master_Log_File: mysql-bin.000002 14 Slave_IO_Running: Yes 15 Slave_SQL_Running: Yes 16 Replicate_Do_DB: 17 Replicate_Ignore_DB: 18 Replicate_Do_Table: 19 Replicate_Ignore_Table: 20 Replicate_Wild_Do_Table: 21 Replicate_Wild_Ignore_Table: 22 Last_Errno: 0 23 Last_Error: 24 Skip_Counter: 0 25 Exec_Master_Log_Pos: 120 26 Relay_Log_Space: 450 27 Until_Condition: None 28 Until_Log_File: 29 Until_Log_Pos: 0 30 Master_SSL_Allowed: No 31 Master_SSL_CA_File: 32 Master_SSL_CA_Path: 33 Master_SSL_Cert: 34 Master_SSL_Cipher: 35 Master_SSL_Key: 36 Seconds_Behind_Master: 0 37 Master_SSL_Verify_Server_Cert: No 38 Last_IO_Errno: 0 39 Last_IO_Error: 40 Last_SQL_Errno: 0 41 Last_SQL_Error: 42 Replicate_Ignore_Server_Ids: 43 Master_Server_Id: 12 44 Master_UUID: 74e721f9-3c82-11e8-a818-000c29a97a9f 45 Master_Info_File: /data/3307/data/master.info 46 SQL_Delay: 0 47 SQL_Remaining_Delay: NULL 48 Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 49 Master_Retry_Count: 86400 50 Master_Bind: 51 Last_IO_Error_Timestamp: 52 Last_SQL_Error_Timestamp: 53 Master_SSL_Crl: 54 Master_SSL_Crlpath: 55 Retrieved_Gtid_Set: 56 Executed_Gtid_Set: 57 Auto_Position: 0
一、编写shell脚本:面试
系统:centos6.7sql
Mysql:多实例主从同步shell
#!/bin/bash #-------------CopyRight------------- # Name:MySQL Check master and slave # Version Number:1.00 # Type:sh # Language:bash shell # Date:2018-05-09 # Author:xubing # QQ:442656067 # Email:eeexu123@163.com # Blog:https://www.cnblogs.com/eeexu123/ Port=3307 //MySQL端口 User="root" //MySQL用户 Password="oldboy123" //MySQL用户密码 Mysql_sock="/data/${Port}/mysql.sock" //mysql.sock随着每次MySQL启动而生成 Mysql_cmd="/application/mysql/bin/mysql -u${User} -p${Password} -S $Mysql_sock -e" //MySQL非交互命令 Error_file="/tmp/mysql_check_error.log" //错误输入文件 #source functions libary . /etc/init.d/functions #check mysql server //检查MySQL是否启动 [ -e $Mysql_sock ]||{ echo "The Mysql server no start" exit 1 } #function ingore errors //忽略错误函数 skip_errors(){ array=(1158 1159 1008 1007 1062) flag=0 for num in ${array[*]} do if [ "$1" = "$num" ];then //若是有对应的错误号,MySQL就跳过此错误号 ${Mysql_cmd} "stop slave;set global sql_slave_skip_counter = 1;start slave;" echo "Last_IO_Errno:$1">>$Error_file else echo "Last_IO_Errno:$1">>$Error_file ((flag++)) //若是此错误号不在array数组中,将此错误号写入错误文件中,并循环五次 fi done if [ $flag = ${#array[@]} ];then //发送邮件 echo "**********`date +%F_%T`************">>$Error_file uniq $Error_file|mail -s "Mysql Slave error" eeexu123@163.com fi } #check mysql slave //检查从库是否正常 check_mysql(){ array1=(`${Mysql_cmd} "show slave status\G"|egrep "Slave_IO_Running|Slave_SQL_Running|Last_SQL_Errno|Seconds_Behind_Master"|awk '{print $NF}'`) if [ "${array1[0]}" = "Yes" -a "${array1[1]}" == "Yes" -a "${array1[2]}" = 0 ];then action "Mysql Slave" /bin/true else action "Mysql Slave" /bin/false if [ "${array1[0]}" != "Yes" ];then //将上述错误的列写入到错误文件中 ${Mysql_cmd} "show slave status\G"|grep "Slave_IO_Running">>$Error_file elif [ "${array1[1]}" != "Yes" ];then ${Mysql_cmd} "show slave status\G"|grep "Slave_SQL_Running"|grep -v "Slave_SQL_Running_State">>$Error_file else [ "${array1[2]}" != 0 ] ${Mysql_cmd} "show slave status\G"|grep "Seconds_Behind_Master">>$Error_file fi skip_errors ${array1[3]} //发送邮件 fi } main(){ while true do check_mysql sleep 60 done } main
二、检测(执行脚本)centos
a:检测忽略号数组
先在从库3307中建立库bash
mysql> create database taili123; Query OK, 1 row affected (0.00 sec)
再在主库3306中建立库app
mysql> create database taili123; Query OK, 1 row affected (0.00 sec)
再次查看从库状态以下:出现错误代码函数
1 mysql> show slave status\G 2 *************************** 1. row *************************** 3 Slave_IO_State: Waiting for master to send event 4 Master_Host: 172.16.1.52 5 Master_User: rep 6 Master_Port: 3306 7 Connect_Retry: 60 8 Master_Log_File: mysql-bin.000002 9 Read_Master_Log_Pos: 977 10 Relay_Log_File: relay-bin.000009 11 Relay_Log_Pos: 370 12 Relay_Master_Log_File: mysql-bin.000002 13 Slave_IO_Running: Yes 14 Slave_SQL_Running: No 15 Replicate_Do_DB: 16 Replicate_Ignore_DB: 17 Replicate_Do_Table: 18 Replicate_Ignore_Table: 19 Replicate_Wild_Do_Table: 20 Replicate_Wild_Ignore_Table: 21 Last_Errno: 1007 22 Last_Error: Error 'Can't create database 'taili123'; database exists' on query. Default database: 'taili123'. Query: 'create database taili123' 23 Skip_Counter: 0 24 Exec_Master_Log_Pos: 871 25 Relay_Log_Space: 1233 26 Until_Condition: None 27 Until_Log_File: 28 Until_Log_Pos: 0 29 Master_SSL_Allowed: No 30 Master_SSL_CA_File: 31 Master_SSL_CA_Path: 32 Master_SSL_Cert: 33 Master_SSL_Cipher: 34 Master_SSL_Key: 35 Seconds_Behind_Master: NULL 36 Master_SSL_Verify_Server_Cert: No 37 Last_IO_Errno: 0 38 Last_IO_Error: 39 Last_SQL_Errno: 1007 40 Last_SQL_Error: Error 'Can't create database 'taili123'; database exists' on query. Default database: 'taili123'. Query: 'create database taili123' 41 Replicate_Ignore_Server_Ids: 42 Master_Server_Id: 12 43 Master_UUID: 74e721f9-3c82-11e8-a818-000c29a97a9f 44 Master_Info_File: /data/3307/data/master.info 45 SQL_Delay: 0 46 SQL_Remaining_Delay: NULL 47 Slave_SQL_Running_State: 48 Master_Retry_Count: 86400 49 Master_Bind: 50 Last_IO_Error_Timestamp: 51 Last_SQL_Error_Timestamp: 180516 08:43:37 52 Master_SSL_Crl: 53 Master_SSL_Crlpath: 54 Retrieved_Gtid_Set: 55 Executed_Gtid_Set: 56 Auto_Position: 0
查看脚本执行状态:spa
1 [root@mysql01 shell]# sh ckmysql_master_slave.sh 2 Warning: Using a password on the command line interface can be insecure. 3 Mysql Slave [失败] 4 Warning: Using a password on the command line interface can be insecure. 5 Warning: Using a password on the command line interface can be insecure. 6 Warning: Using a password on the command line interface can be insecure. 7 Mysql Slave [失败] 8 Warning: Using a password on the command line interface can be insecure. 9 Warning: Using a password on the command line interface can be insecure. 10 Warning: Using a password on the command line interface can be insecure. 11 Mysql Slave [肯定]
b:检测发送邮件
在从库中中止SQL线程
mysql> stop slave sql_thread; Query OK, 0 rows affected (0.02 sec)
再次查看从库状态
1 mysql> show slave status\G 2 *************************** 1. row *************************** 3 Slave_IO_State: Waiting for master to send event 4 Master_Host: 172.16.1.52 5 Master_User: rep 6 Master_Port: 3306 7 Connect_Retry: 60 8 Master_Log_File: mysql-bin.000002 9 Read_Master_Log_Pos: 977 10 Relay_Log_File: relay-bin.000011 11 Relay_Log_Pos: 283 12 Relay_Master_Log_File: mysql-bin.000002 13 Slave_IO_Running: Yes 14 Slave_SQL_Running: No 15 Replicate_Do_DB: 16 Replicate_Ignore_DB: 17 Replicate_Do_Table: 18 Replicate_Ignore_Table: 19 Replicate_Wild_Do_Table: 20 Replicate_Wild_Ignore_Table: 21 Last_Errno: 0 22 Last_Error: 23 Skip_Counter: 0 24 Exec_Master_Log_Pos: 977 25 Relay_Log_Space: 613 26 Until_Condition: None 27 Until_Log_File: 28 Until_Log_Pos: 0 29 Master_SSL_Allowed: No 30 Master_SSL_CA_File: 31 Master_SSL_CA_Path: 32 Master_SSL_Cert: 33 Master_SSL_Cipher: 34 Master_SSL_Key: 35 Seconds_Behind_Master: NULL 36 Master_SSL_Verify_Server_Cert: No 37 Last_IO_Errno: 0 38 Last_IO_Error: 39 Last_SQL_Errno: 0 40 Last_SQL_Error: 41 Replicate_Ignore_Server_Ids: 42 Master_Server_Id: 12 43 Master_UUID: 74e721f9-3c82-11e8-a818-000c29a97a9f 44 Master_Info_File: /data/3307/data/master.info 45 SQL_Delay: 0 46 SQL_Remaining_Delay: NULL 47 Slave_SQL_Running_State: 48 Master_Retry_Count: 86400 49 Master_Bind: 50 Last_IO_Error_Timestamp: 51 Last_SQL_Error_Timestamp: 52 Master_SSL_Crl: 53 Master_SSL_Crlpath: 54 Retrieved_Gtid_Set: 55 Executed_Gtid_Set: 56 Auto_Position: 0
查看邮件收发,接收以下状态: