Mysql数据库回滚操作

参考: https://blog.51cto.com/zhanx/2416449
https://blog.csdn.net/u013895577/article/details/109493700

1.确保数据库开启了log_bin

show variables  like 'log_bin';

+-------------+-----+
|Variable_name|Value|
+-------------+-----+
|log_bin      |ON   |
+-------------+-----+

my.cnf配置文件添加配置开启:

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7

2.查看最新的binlog文件

mysql> show master status;

+-------------+---------+------------+----------------+-----------------+
|File         |Position |Binlog_Do_DB|Binlog_Ignore_DB|Executed_Gtid_Set|
+-------------+---------+------------+----------------+-----------------+
|binlog.000010|120507544|            |                |                 |
+-------------+---------+------------+----------------+-----------------+

3.binlog一般默认在/var/lib/mysql下,找到上述查询到的最新文件binlog.000010

根据要回滚的大致时间查询定位

mysqlbinlog --no-defaults --database=www_sentgon_com --start-datetime="2022-06-21 09:00:00" --stop-datetime="2022-06-22 13:00:00" /data/mysql/binlog.000010

4.根据查询到的定位生成sql

mysqlbinlog --no-defaults --database=www_sentgon_com --start-datetime="2022-06-21 09:00:00" --stop-datetime="2022-06-22 13:00:00" /data/mysql/binlog.000010 > /home/rollback.sql

或者,通过位置编号设定范围

mysqlbinlog --no-defaults --start-position="239" --stop-position="736" /data/mysql/binlog.000010 > /home/rollback.sql

5.执行sql恢复数据库

mysqlbinlog --start-position=0 --stop-position=45325593 /data/mysql/binlog.000010 | mysql -utest -p

如果是通过日期时间回滚的命令如下:
mysqlbinlog --start-datetime '2019-07-02 21:42:36' --stop-datetime '2019-07-02  21:57:42' /data/mysql/binlog.000010 | mysql -utest -p

你可能感兴趣的:(mysql,数据库,mysql,sql)