MySQL 8.0.28 数据库完整备份及恢复以及binlog日志增量恢复

模拟环境说明:

服务器 说明
192.168.37.185 主库
192.168.37.185 备用库

数据库开启binlog日志 /etc/my.cnf(此配置文件可直接使用)

[mysqld]
server-id=1
port = 3306
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data/mysql
socket=/tmp/mysql.sock
## 开启binlog 指定日志存放路径 日志文件为 mysql-bin.00000x
log-bin                       = /usr/local/mysql/data/mysql-bin
log-bin-index                  =/usr/local/mysql/data/mysql-bin.index

relay-log                      = mysql-relay-bin
skip-name-resolve
back_log=1000
## 忽略大小写
lower-case-table-names=1
max_connections=1000

备份命令以及参数说明

 [root@bogon /]# mysqldump -uroot -proot --single-transaction --flush-logs --master-data=2 --databases testdb --events --routines --triggers > /testdb.sql
参数 说明
–single-transaction 使用一致的读取并保证mysqldump开始后看到的数据不会更改
–flush-logs 创建一个新的binlog日志文件
–master-data=2 将二进制日志信息写入备份文件
–databases 指定备份数据库 如果没有声明这个参数 备份文件将不包含 CREATE DATABASE 或 USE 语句
testdb 备份的数据库名称
–events 导出时间触发器 mysqldump 默认不携带此参数
–routines 导出存储过程和函数 mysqldump 默认不携带此参数
–triggers 导出存储过程和函数 mysqldump 默认携带此参数
–no-data 只备份表定义 (此文章内备份未使用)
–no-create-info 只备份数据 (此文章内备份未使用)
> /testdb.sql 将备份文件输出到根目录

binlog日志相关的操作命令


查看binlog日志文件
[root@bogon /]# mysqlbinlog /usr/local/mysql/data/mysql-bin.000002 | more

指定时间范围 指定binlog日志文件 是否包含某项操作
[root@bogon /]# mysqlbinlog --start-datetime="2022-02-26 20:50:00" --stop-datetime="2022-02-26 23:55:00" --verbose /usr/local/mysql/data/mysql-bin.000002 | grep -C 15 "DROP TABLE"

指定指定binlog日志事件开始和结束位置 指定binlog日志文件 恢复binlog日志
[root@bogon /]# mysqlbinlog --start-position=157 --stop-position=543 /usr/local/mysql/data/mysql-bin.000002 | mysql -uroot -proot

批量执行binlog日志 
[root@bogon /]# mysqlbinlog  mysql-bin.000003  mysql-bin.000004 |mysql -uroot -proot

机器:192.168.37.185(主库)

  1. 创建测试数据库
mysql> create database testdb;
mysql> use testdb;
mysql> CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
mysql> INSERT INTO pet VALUES ('Fluffy','Harold','cat','f','1993-02-04',NULL);
mysql> INSERT INTO pet VALUES ('Claws','Gwen','cat','m','1994-03-17',NULL);
mysql> INSERT INTO pet VALUES ('Buffy','Harold','dog','f','1989-05-13',	NULL);
mysql> INSERT INTO pet VALUES ('Fang','Benny','dog','m','1990-08-27',NULL);
mysql> INSERT INTO pet VALUES ('Bowser','Diane','dog','m','1979-08-31','1995-07-29');	
mysql> INSERT INTO pet VALUES ('Chirpy','Gwen','bird','f','1998-09-11',NULL);	
mysql> INSERT INTO pet VALUES ('Whistler','Gwen','bird','','1997-12-09',NULL);
mysql> INSERT INTO pet VALUES ('Slim','Benny','snake','m','1996-04-29',NULL);
-- 存储过程
mysql> delimiter $ 
CREATE PROCEDURE getpetcountbyowner (IN petowner varchar(20), OUT petcounts INT)
     BEGIN
         SELECT COUNT(*) INTO petcounts FROM testdb.pet
         WHERE owner = petowner;
	END 
$
-- 函数
mysql> delimiter $ 
CREATE FUNCTION hello (s CHAR(20))
RETURNS CHAR(50) DETERMINISTIC
       RETURN CONCAT('Hello, ',s,'!');
$
-- 视图 		 
mysql> delimiter $ 
CREATE VIEW testdb.petview AS SELECT * FROM testdb.pet;
$

-- 事件
mysql> delimiter $ 
CREATE EVENT myevent
    ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
    DO
      UPDATE testdb.pet SET birth = birth + INTERVAL 1 DAY;
	$		
-- 触发器			
mysql> delimiter $
CREATE TRIGGER birthadd BEFORE INSERT ON pet
       FOR EACH ROW SET NEW.birth = INTERVAL 1 DAY + NEW.birth;
       $
  1. 数据库备份
如果没有配置mysql的环境变量,  备份方式为   /mysql安装路径/bin/mysqldump ...
[root@bogon /]# mysqldump -uroot -proot --single-transaction --flush-logs --master-data=2 --databases testdb --events --routines --triggers > /testdb.sql
mysql> show master status; --此信息会写入备份文件内
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      157 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
将备份文件传到备用库服务器上
[root@bogon /]# scp /testdb.sql [email protected]:/

切换机器:192.168.37.186(备用库)

mysql> source testdb.sql
mysql> use testdb;
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    |      | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)

上述步骤完成之后,testdb的完整备份已在 192.168.37.186 恢复完成。接下来根据binlog日志进行增量恢复
(例如 :我们在2022-12-01 23:59:59 进行了完整备份 。在2022-12-02 12:59:59数据库出现了故障,我们通过2022-12-01 23:59:59 的完整备份恢复了数据。而 2022-12-01 23:59:59 到 2022-12-02 12:59:59 的数据库操作我们需要通过binlog日志进行恢复

切换机器:192.168.37.185(主库) 模拟数据删除

mysql> use testdb;
mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    |      | 1997-12-09 | NULL       |
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |
+----------+--------+---------+------+------------+------------+
8 rows in set (0.00 sec)

mysql> delete from pet where name ='Slim';

mysql> select * from pet;
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    |      | 1997-12-09 | NULL       |
+----------+--------+---------+------+------------+------------+
mysql> exit

由于我们在备份时使用了 --flush-logs --master-data=2 所以通过查看备份文件是可以知道备份时新开启的binlog日志文件是什么

[root@bogon /]# more /testdb.sql

MySQL 8.0.28 数据库完整备份及恢复以及binlog日志增量恢复_第1张图片
进入binlog日志目录,将备份文件内 master_log_file 指定的binlog日志以及之后生成的日志发送到 备用库

[root@bogon /]# cd /usr/local/mysql/data
[root@bogon /]# ls
mysql  mysql-bin.000001  mysql-bin.000002  mysql-bin.000003 mysql-bin.000004
[root@bogon /]# scp mysql-bin.000003 mysql-bin.000004 [email protected]:/

切换机器:192.168.37.186 进行binlog日志恢复

[root@bogon /]# mysqlbinlog  mysql-bin.000003  mysql-bin.000004 |mysql -uroot -proot
mysql> use testdb;
mysql> select * from pet; -- 可以看到我们模拟删除的数据已经不见了
+----------+--------+---------+------+------------+------------+
| name     | owner  | species | sex  | birth      | death      |
+----------+--------+---------+------+------------+------------+
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |
| Bowser   | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |
| Whistler | Gwen   | bird    |      | 1997-12-09 | NULL       |
+----------+--------+---------+------+------------+------------+

至此 整个数据库已从 192.168.37.185 迁移至 192.168.37.186

参考 MySQL官网文档 - 备份以及恢复 章节

你可能感兴趣的:(MySQL,数据库,mysql,服务器)