Mysql database rollback operation

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

1. Make sure the database has log_bin enabled

show variables  like 'log_bin';

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

The my.cnf configuration file adds configuration to enable:

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7

2. View the latest binlog file

mysql> show master status;

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

3. Binlog is generally under /var/lib/mysql by default, find the latest file binlog.000010 found in the above query

Query and locate according to the approximate time to roll back

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. Generate sql according to the queried location

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

Alternatively, set the range by position number

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

5. Execute sql to restore the database

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

Guess you like

Origin blog.csdn.net/u014438244/article/details/125398069