企業面試題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
查看郵件收發,接收以下狀態: