这篇文章主要介绍“如何用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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。