根据二进制日志恢复
目的:恢复数据,根据二进制日志将数据恢复到今天任意时刻
增量恢复,回滚恢复
如果有备份好的数据,将备份好的数据导入新数据库时,会随着产生二进制日志
先准备一台初始化的数据库
mysqld --initialize --user =mysql --basedir=/usr/local/mysql --datadir=/data/mysql/
启动数据库
service mysqld start
因为数据库版本为5.7,初始化启动后新密码会在mysql-error.log文件中
2018-08-02T11:59:01.189981+08:00 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 63baed5c-9608-11e8-b7c6-525400b807c2.
2018-08-02T11:59:01.193712+08:00 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-02T11:59:01.197445+08:00 1 [Note] A temporary password is generated for root@localhost: pVwD<NQ:i85z
2018-08-02T11:59:02.323852+08:00 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2018-08-02T11:59:02.323876+08:00 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
创建数据,我在这创建了4个空的库
MySQL [mysql]> create database t1;
Query OK, 1 row affected (0.00 sec)
MySQL [mysql]> create database t2;
Query OK, 1 row affected (0.00 sec)
MySQL [mysql]> create database t3;
Query OK, 1 row affected (0.00 sec)
MySQL [mysql]> create database t4;
Query OK, 1 row affected (0.00 sec)
MySQL [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| t1 |
| t2 |
| t3 |
| t4 |
+--------------------+
8 rows in set (0.00 sec)
刷新二进制日志
MySQL [mysql]> flush logs;
Query OK, 0 rows affected (0.01 sec)
查看对应二进制日志(查看时建议导出,vim功能定位方便)
mysqlbinlog --no-defaults mysql-bin.000009 >/data/000009.log
# at 219
#180802 17:56:31 server id 48184 end_log_pos 310 CRC32 0x98636c54 Query thread_id=10589 exec_time=0 error_code=0
SET TIMESTAMP=1533203791/*!*/;
SET @@session.pseudo_thread_id=10589/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=224,@@session.collation_connection=224,@@session.collation_server=224/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
create database ku1
/*!*/;
# at 310
#180802 17:56:34 server id 48184 end_log_pos 375 CRC32 0xfb9a2910 Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 375
#180802 17:56:34 server id 48184 end_log_pos 466 CRC32 0x8d2da46b Query thread_id=10589 exec_time=0 error_code=0
SET TIMESTAMP=1533203794/*!*/;
create database ku2
/*!*/;
# at 466
#180802 17:56:36 server id 48184 end_log_pos 531 CRC32 0x33111aad Anonymous_GTID last_committed=2 sequence_number=3 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 531
#180802 17:56:36 server id 48184 end_log_pos 622 CRC32 0x74d93a38 Query thread_id=10589 exec_time=0 error_code=0
SET TIMESTAMP=1533203796/*!*/;
create database ku3
/*!*/;
# at 622
#180802 17:56:50 server id 48184 end_log_pos 687 CRC32 0xe71ae53e Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 687
#180802 17:56:50 server id 48184 end_log_pos 770 CRC32 0xc0d9aa6d Query thread_id=10589 exec_time=0 error_code=0
SET TIMESTAMP=1533203810/*!*/;
drop database ku1
/*!*/;
# at 770
#180802 17:56:53 server id 48184 end_log_pos 835 CRC32 0xbb65a7c2 Anonymous_GTID last_committed=4 sequence_number=5 rbr_only=no
SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/;
# at 835
#180802 17:56:53 server id 48184 end_log_pos 918 CRC32 0x2b0760b0 Query thread_id=10589 exec_time=0 error_code=0
SET TIMESTAMP=1533203813/*!*/;
drop database ku2
/*!*/;
# at 918
#180802 17:56:59 server id 48184 end_log_pos 965 CRC32 0xd3f06efa Rotate to mysql-bin.000010 pos: 4
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*/;
回滚恢复数据
我创建了ku1,ku2,ku3三个数据库,然后删除了ku1和ku2两个库,现在需要恢复ku1
根据标记恢复,创建ku1的标记为at219
,结束标记为end_log_pos 310
,如果有commit
标记,需要将commit包含在恢复的范围内!
mysqlbinlog --no-defaults --start-position="219" --stop-position="310" /data/mysql/mysql-bin.000009 | mysql -uroot -p
总结
数据的恢复本质是执行sql语句,需要恢复哪部分数据就去选择二进制日志中记录这部分语句的标记
数据往后追加,不能逆向删除。