码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2017-08-12 15:31:06      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:ret   无法   int   外网   tle   inf   word   ash   home   

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

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


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



2,远程获取binlog日志记录

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

[root@db12162bright]# 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
[root@db12162bright]# 

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

标签:ret   无法   int   外网   tle   inf   word   ash   home   

原文地址:http://www.cnblogs.com/xiaoliu500/p/7350219.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!