zoukankan      html  css  js  c++  java
  • 使用全备+binlog日志恢复数据库

    1.binlog日志类型

    • Statement 只记录执行的sql语句,磁盘占用少,但是恢复的时候容易出问题。InodeDB不能使用Statement 。
    • Row 记录修改后的具体数据,磁盘占用较多
    • Mixed 上面两种混合,mysqlsql根据具体情形,选择较好的方案

    查看当前的类型:
    show variables like 'binlog_format'

    /etc/my.cnf 里面的binlog_format参数

    2. 恢复方式

    全备+恢复部分binlog日志

    3.恢复方法

    1. 全备

    mysqldump -uroot -pfjselinfgsiengiseg test3 > /data1/test3.sql. 
    

    2. 执行操作

    SQL1:update student set age=21 where id=4;
    SQL2:update student set age=22 where id=5;

    这时执行了两个sql,对应的是两个事务。
    如果我们想把数据库状态恢复到执行SQL1之后

    3.刷新日志

    要刷新日志后,我们才能查看日志,找到需要恢复的binlog

    执行:

    flush logs;
    

    4. 找到最新的日志

    找到binlog日志:/var/lib/mysql/mysql-bin.000031

    5. 分析日志

    mysql执行:

    show binlog events in "mysql-bin.000031"
    
    • Log_name 日志文件名
    • Pos 事件的开始位置
    • Event_type事件类型
    • Server_id服务器ID,和主从关系的server_id对应
    • End_log_pos 事件的结束位置
    • Info 事件信息

    找到刚才的两次事务

    |

     mysql-bin.000031 | 6622  | Query        | 1           | 6691          | BEGIN                                                                       |
    | mysql-bin.000031 | 6691  | Table_map    | 1           | 6745          | table_id: 308 (test3.student)                                               |
    | mysql-bin.000031 | 6745  | Update_rows  | 1           | 6801          | table_id: 308 flags: STMT_END_F                                             |
    | mysql-bin.000031 | 6801  | Xid          | 1           | 6828          | COMMIT /* xid=6771600 */                                                    |
    | mysql-bin.000031 | 6828  | Query        | 1           | 6897          | BEGIN                                                                       |
    | mysql-bin.000031 | 6897  | Table_map    | 1           | 6951          | table_id: 308 (test3.student)                                               |
    | mysql-bin.000031 | 6951  | Update_rows  | 1           | 7007          | table_id: 308 flags: STMT_END_F                                             |
    | mysql-bin.000031 | 7007  | Xid          | 1           | 7034          | COMMIT /* xid=6771607 */                                                    |
    | mysql-bin.000031 | 7034  | Rotate       | 1           | 7077          | mysql-bin.000032;pos=4                                                      |
    +------------------+-------+--------------+-------------+---------------+-----------------------------------------------------------------------------+
    

    BEGIN是开始事务
    COMMIT是提交事务

    所以我们需要恢复的是6622 到 6828

    6.重新建库

    把旧的test3数据库改名,或者删除。建议先备份

    drop database test3;
    

    创建数据库test3

    create database test3;
    

    导入全备份

    source /data1/test3.sql;
    

    恢复部分binlog日志

    mysqlbinlog --start-position=6622     --stop-position=6828   --database=test3  /var/lib/mysql/mysql-bin.000031   |mysql -uroot -pfjselinfgsiengiseg  -D test3
    
    • start-position 开始位置
    • stop-position 结束位置
    • database 只获取test3库的日志
    • /var/lib/mysql/mysql-bin.000031 binlog日志路径

    mysqlbinlog 命令会把对应的binlog日志转换为sql语句,导出来,然后通过mysql命令来执行sql语句
    恢复前的数据:

    mysql root@localhost:test3> select * from student;
    +------+--------+-------+---------+
    | id   | name   | age   | class   |
    |------+--------+-------+---------|
    | 4    | 2      | 20    | B       |
    | 5    | 2      | 21    | B       |
    +------+--------+-------+---------+
    

    恢复后的数据

    mysql root@localhost:test3> select * from student;
    +------+--------+-------+---------+
    | id   | name   | age   | class   |
    |------+--------+-------+---------|
    | 4    | 2      | 21    | B       |
    | 5    | 2      | 21    | B       |
    +------+--------+-------+---------+
    

    可以看到,SQL1执行了,SQL2没有执行。

    其他

    1. 要定期全备,不然恢复的时候需要恢复大量的binlog日志,容易出错
    2. 操作恢复前,做好备份
    3. 除了用位置筛选binlog日志,还可以用时间
    4. 有些恢复需要跨多个binlog文件

    未经允许,请不要转载

  • 相关阅读:
    bound 很靠近0的mle
    order statistics 为什么3维要把6块加起来
    正态分布的来历
    互斥和独立
    平稳随机过程 会不会和随机过程矛盾呢?
    请问如何理解同一个随机过程,里面的两个随机变量的期望不同呢?
    bagging和boosting以及rand-forest
    arr = map(float,arr)输出问题
    lasso数学解释
    岭回归的数学解释
  • 原文地址:https://www.cnblogs.com/Xjng/p/11401850.html
Copyright © 2011-2022 走看看