1.実験環境
サーバーのオペレーティングシステムは
centos7mariadbバージョン:5.5.68です
2.mysqlサービスのバイナリログをオンにします
2.1mysqlサービスの構成ファイルを変更します
vim /etc/my.cnf
mysqldセクションの下に行を追加します log-bin=mysql-bin
3.mysqlサービスの遅いログを開きます
3.1mysqlサービスの構成ファイルを変更します
vim /etc/my.cnf
mysqldセクションの下に遅いログの次のパラメータを追加します
slow_query_log=1 慢查询开启状态,ON开启,OFF关闭
slow_query_log_file=/var/run/mysqld/mysqld-slow.log 慢查询日志存放的位置
long_query_time=1 查询超过多少秒才记录
保存して変更後に終了
3.2mysqlサービスを再起動します
systemctl restart mariadb
4.データの作成と復元
4.1 mysqlデータベースにログインして、データベースとテーブルの操作を構築します
[root@host-137 ~]# mysql -uroot -p456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> create database school; 创建school数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use school; 使用school数据库
Database changed
MariaDB [school]> create table stu(id int,name varchar(20)); 创建stu表
Query OK, 0 rows affected (0.00 sec)
MariaDB [school]> insert into stu values(1,'tom'); 向表中插入数据tom
Query OK, 1 row affected (0.00 sec)
MariaDB [school]> insert into stu values(2,'jack'); 向表中插入数据jack
Query OK, 1 row affected (0.00 sec)
MariaDB [school]> insert into stu values(3,'frank'); 向表中插入数据frank
Query OK, 1 row affected (0.00 sec)
MariaDB [school]> insert into stu values(4,'lucy'); 向表中插入数据lucy
Query OK, 1 row affected (0.01 sec)
MariaDB [school]> drop database school; 删除school数据库
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases; 验证成功删除
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.01 sec)
4.2バイナリログの位置パラメータに従って復元します
mysqlバイナリログの保存場所は次のとおりです。/var/lib/mysql/mysql-bin.000001
4.2.1要件:学校データベースの作成からジャックユーザーのデータのstuテーブルへの挿入までの操作を復元します
4.2.2mysqlmysqlbinlog
のバイナリログファイルを表示するための最初の使用
mysqlbinlog /var/lib/mysql/mysql-bin.000001
回復操作の開始位置と終了位置を見つけます
4.2.3開始位置と終了位置に従って復元します
mysqlbinlog --start-position=245 --stop-position=895 /var/lib/mysql/mysql-bin.000001 |mysql -uroot -p456
4.2.4回復効果を表示する
MariaDB [(none)]> show databases; 查看school数据库恢复成功
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
8 rows in set (0.00 sec)
MariaDB [(none)]> use school;
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
MariaDB [school]> show tables; 查看stu表恢复成功
+------------------+
| Tables_in_school |
+------------------+
| stu |
+------------------+
1 row in set (0.00 sec)
MariaDB [school]> select * from stu; 查看stu表中的数据恢复成功
+------+------+
| id | name |
+------+------+
| 1 | tom |
| 2 | jack |
+------+------+
2 rows in set (0.00 sec)
4.3バイナリログの時間パラメータに従って回復する
4.3.1上記で復元された学校データベースを削除する
MariaDB [school]> drop database school;
Query OK, 1 row affected (0.01 sec)
4.3.2mysqlmysqlbinlog
バイナリログファイルを表示するために使用します
mysqlbinlog /var/lib/mysql/mysql-bin.000001
再開する操作の開始時刻と終了時刻を確認します
4.3.3開始時間と終了時間に基づいて復元する
mysqlbinlog --start-datetime="2021-01-11 16:46:39" --stop-datetime="2021-01-11 16:50:28" /var/lib/mysql/mysql-bin.000001 |mysql -uroot -p456
4.3.4回復効果を表示する
MariaDB [(none)]> show databases; 查看school数据库恢复成功
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
8 rows in set (0.00 sec)
MariaDB [(none)]> use school;
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
MariaDB [school]> show tables;
+------------------+
| Tables_in_school |
+------------------+
| stu |
+------------------+
1 row in set (0.00 sec)
MariaDB [school]> select * from stu; 查看stu表中的数据恢复成功
+------+-------+
| id | name |
+------+-------+
| 1 | tom |
| 2 | jack |
| 3 | frank |
| 4 | lucy |
+------+-------+
4 rows in set (0.01 sec)