• ADADADADAD

    怎么用mysqlbinlog做基于时间点的数据恢复[ mysql数据库 ]

    mysql数据库 时间:2024-11-26 22:14:35

    作者:文/会员上传

    简介:

    mysql> show databases;+--------------------+| Database|+--------------------+| information_schema || mysql || performance_schema || test|+--------------------+4

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    mysql> show databases;
    +--------------------+
    | Database|
    +--------------------+
    | information_schema |
    | mysql |
    | performance_schema |
    | test|
    +--------------------+
    4 rows in set (0.00 sec)


    mysql> use test
    Database changed
    mysql> show tables;
    Empty set (0.00 sec)


    mysql> show binary logs;
    +------------------+-----------+
    | Log_name | File_size |
    +------------------+-----------+
    | mysql-bin.000001 |120 |
    +------------------+-----------+
    1 row in set (0.00 sec)


    mysql>
    mysql>
    mysql> flush logs;
    Query OK, 0 rows affected (0.16 sec)


    mysql> show binary logs;
    +------------------+-----------+
    | Log_name | File_size |
    +------------------+-----------+
    | mysql-bin.000001 |167 |
    | mysql-bin.000002 |120 |
    +------------------+-----------+
    2 rows in set (0.00 sec)

    创建一个新表chenfeng并插入三条记录:
    mysql> create table chenfeng(t1 int not null primary key,t2 varchar(50),t3 datetime);
    Query OK, 0 rows affected (0.13 sec)


    mysql> insert into chenfeng values (1,"beijing",now());
    Query OK, 1 row affected (0.02 sec)


    mysql> insert into chenfeng values (2,"shanghai",now());
    Query OK, 1 row affected (0.02 sec)


    mysql> insert into chenfeng values (3,"zhengzhou",now());
    Query OK, 1 row affected (0.03 sec)


    mysql> select * from chenfeng;
    +----+-----------+---------------------+
    | t1 | t2| t3 |
    +----+-----------+---------------------+
    | 1 | beijing| 2017-01-25 15:33:54 |
    | 2 | shanghai | 2017-01-25 15:34:08 |
    | 3 | zhengzhou | 2017-01-25 15:34:23 |
    +----+-----------+---------------------+
    3 rows in set (0.00 sec)


    现在我们执行delete误操作,删除所有的数据:
    mysql> delete from chenfeng;
    Query OK, 3 rows affected (0.04 sec)


    先查看binlog,生成002.sql:
    mysqlbinlog mysql-bin.000002 > 002.sql

    查看002.sql,并只摘取delete部分内容:
    BEGIN
    /*!*/;
    # at 1094
    #170125 15:37:10 server id 1 end_log_pos 1188 CRC32 0x53d8348f Querythread_id=197exec_time=0 error_code=0
    SET TIMESTAMP=1485329830/*!*/;
    delete from chenfeng
    /*!*/;
    # at 1188
    #170125 15:37:10 server id 1 end_log_pos 1219 CRC32 0xfe067937 Xid = 25
    COMMIT/*!*/;
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;


    可以看到在时间2017-01-25 15:37:10我们做了delete误操作。现在需要用mysqlbinlog恢复到这个时间点前的数据:
    # mysqlbinlog mysql-bin.000002 --stop-date='2017-01-25 15:37:10' > resume.sql

    执行resume.sql内容后发现数据已恢复:
    mysql> select * from chenfeng;
    +----+-----------+---------------------+
    | t1 | t2| t3 |
    +----+-----------+---------------------+
    | 1 | beijing| 2017-01-25 15:33:54 |
    | 2 | shanghai | 2017-01-25 15:34:08 |
    | 3 | zhengzhou | 2017-01-25 15:34:23 |
    +----+-----------+---------------------+
    3 rows in set (0.00 sec)

    怎么用mysqlbinlog做基于时间点的数据恢复.docx

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: mysqlbinlog