MySQL

做最专业的DBA

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

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



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/mysqlbinlog $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]# 
阅读更多

扫码向博主提问

去开通我的Chat快问

mchdba

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • MySQL Hbas
  • Hbase
  • Tidb
  • cdb
  • 数据拆分
个人分类: - - - MySQL Replication
想对作者说点什么? 我来说一句

数据库的异地复制和同步

2009年07月10日 3.54MB 下载

数据库<em>异地</em>导入导出

2018年03月23日 0B 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭