mysql数据库备份设置延时备份方法(mysql主从配置)

一 为什么需要延时备份

percona-xtrabackup是一个优秀的用于增量备份的工具。今天我们讲到的延时备份也是使用他们的产品。
以前在MySQL AB复制一文中提到了AB复制。我们首先回顾下MySQL复制的相关要点。AB复制又称主从复制,实现的是数据同步。经过以下步骤:

1)主服务器把数据更改记录到二进制日志中,这个操作叫做二进制日志事件;

2)从服务器把主服务器的二进制日志事件拷贝到自己的中继日志(relay log)中;

3)从服务器执行中继日志中的事件,把更改应用到自己的数据上。

在生产中,我们在使用 mysql AB 复制技术不但可以起到数据库层面负载均衡的能力,还可以起到备份数据的功能,但有的时候我们可能由于不小心误操作导致数据被删除,这这个时候 slave服务器上的数据也会同时被删除,如果我们能够能是的其中的一台 slave 延时备份的话, 这样就可以从 slave服务器上找回被误删的数据了。
从服务器到主服务器中拷贝二进制日志文件,如果在并发量高,网络延时严重的情况下,会对主服务器造成相当大的压力,负载高,必定会出现很多问题,比如访问延迟,IO瓶颈,网络拥塞等等。服务器压力过大是我们都不愿看到的情况,那有没有方案缓解这种情况呢?有,这就是本文讲到的延时备份。延时备份通过第三方工具,将检查同步和真正同步的时间控制在一定的范围内,而不是主服务器数据发生变化,从服务器立即去同步二进制事件到自己的中继日志中,这样的话可以大大减轻主服务器的压力,并且基于AB复制的优点,可以达到备份数据的目的。

环境简介

serv01:主服务器 192.168.1.11serv01.host.com

serv08:及时同步服务器 192.168.1.18serv01.host.com

serv09:延时同步服务器 192.168.1.19serv08.host.com

操作系统版本

RHEL Server6.1 64位系统

使用到的软件包版本

mysql-5.5.29-linux2.6-x86_64.tar.gz

percona-toolkit-2.1.7-1.noarch.rpm

第一步,搭建环境。修改配置文件,注意每台的server-id不一致;

[[email protected] ~]# cat /etc/my.cnf | grep server-id
server-id = 1
#server-id       = 2
[[email protected] ~]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!

[[email protected] ~]# cat /etc/my.cnf | grep server-id
server-id = 2
#server-id       = 2
[[email protected] ~]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!

[[email protected] ~]# cat /etc/my.cnf | grep server-id
server-id = 3
#server-id       = 2
[[email protected] ~]# /etc/init.d/mysqld start
Starting MySQL SUCCESS!

第二步,serv01serv08 serv09清空日志

serv01
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       683 |
+------------------+-----------+
1 row in set (0.01 sec)

mysql> reset master;
Query OK, 0 rows affected (0.01 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       107 |
+------------------+-----------+
1 row in set (0.00 sec)

serv08
mysql> reset master;
Query OK, 0 rows affected (0.02 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       107 |
+------------------+-----------+
1 row in set (0.00 sec)

serv09
mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       107 |
+------------------+-----------+
1 row in set (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.00 sec)

mysql> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       107 |
+------------------+-----------+
1 row in set (0.00 sec)

第三步,主服务器serv01创建授权用户

mysql> grant replication client, replication slave on *.* to ‘larry‘@‘192.168.1.%‘ identified by ‘larry‘;

第四步,serv08修改master设置,开启slave,查看slave状态

mysql> change master to
    -> master_host=‘192.168.1.11‘,
    -> master_user=‘larry‘,
    -> master_password=‘larry‘,
    -> master_port=3306,
    -> master_log_file=‘mysql-bin.000001‘,
    -> master_log_pos=107;
Query OK, 0 rows affected (0.03 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.11
                  Master_User: larry
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: serv08-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 107
              Relay_Log_Space: 410
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR:
No query specified

第五步,serv09延时服务器修改master状态,开启slave,查看slave状态

mysql> change master to master_host=‘192.168.1.11‘, master_user=‘larry‘, master_password=‘larry‘, master_port=3306, master_log_file=‘mysql-bin.000001‘, master_log_pos=107;
Query OK, 0 rows affected (0.02 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.11
                  Master_User: larry
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: serv09-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 107
              Relay_Log_Space: 410
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR:
No query specified

第六步,在没有使用延时服务器时,serv01创建测试数据库,可以看到同步服务器serv08和延时服务器serv09已经同步了

serv01
mysql> create database justdb;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| crm                |
| justdb             |
| larry              |
| larrydb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
8 rows in set (0.00 sec)

serv08
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| justdb             |
| larrydb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.03 sec)

serv09
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| justdb             |
| larry              |
| larrydb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.00 sec)

第七步,拷贝percona-toolkit-2.1.7-1.noarch.rpm

[[email protected] ule-mysql]# scp percona-toolkit-2.1.7-1.noarch.rpm 192.168.1.11:/opt
[email protected]‘s password:
percona-toolkit-2.1.7-1.noarch.rpm                       100% 1767KB   1.7MB/s   00:00

第八步,主服务器中通过yum安装percona-toolkit-2.1.7-1.noarch.rpm

[[email protected] opt]# yum install percona-toolkit-2.1.7-1.noarch.rpm -y

第九步,使用pt-slave-delay工具进行延时设置。可以先查看帮助。

[[email protected] opt]# pt-slave-delay --help
pt-slave-delay starts and stops a slave server as needed to make it lag behind
the master.  The SLAVE-HOST and MASTER-HOST use DSN syntax, and values are
copied from the SLAVE-HOST to the MASTER-HOST if omitted.  For more details,
please use the --help option, or try ‘perldoc /usr/bin/pt-slave-delay‘ for
complete documentation.

Usage: pt-slave-delay [OPTION...] SLAVE-HOST [MASTER-HOST]

Options:

--ask-pass            Prompt for a password when connecting to MySQL
  --charset=s       -A  Default character set
  --config=A            Read this comma-separated list of config files; if
                        specified, this must be the first option on the command
                        line
  --[no]continue        Continue replication normally on exit (default yes)
  --daemonize           Fork to the background and detach from the shell
  --database=s      -D  The database to use for the connection
  --defaults-file=s -F  Only read mysql options from the given file
  --delay=m             How far the slave should lag its master (default 1h).
                        Optional suffix s=seconds, m=minutes, h=hours, d=days;
                        if no suffix, s is used.
  --help                Show help and exit
  --host=s          -h  Connect to host
  --interval=m          How frequently pt-slave-delay should check whether the
                        slave needs to be started or stopped (default 1m).
                        Optional suffix s=seconds, m=minutes, h=hours, d=days;
                        if no suffix, s is used.
  --log=s               Print all output to this file when daemonized
  --password=s      -p  Password to use when connecting
  --pid=s               Create the given PID file when daemonized
  --port=i          -P  Port number to use for connection
  --quiet           -q  Don‘t print informational messages about operation
  --run-time=m          How long pt-slave-delay should run before exiting.
                        Optional suffix s=seconds, m=minutes, h=hours, d=days;
                        if no suffix, s is used.
  --set-vars=s          Set these MySQL variables (default wait_timeout=10000)
  --socket=s        -S  Socket file to use for connection
  --use-master          Get binlog positions from master, not slave
  --user=s          -u  User for login if not current user
  --version             Show version and exit
  --version-check=s     Send program versions to Percona and print suggested
                        upgrades and problems (default off)

Option types: s=string, i=integer, f=float, h/H/a/A=comma-separated list, d=DSN, z=size, m=time

Rules:

This tool accepts additional command-line arguments. Refer to the SYNOPSIS and usage information for details.

DSN syntax is key=value[,key=value...]  Allowable DSN keys:

KEY  COPY  MEANING
  ===  ====  =============================================
  A    yes   Default character set
  D    yes   Default database
  F    yes   Only read default options from the given file
  P    yes   Port number to use for connection
  S    yes   Socket file to use for connection
  h    yes   Connect to host
  p    yes   Password to use when connecting
  u    yes   User for login if not current user

If the DSN is a bareword, the word is treated as the ‘h‘ key.

Options and values after processing arguments:

--ask-pass            FALSE
  --charset             (No value)
  --config              /etc/percona-toolkit/percona-toolkit.conf,/etc/percona-toolkit/pt-slave-delay.conf,/root/.percona-toolkit.conf,/root/.pt-slave-delay.conf
  --continue            TRUE
  --daemonize           FALSE
  --database            (No value)
  --defaults-file       (No value)
  --delay               3600
  --help                TRUE
  --host                (No value)
  --interval            60
  --log                 (No value)
  --password            (No value)
  --pid                 (No value)
  --port                (No value)
  --quiet               FALSE
  --run-time            (No value)
  --set-vars            wait_timeout=10000
  --socket              (No value)
  --use-master          FALSE
  --user                (No value)
  --version             FALSE
  --version-check       off

第十步,serv09延时服务器中创建授权用户

mysql> grant all on *.* to ‘rep‘@‘192.168.1.%‘ identified by ‘larry‘;
Query OK, 0 rows affected (0.00 sec)

第十一步,实现功能。

[[email protected] ~]# pt-slave-delay --user=‘rep‘ --password=‘larry‘ --delay=3m --interval=20s --run-time=30m 192.168.1.19
2013-10-06T19:43:30 slave running 0 seconds behind
2013-10-06T19:43:30 STOP SLAVE until 2013-10-06T19:46:30 at master position mysql-bin.000001/199

<strong>命令解释</strong>
--user=‘rep‘:延时服务器中授权用户的用户名,这里设置为rep
--password=‘larry‘:延时服务器中授权用户的密码,这里设置为larry
--delay=3m:延时同步的时间,这里设置为3分钟
--interval=20s:检查同步的时间,这里设置为20s
--run-time=30m:pt-slave-delay的运行时间,这里设置为30分钟
192.168.1.19:延时服务器的IP地址

第十二步,测试,主服务器serv01创建测试数据库,可以发现同步服务器立马更新,而延时同步服务器要等3分钟之后才更新

serv01
mysql> use justdb;
Database changed
mysql> create table test(id int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into test values(1);
Query OK, 1 row affected (0.00 sec)

serv08
  mysql> select * from justdb.test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

serv09
mysql> select * from justdb.test;
ERROR 1146 (42S02): Table ‘justdb.test‘ doesn‘t exist

三分钟过后查看延时服务器已经同步成功
[[email protected] ~]# pt-slave-delay --user=‘rep‘ --password=‘larry‘ --delay=3m --interval=20s --run-time=30m 192.168.1.19
2013-10-06T19:43:30 slave running 0 seconds behind
2013-10-06T19:43:30 STOP SLAVE until 2013-10-06T19:46:30 at master position mysql-bin.000001/199
2013-10-06T19:43:50 slave stopped at master position mysql-bin.000001/199
2013-10-06T19:44:10 slave stopped at master position mysql-bin.000001/199
2013-10-06T19:44:30 slave stopped at master position mysql-bin.000001/492
2013-10-06T19:44:50 slave stopped at master position mysql-bin.000001/492
2013-10-06T19:45:10 slave stopped at master position mysql-bin.000001/492
2013-10-06T19:45:30 slave stopped at master position mysql-bin.000001/492
2013-10-06T19:45:50 slave stopped at master position mysql-bin.000001/492
。。。
2013-10-06T20:09:50 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:10:10 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:10:30 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:10:50 no new binlog events
2013-10-06T20:11:10 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:11:30 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:11:50 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:12:10 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:12:30 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:12:50 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:13:10 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:13:30 slave stopped at master position mysql-bin.000001/492
2013-10-06T20:13:30 Setting slave to run normally

mysql> select * from justdb.test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

四 附延时备份脚本

#!/bin/bash
#
# chkconfig: - 88 12
# description: the mysql ab delay scripts

host=192.168.100.54
user=rep
password=larry

delay=2m
in=15s

prog=/usr/bin/pt-slave-delay

. /etc/init.d/functions

start() {
    echo -n "Starting `basename $prog`..."
    daemon $prog --host=$host --user=$user --password=$password --delay=$delay --interval=$in --daemonize --log=/var/log/mysql-delay.log
    echo
}

stop() {
    echo -n "Stopping `basename $prog`..."
    killproc  $prog
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

时间: 2024-10-20 05:26:57

mysql数据库备份设置延时备份方法(mysql主从配置)的相关文章

MySQL数据库使用命令行备份

一.MySQL数据库使用命令行备份 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:pass 数据库名称:myweb 备份数据库到D盘跟目录 mysqldump -h127.0.0.1 -uroot -ppass myweb > d:/backupfile.sql 备份到当前目录 备份MySQL数据库为带删除表的格式,能够让该备份覆盖已有数据库而不需要手动删除原有数据库 mysqldump --add-drop-table -h127.0.0.1 -uroot -pp

mysql数据库远程同步及备份

复制解决的基本问题是让一台服务器的数据和另外的服务器保持同步.一台主服务器可以连接多台从服务器,并且从服务器也可以反过来作为主服务器.主服务器和从服务器可以位于不同的网络拓扑中,还能对整台服务器.特定的数据库,甚至特定的表进行复制.Mysql复制大部分都是向后兼容的.这意味着版本较新的服务器可以是版本较老的服务器的从服务器.但老版本的服务器通常不能是新版本号的服务器的从服务器.复制通常不会大幅增加主服务器的开销.它需要主服务器启用二进制日志,这或许会有较大的开销,但是出于备份的目的,无论如何这种

16、MySQL数据库分库分表备份脚本

MySQL数据库分库分表备份脚本 ===================学员分享分库分表========================== 脚本单双引号的区别: 单引号是强引用,强制输出是所见即所得. 双引号是解析变量 和 多个字符串.数字等连接一个字符串 条件1  ||    条件2                      或   假真   真假 条件1 && 条件2                      并   真真    假假 !条件1  && 条件2    

mysql 数据库远程访问设置方法

摘自: http://www.iteye.com/topic/418151 mysql数据库远程访问设置方法 1.修改localhost更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改成"%" mysql>use mysql; mysql>update user set host = '%' where user = 'root'; mysql

MySQL数据库的定时自动备份

昨天做了MySQL数据库的备份基本操作的实验,今天试一下MySQL数据库的定时自动备份. 大概思路就是,首先为备份文件单独创建一个目录,然后再目录下创建Shell脚本里写上执行数据库备份的命令,并且给这个Shell脚本添加可执行的权限,最后给这个Shell脚本添加到crond计划任务,让它每天定时执行. 1.首先进入home目录,创建一个mysqlBackup的目录,然后进入这个目录 2.创建一个.sh后缀的文件, 3.编辑Shell脚本,备份所有数据库到/home/mysqlBackup目录下

添加MySql数据库超时设置的相关问题

最近在工作中, 遇到MySql数据库连接超时的问题,申同事帮忙解决了一下,看到原来的MySqlHelper里面没有封装相关的超时方法,就添加了一个,我在这里记录了一下,希望对像我一样入门级的程序员有所帮助: 如下方法: Private int ExecuteScalarBySql(string sqlstr) { try{ using((MySqlConnection mysqlconn)=new MySqlConnection(Config.ConnectionStrings)) { mysq

Java连接MySQL数据库增删改查通用方法

Java连接MySQL数据库增删改查通用方法 运行环境:eclipse+MySQL 以前我们Java连接MySQL数据库都是一个数据库写一个类,类下面写好多方法,要是多个数据库,就要写多个类多个方法,导致代码编写太过于繁琐,所以为了改变这样的繁琐,我将连接数据库的方法进行了一系列的封装,使用户传入一个对象值Object就可以得到想要的. 我在之前写过一篇普通的Java连接MySQL数据库,大家可以看看,以便对比参考之后就知道差距了  数据库--MySQL-->Java篇 接下来我给大家讲讲如何将

mysql数据库出现2003-Can&#39;t connect to MySQL server on &#39;localhost&#39; (10061)的解决方法

1.右键点击我的电脑,找到管理! 2.找到服务和应用程序: 3.打开找到服务,打开: 4.在服务里找到MySQL,改成启动: mysql数据库出现2003-Can't connect to MySQL server on 'localhost' (10061)的解决方法

MySQL数据库的优化(下)MySQL数据库的高可用架构方案

MySQL数据库的优化(下)MySQL数据库的高可用架构方案 2011-03-09 08:53 抚琴煮酒 51CTO 字号:T | T 在上一篇MySQL数据库的优化中,我们跟随笔者学习了单机MySQL数据库的优化,今天我们继续跟随笔者学习MySQL优化的集群方案. AD:51CTO 网+首届APP创新评选大赛火热启动——超百万资源等你拿! [51CTO独家特稿]在上一篇MySQL数据库的优化中,我们跟随笔者学习了单机MySQL数据库的优化,今天我们继续跟随笔者学习MySQL优化的集群方案. M