MySQL 不开启slave如何完成异地复制

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 不开启slave如何完成异地复制相关的知识,希望对你有一定的参考价值。

1,分批次通过远程的binlog来进行数据加载

业务新需求,线上数据库数据拉到本次,但是不允许开启slave服务,不建立直接外网的数据库账号,也不能打通数据库对外网的网络,所以我们测试环境无法通过普通的slave主从复制机制来获得实时数据。


想了一个办法,将binlog放在一个可获取的安全服务器上,然后把binlog拉到测试环境,在测试环境解析binlog加载数据。



2,远程获取binlog日志记录

写一个远程能获取binlog日志记录的脚本,一个小时拉一次binlog记录

[[email protected]]# more get_binlog.sh 
#!/bin/bash

loadlogfile=/home/mysql/binlogs/bright/get_binlogs.log
dir=$(date +%Y%m%d%H)
echo "---- "$dir" begin-----------------------------------------------------------------------------------------">> $loadlogfile 
mkdir -p /home/mysql/binlogs/bright/$dir
echo "begin to get bright binlogs ......"  >> $loadlogfile
ftp -n<<!
open 120.132.27.91 21
user downdata RakudespuH3bAk+ruy
binary
cd bright/$dir
lcd /home/mysql/binlogs/bright/$dir
prompt
mget mysql-bin*
close
bye
!

ls -l /home/mysql/binlogs/bright/$dir >> $loadlogfile

echo $(date +%Y-%m-%d-%H:%M:%S) >> $loadlogfile
echo "get binlogs end ..." >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29



3,加载远程过来的binlog

在上个step2之后,立即加载binlog,获得数据显示

#sh load_data_frombinlog.sh $dir 



if [ ! -n "$1" ]; then
        echo -e "需输入日期参数如:2017060909\n"
        echo -e "需输入日期参数如:2017060909\n"  >> $loadlogfile

#        exit
fi

loadlogfile=/home/mysql/binlogs/bright/bright_load_data.log
filename=`date ‘+%Y%m%d_%H%M%S‘`.txt
#find /home/mysql/binlogs/bright -amin -1300 -name mysql-bin.*.zip |sort -n> $filename
find /home/mysql/binlogs/bright/$dir -name mysql-bin.*.zip |sort -n> $filename
echo `date ‘+%Y-%m-%d %H:%M:%S‘`  >> $loadlogfile
echo "begin to load data from binlog ......" >> $loadlogfile
echo $filename >> $loadlogfile
for binlogfile in `cat $filename`
do
    echo ""
    echo "begin to load binlog"  >> $loadlogfile
    echo $binlogfile
    chmod +x  $binlogfile
    dir_binlog=`/usr/bin/dirname $binlogfile`
    realbinlogfile=${binlogfile%.zip}
    rm -rf $realbinlogfile
    unzip $binlogfile -d  $dir_binlog
    echo "unzip binlog $binlogfile"  >> $loadlogfile
    echo "binlog file is: $realbinlogfile" >> $loadlogfile
    rm -rf $realbinlogfile.sql
    /usr/local/mysql3309/bin/m7888500ysqlbinlog $realbinlogfile > $realbinlogfile.sql
    echo "create sql file $realbinlogfile.sql from $realbinlogfile" >> $loadlogfile
    echo "begin to load data" >> $loadlogfile
    #mysqlruninfo=`/usr/local/mysql3309/bin/mysql -uroot --password="bright_yueworld" -S /usr/local/mysql3309/mysql.sock < $realbinlogfile.sql`
    #$mysqlruninfo >> $loadlogfile
    /usr/local/mysql3309/bin/mysql -uroot --password="bright_yueworld" -S /usr/local/mysql3309/mysql.sock < $realbinlogfile.sql
    #echo $mysqlruninfo >> $loadlogfile
    echo "load $realbinlogfile data end" >> $loadlogfile
    echo "" >> $loadlogfile
    echo "" >> $loadlogfile
done
echo "---- "$dir" end----------------------------------------------------------------------------------------------" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
echo "" >> $loadlogfile
[[email protected]]# 





以上是关于MySQL 不开启slave如何完成异地复制的主要内容,如果未能解决你的问题,请参考以下文章

MySQL5.7.21开启Gtid配置主从复制

mysql主从异步复制过程

MySQL master-slave主从复制环境搭建初试

MySQL高可用主从复制新增slave

数据库 之 Mysql复制概念介绍

mysql5.7 开启增强半同步复制