How to implement Mysql delayed replication with shell
This article mainly introduces "how to use shell to achieve Mysql delay replication". In daily operation, I believe many people have doubts about how to use shell to achieve Mysql delay replication. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use shell to achieve Mysql delay replication". Next, please follow the editor to study!
This script initially implements the delayed replication of mysql, and will continue to strengthen it in the future.
Lag_minute=30
Sleep_time=5
Binlogdir=/usr/local/mysql/data
Cd $binlogdir
Lag_time= `expr $lag_minute * 60`
While [0-eq 0]
Do
Relay_pos_str=$ (echo `mysql-e "show slave statusG" | grep-I Relay_Log_ Pos`)
Relay_pos_val= `expr substr "$relay_pos_str" 16 30`
Echo $relay_pos_val > relay_pos_val.his
Relay_file_str=$ (echo `mysql-e "show slave statusG" | grep-I Relay_Log_ File`)
Relay_file_val= `expr substr "$relay_file_str" 17 50`
Echo $relay_file_val > relay_file_val.his
Cur_ts_string= `/ usr/local/mysql/bin/mysqlbinlog-j "$relay_pos_val"$relay_file_val" | grep "SET TIMESTAMP" | sed-n '1p'`
# echo $cur_ts_string
Cur_ts= `expr substr "$cur_ts_string" 15 10`
Echo "exec timestamp is $cur_ts"
While [`echo ${# cur_ts} `- eq 0]
Do
Echo "can not get timestamp,wait and try again"
Sleep 10
Relay_pos_val= `cat relay_pos_ val.his`
Relay_file_val= `cat relay_file_ val.his`
Cur_ts_string= `/ usr/local/mysql/bin/mysqlbinlog-j "$relay_pos_val"$relay_file_val" | grep "SET TIMESTAMP" | sed-n '1p'`
Cur_ts= `expr substr "$cur_ts_string" 15 10`
Echo "exec timestamp is $cur_ts"
Done
Local_ts= `mysql-e "select unix_timestamp ()" | sed-n '2p' `
Target_ts= `expr $lag_time + $cur_ Ts`
Echo "local timestamp is $local_ts"
Echo "target timestamp is $target_ts"
Slave_flag= `mysqladmin extended-status | grep Slave_running | grep-ic on`
If [$target_ts-gt $local_ts]
Then
If [$slave_flag-eq 1]
Then
Mysql-e "stop slave sql_thread"
Echo "slave stopped"
Fi
Else
If [$slave_flag-eq 0]
Then
Mysql-e "start slave sql_thread"
Echo "slave started"
Fi
Fi
Sleep $sleep_time
Done
At this point, the study on "how to use shell to achieve Mysql delayed replication" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!