Written question Shell

Source: Internet
Author: User
Tags mysql view
Enterprise Practice question 1: (Production actual case): monitor MySQL master-slave synchronization is abnormal, if abnormal, send sms or mail to the administrator. Note: If there is no master-slave synchronization environment, you can use the following text in the file to read to simulate:
Phase 1: Develop a daemon script to detect every 30 seconds.
Phase 2: If the following error number (1158,1159,1008,1007,1062) is present in the synchronization, the error is skipped.

Phase 3: Use array technology to implement the above script (get master-slave judgment and error number section)

MySQL View status statement

Mysql>show slave Status\g

1. Row ***************************
Slave_io_state:waiting for Master to send event
master_host:10.0.0.42
Master_user:backup
master_port:3306
Connect_retry:60
master_log_file:mysql-bin.000001
read_master_log_pos:538
relay_log_file:mysql-relay-bin.000002
relay_log_pos:701
relay_master_log_file:mysql-bin.000001
Slave_io_running:yes
Slave_sql_running:yes


Use the MYSQLCMD2 command to filter after statements:

Yes
Yes
0
0
It


#!/bin/bash
port=3306 
error= (1158 1159 1008 1007 1062) #错误编号的数组
mysqlcmd1= mysql-uroot-p192.168.51.9-e< C3/>mysqlcmd2= mysql-uroot-p192.168.51.9-e "show slave Status\g" |egrep "_running| last_errno| Behind_master "|awk ' {print $NF} '

#检查mysql是否运行
function Is_run () {
 [' lsof-i: $port |wc-l '-lt 2]&&{
 echo "MySQL is stop"
 exit 1
  }
}

#提取mysql slave state to array
function Mysql_status () {
 array= (' $MYSQLCMD 2 ')
 }

#判断错误类型, are the few error numbers that need to be excluded
function Judge_error () {for
I in ${error[*]} do 
  if ["${array[2]}" = = "$i" ];then
   $MysqlCmd 1 "stop slave; SET GLOBAL sql_slave_skip_counter = 1;start SLAVE; "
  else
   echo "MySQL is failed,error ID was ${array[2]}"
  fi
Done
}

#判断 mysql slave status
judge_status () {
mysql_status
echo ${array[*]}
if ["${array[0]}" = = "Yes"-a "${array [1]} " = = "Yes"-a "${array[3]}" = = "0"];then
    echo "Mysql slave is OK"
else
    judge_error ${array[2]}
fi
}

#主函数
Function Main () {while
true
 do
   is_run
   judge_status
   sleep

Main





Enterprise Practice Question 2:
Use the For loop in the/oldboy directory to create 10 HTML files in bulk by using a random lowercase 10-letter plus a fixed string oldboy, for example:

Coaolvajcq_oldboy.html qnvuxvicni_oldboy.html

#!/bin/bash
Dir=/oldboy
[!-D $dir] && mkdir-p $dir
CD $dir


For ((i=1;i<=10;i++))
Do
A= ' Tr-dc "A-Z" </dev/urandom | Head-c ' _oldboy,html
Touch $a
Done

#tr-DC means to extract and delete characters from A to Z in a string














Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.