binlog-server搭建

喜欢而已 提交于 2020-03-06 00:59:44

binlog-server介绍

binlog在备份中起着至关重要的作用,备份binlog文件时,只能先在本地备份,然后才能传送到远程服务器上。从MySQL5.6版本后,可以利用mysqlbinlog命令把远程机器的日志备份到本地目录,这样就更加方便地实现binlog日志的安全备份。

  • -R | –read-from-remote-server

  表示开启binlog备份,在对应的主节点上请求binlog到本地。

  • -r | –result-file

  指定目录或文件名: 若指定了–raw参数,-r的值指定binlog的存放目录和文件名前缀;若没有指定–raw参数,-r的值指定文本存放的目录和文件名。

  • -t

  这个选项代表从指定的binlog开始拉取,直到当前主节点上binlog的最后一个。

  • –stop-never

  持续连续从主节点拉取binlog,持续备份到当前最后一个,并继续下去。该参数包含-t

  • –stop-never-slave-server-id

  默认值65535,用于在多个mysqlbinlog进程或者从服务器的情况下,避免ID冲突。

MySQL server

#准备工作:创建wei账号,授权replication权限
grant replication slave  on *.* to wei@'10.0.0.%' identified by '123';

#查看当前二进制文件
db01 [(none)]>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000002 |     1019 |              |                  | 5dfe203f-5edc-11ea-a789-000c291f7357:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

binlog server

[root@db01 ~]# mkdir  /data/backup
[root@db01 ~]# cd /data/backup
[root@db01 /data/backup]# mysqlbinlog  -R --host=10.0.0.51 --user=wei --password=123 \
--raw  -r /data/backup/  --stop-never mysql-bin.000001 &
[1] 7647
[root@db01 /data/backup]# mysqlbinlog: [Warning] Using a password on the command line interface can be insecure.

MySQL server 更新日志

db01 [(none)]>flush binary logs;
Query OK, 0 rows affected (0.00 sec)

db01 [(none)]>flush binary logs;
Query OK, 0 rows affected (0.01 sec)

db01 [(none)]>flush binary logs;
Query OK, 0 rows affected (0.00 sec)

db01 [(none)]>flush binary logs;
Query OK, 0 rows affected (0.01 sec)

db01 [(none)]>flush binary logs;
Query OK, 0 rows affected (0.00 sec)

db01 [(none)]>show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000009 |      194 |              |                  | 5dfe203f-5edc-11ea-a789-000c291f7357:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)

binlog server查看

[root@db01 /data/backup]# ll
total 36
-rw-r----- 1 root root  177 Mar  5 23:40 mysql-bin.000001
-rw-r----- 1 root root 1066 Mar  5 23:40 mysql-bin.000002
-rw-r----- 1 root root  217 Mar  5 23:40 mysql-bin.000003
-rw-r----- 1 root root  241 Mar  5 23:40 mysql-bin.000004
-rw-r----- 1 root root  241 Mar  5 23:40 mysql-bin.000005
-rw-r----- 1 root root  241 Mar  5 23:40 mysql-bin.000006
-rw-r----- 1 root root  241 Mar  5 23:40 mysql-bin.000007
-rw-r----- 1 root root  241 Mar  5 23:40 mysql-bin.000008
-rw-r----- 1 root root  194 Mar  5 23:40 mysql-bin.000009

问题于解决

如果mysqlbinlog断了怎么办,并不像主从一样取尝试重连

思路:将mysqlbinlog写在一个死循环里,断了就发起重新执行

#!/bin/sh
BACKUP_BIN=/usr/bin/mysqlbinlog
LOCAL_BACKUP_DIR=/backup/binlog/
BACKUP_LOG=/backup/binlog/backuplog

REMOTE_HOST=192.168.244.145
REMOTE_PORT=3306
REMOTE_USER=repl
REMOTE_PASS=repl
FIRST_BINLOG=mysql-bin.000001

#time to wait before reconnecting after failure
SLEEP_SECONDS=10

##create local_backup_dir if necessary
mkdir -p ${LOCAL_BACKUP_DIR}
cd ${LOCAL_BACKUP_DIR}

## 运行while循环,连接断开后等待指定时间,重新连接
while :
do
  if [ `ls -A "${LOCAL_BACKUP_DIR}" |wc -l` -eq 0 ];then
     LAST_FILE=${FIRST_BINLOG}
  else
     LAST_FILE=`ls -l ${LOCAL_BACKUP_DIR} | grep -v backuplog |tail -n 1 |awk '{print $9}'`
  fi
  ${BACKUP_BIN} --raw --read-from-remote-server --stop-never --host=${REMOTE_HOST} --port=${REMOTE_PORT} --user=${REMOTE_USER} --password=${REMOTE_PASS} ${LAST_FILE}

  echo "`date +"%Y/%m/%d %H:%M:%S"` mysqlbinlog停止,返回代码:$?" | tee -a ${BACKUP_LOG}
  echo "${SLEEP_SECONDS}秒后再次连接并继续备份" | tee -a ${BACKUP_LOG}  
  sleep ${SLEEP_SECONDS}
done

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!