MySQLデータベースの増分リカバリ

システム環境

[root @ mysql1〜] #cat / etc / redhat-release
CentOS Linuxリリース7.4.1708(コア)
[root @ mysql1〜] #mysql -V
mysql Ver 14.14 Distrib 5.7.20、Linux(x86_64)用EditLineラッパーを使用

ポイントインタイムリカバリ

1.バイナリログファイルを開きます

vi /etc/my.cnf        #编辑mysql配置文件
添加
log_bin=/usr/local/mysql/data/mysql_bin

[root@mysql1 ~]# systemctl restart mysqld
[root@mysql1 ~]# ll /usr/local/mysql/data/    #查看是否生成日志文件
总用量 122924
......此处省略
-rw-r-----. 1 mysql mysql      154 12月 25 14:22 mysql_bin.000001
-rw-r-----. 1 mysql mysql       39 12月 25 14:22 mysql_bin.index
......此处省略
[root@mysql1 ~]# 

PS:ログファイルの表示方法

mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql_bin.000001 

--no-defaults:utf-8エラーを
--base64-output=decode-rows解決します:文字化けした問題を解決します
-v出力ファイルをポイントします

2.データベースを作成します(テスト用)

[root@mysql1 ~]# mysql -uroot -p        #进入数据库
Enter password: 

......此处省略

mysql> create database aaa;    #创建aaa库
mysql> use aaa;       #使用aaa库
mysql> create table bbb(id int(5), name varchar(64), age int(3));  #创建表
mysql> insert into bbb values(001,'zhangsan',18),(002,'lisi',19);  #往表中插入数据

......此处省略

mysql> select * from bbb;  查询表
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   18 |
|    2 | lisi     |   19 |
+------+----------+------+
2 rows in set (0.00 sec)

mysql> 

ここに画像の説明を挿入します
3.完全バックアップ

[root@mysql1 ~]# mkdir /backup   #创建目录用于存放备份文件
[root@mysql1 ~]# mysqldump -uroot -p aaa > /backup/aaa-$(date +%F).sql;  #将aaa库完全备份到/backup目录下,文件名带上日期
Enter password: 
[root@mysql1 ~]# mysqladmin -uroot -p flush-logs   刷新日志
Enter password: 
[root@mysql1 ~]# ll /backup/   查看备份备份文件
总用量 4
-rw-r--r--. 1 root root 1859 12月 25 15:05 aaa-2020-12-25.sql
[root@mysql1 ~]# ll /backup/
总用量 4
-rw-r--r--. 1 root root 1859 12月 25 15:05 aaa-2020-12-25.sql


[root@mysql1 ~]# ll /usr/local/mysql/data/
总用量 122928
drwxr-x---. 2 mysql mysql       50 12月 25 14:54 aaa    #第二步创建用于测试用的库

......此处省略

-rw-r-----. 1 mysql mysql      834 12月 25 15:20 mysql_bin.000001
-rw-r-----. 1 mysql mysql      154 12月 25 15:20 mysql_bin.000002    #刷新日志文件生成的文件
-rw-r-----. 1 mysql mysql       78 12月 25 15:20 mysql_bin.index
......此处省略
[root@mysql1 ~]# 

4.間違った操作をシミュレートする

[root@mysql1 ~]# mysql -u root -p
Enter password: 

.....此处省略

mysql> use aaa;
mysql> insert into bbb values(3,'wangwu',20);  #正确操作
mysql> delete from bbb where name='zhangsan';  #错误操作
mysql> insert into bbb values(4,'zhaoliu',21);  #正确操作

mysql> select * from bbb;
+------+---------+------+
| id   | name    | age  |
+------+---------+------+
|    2 | lisi    |   19 |
|    3 | wangwu  |   20 |
|    4 | zhaoliu |   21 |
+------+---------+------+
3 rows in set (0.00 sec)

mysql> 

5.ログファイルを更新します

[root@mysql1 ~]# mysqladmin -uroot -p flush-logs
Enter password: 
[root@mysql1 ~]# ll /usr/local/mysql/data/
总用量 122932
drwxr-x---. 2 mysql mysql       50 12月 25 14:54 aaa

.....此处省略

-rw-r-----. 1 mysql mysql      834 12月 25 15:20 mysql_bin.000001
-rw-r-----. 1 mysql mysql     1005 12月 25 15:34 mysql_bin.000002
-rw-r-----. 1 mysql mysql      154 12月 25 15:34 mysql_bin.000003
-rw-r-----. 1 mysql mysql      117 12月 25 15:34 mysql_bin.index

.....此处省略

[root@mysql1 ~]# 

6.ログファイルを表示します

誤って削除された時間を見つけて書き留めます

.....此处省略

/*!*/;
# at 557
#201225 15:29:39 server id 1  end_log_pos 606 CRC32 0xecfa4137 	Table_map: `aaa`.`bbb` mapped to number 219
# at 606
#201225 15:29:39 server id 1  end_log_pos 659 CRC32 0x0e18157e 	Delete_rows: table id 219 flags: STMT_END_F
### DELETE FROM `aaa`.`bbb`
### WHERE
###   @1=1
###   @2='zhangsan'
###   @3=18
# at 659
#201225 15:29:39 server id 1  end_log_pos 690 CRC32 0x5bf0f416 	Xid = 48
COMMIT/*!*/;
# at 690
#201225 15:29:46 server id 1  end_log_pos 755 CRC32 0xcafa2ead 	Anonymous_GTID	last_committed=2	sequence_number=3	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 755
#201225 15:29:46 server id 1  end_log_pos 826 CRC32 0x4dd56eb4 	Query	thread_id=7	exec_time=0	error_code=0
SET TIMESTAMP=1608881386/*!*/;

.....此处省略

ここに画像の説明を挿入します

7.データベースにログインし、間違ったデータベースを削除します

[root@mysql1 ~]# mysql -uroot -p
Enter password: 


mysql> drop database aaa;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database aaa;  # 创建同名的数据库
Query OK, 1 row affected (0.00 sec)
mysql> exit;
Bye

8.データを復元する

[root@mysql1 ~]# mysql -u root -p aaa < /backup/aaa-2020-12-25.sql   ##完全恢复
Enter password: 
[root@mysql1 ~]# mysql -u root -p
Enter password: 

......此处省略内容

mysql> use aaa;
mysql> select * from bbb;    #查看表
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   18 |
|    2 | lisi     |   19 |
+------+----------+------+
2 rows in set (0.00 sec)

mysql> exit
Bye

インクリメンタルリカバリー

間違った操作を開始したときに停止し、正しい操作を開始したときに開始します

[root@mysql1 ~]# mysqlbinlog --no-defaults --stop-datetime='2020-12-25  15:29:39' /usr/local/mysql/data/mysql_bin.000002 | mysql -u root -p
Enter password: 
[root@mysql1 ~]# mysqlbinlog --no-defaults --start-datetime='2020-12-25  15:29:46' /usr/local/mysql/data/mysql_bin.000002 | mysql -u root -p
Enter password: 

10.確認する

[root@mysql1 ~]# mysql -u root -p
Enter password: 

mysql> use aaa;
Database changed
mysql> select * from bbb;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   18 |
|    2 | lisi     |   19 |
|    3 | wangwu   |   20 |
|    4 | zhaoliu  |   21 |
+------+----------+------+
4 rows in set (0.00 sec)

mysql> 

ここに画像の説明を挿入します

ポイントベースのリカバリ

1.データベースにログインし、誤って2行のデータを2行削除します

[root@mysql1 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.7.20-log Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use aaa;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from bbb;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   18 |
|    2 | lisi     |   19 |
|    3 | wangwu   |   20 |
|    4 | zhaoliu  |   21 |
+------+----------+------+
4 rows in set (0.00 sec)

mysql> delete from bbb where name='wangwu';
Query OK, 1 row affected (0.00 sec)

mysql> delete from bbb where name='zhaoliu';
Query OK, 1 row affected (0.00 sec)

mysql> select * from bbb;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   18 |
|    2 | lisi     |   19 |
+------+----------+------+
2 rows in set (0.00 sec)

mysql> 

2.ログファイルを表示します

[root@mysql1 ~]# mysqlbinlog --no-defaults --base64-output=decode-rows -v /usr/local/mysql/data/mysql_bin.000003
.....此处省略
# at 2172
#201225 16:56:02 server id 1  end_log_pos 2243 CRC32 0x5f449ecc 	Query	thread_id=17	exec_time=0	error_code=0
SET TIMESTAMP=1608886562/*!*/;
SET @@session.sql_mode=1437073414/*!*/;
BEGIN
/*!*/;
# at 2243
#201225 16:56:02 server id 1  end_log_pos 2292 CRC32 0x6134aeed 	Table_map: `aaa`.`bbb` mapped to number 221
# at 2292
#201225 16:56:02 server id 1  end_log_pos 2343 CRC32 0x1666b594 	Delete_rows: table id 221 flags: STMT_END_F
### DELETE FROM `aaa`.`bbb`
### WHERE
###   @1=3
###   @2='wangwu'
###   @3=20
# at 2343
#201225 16:56:02 server id 1  end_log_pos 2374 CRC32 0x44d7349f 	Xid = 165
COMMIT/*!*/;
# at 2374
#201225 16:56:11 server id 1  end_log_pos 2439 CRC32 0xc61398a7 	Anonymous_GTID	last_committed=10	sequence_number=11	rbr_only=yes
/*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/;
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 2439
#201225 16:56:11 server id 1  end_log_pos 2510 CRC32 0x17eabf4d 	Query	thread_id=17	exec_time=0	error_code=0
SET TIMESTAMP=1608886571/*!*/;
BEGIN
/*!*/;
# at 2510
#201225 16:56:11 server id 1  end_log_pos 2559 CRC32 0xfaa8033c 	Table_map: `aaa`.`bbb` mapped to number 221
# at 2559
#201225 16:56:11 server id 1  end_log_pos 2611 CRC32 0x39c87782 	Delete_rows: table id 221 flags: STMT_END_F
### DELETE FROM `aaa`.`bbb`
### WHERE
###   @1=4
###   @2='zhaoliu'
###   @3=21
# at 2611
#201225 16:56:11 server id 1  end_log_pos 2642 CRC32 0xa482210e 	Xid = 166
COMMIT/*!*/;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/;
DELIMITER ;
# End of log file
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@mysql1 ~]# 

ここに画像の説明を挿入します

ここに画像の説明を挿入します
データ復旧

[root@mysql1 ~]# mysqlbinlog --no-defaults --stop-position='2243' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -p
Enter password: 
[root@mysql1 ~]# mysqlbinlog --no-defaults --start-position='2611' /usr/local/mysql/data/mysql_bin.000003 | mysql -uroot -p
Enter password: 
[root@mysql1 ~]# mysql -uroot -p
Enter password: 

......此处省略内容

mysql> use aaa;
mysql> select * from bbb;
+------+----------+------+
| id   | name     | age  |
+------+----------+------+
|    1 | zhangsan |   18 |
|    2 | lisi     |   19 |
|    3 | wangwu   |   20 |
|    4 | zhaoliu  |   21 |
+------+----------+------+
4 rows in set (0.00 sec)

mysql> 

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/weixin_50345511/article/details/111679913