您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何用shell實現Mysql延時復制”,在日常操作中,相信很多人在如何用shell實現Mysql延時復制問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何用shell實現Mysql延時復制”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
這個腳本初步實現了mysql的延時復制,以后還會繼續加強
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
到此,關于“如何用shell實現Mysql延時復制”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。