需要开启binlog日志功能
加上配置之后重启服务,使其生效
[root@mysql-1 mysql]# systemctl restart mysqld
mysql> flush logs; #刷新binlog日志,使下面的语句存放到下一个binlog日志中
mysql> create table testdb.t2(id int);
根据位置恢复
找到要恢复的sql语句的起始位置、结束位置
[root@mysql-1 mysql]# mysqlbinlog mysql-bin.000002 #查看binlog日志内容
mysql>drop table testdb.t2(id int); //删除表格。注意:增删改都会记录到binlog日志中。
#恢复语句,按照上面文件中找到的位置,起始是219 结束是321,文件名要看清楚。
[root@mysql-1 mysql]# mysqlbinlog --start-position 219 --stop-position 321 mysql-bin.000002 |mysql -uroot -p123
扩展:
mysql> mysql> show binlog events in 'mysql-bin.000002'; 在数据库中找到binlog起止位置和结束位置。
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
| mysql-bin.000002 | 4 | Format_desc | 1 | 123 | Server ver: 5.7.29-log, Binlog ver: 4 |
| mysql-bin.000002 | 123 | Previous_gtids | 1 | 154 | |
| mysql-bin.000002 | 154 | Anonymous_Gtid | 1 | 219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS' |
| mysql-bin.000002 | 219 | Query | 1 | 320 | use `testdb`; create table t2(id int) |
| mysql-bin.000002 | 320 | Rotate | 1 | 367 | mysql-bin.000003;pos=4 |
+------------------+-----+----------------+-----------+-------------+---------------------------------------+
根据binlog日志的时间点恢复
找到要恢复sql语句的起始时间、结束时间
mysqlbinlog --start-datetime='2019-8-21 23:55:54' --stop-datetime='2019-8-21 23:56:541' mylog.000001 | mysql -u root -p1