• MySQL通过Binlog恢复删除的表


    查看log-bin是否开启:
    mysql> show variables like '%log%bin%';
    +---------------------------------+-------+
    | Variable_name                   | Value |
    +---------------------------------+-------+
    | log_bin                         | ON    |
    | log_bin_trust_function_creators | OFF   |
    | sql_log_bin                     | ON    |
    +---------------------------------+-------+
    3 rows in set (0.00 sec)


    用sakila数据库测试:
    mysql> use sakila;
    Database changed


    查看表内容:
    mysql> select * from yoon;
    +----+------+
    | id | name |
    +----+------+
    |  1 | yoon |
    |  7 | aaa  |
    +----+------+
    2 rows in set (0.00 sec)


    查看日志信息:
    mysql> show master status;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000025 |      932 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)


    mysql> drop table yoon;
    Query OK, 0 rows affected (0.00 sec)


    刷新日志:
    mysql> flush logs;
    Query OK, 0 rows affected (0.01 sec)


    mysql> select * from yoon;
    ERROR 1146 (42S02): Table 'sakila.yoon' doesn't exist


    mysql> show master status;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000026 |      107 |              |                  |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)




    [root@yoon data]# ls
    ibdata1  ib_logfile0  ib_logfile2  mysql-bin.000025  mysql-bin.index     rocover.sql  test
    ibdata2  ib_logfile1  mysql        mysql-bin.000026  performance_schema  sakila


    [root@yoon data]# mysqlbinlog mysql-bin.000025 | grep --ignore-case DROP -A3 -B4
    /*!C utf8 *//*!*/;
    SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
    SET @@session.lc_time_names=0/*!*/;
    SET @@session.collation_database=DEFAULT/*!*/;
    DROP TABLE `yoon` /* generated by server */
    /*!*/;
    # at 215
    #150814  3:34:55 server id 360360  end_log_pos 379      Query   thread_id=1     exec_time=3215  error_code=0
    --
    COMMIT/*!*/;
    # at 932
    #150814  4:42:00 server id 360360  end_log_pos 1040     Query   thread_id=1     exec_time=0     error_code=0
    SET TIMESTAMP=1439541720/*!*/;
    DROP TABLE `yoon` /* generated by server */
    /*!*/;
    # at 1040
    #150814  4:42:10 server id 360360  end_log_pos 1083     Rotate to mysql-bin.000026  pos: 4


    mysql> select from_unixtime('1439541720');
    +-----------------------------+
    | from_unixtime('1439541720') |
    +-----------------------------+
    | 2015-08-14 04:42:00         |
    +-----------------------------+
    1 row in set (0.00 sec)


    ###如果从上次备份刷新binlog,到发现表被删掉的过程中产生了多个binlog,则要按照binlog产生的顺序,
    那么恢复的次序应该是按照binglog的产生的序号,从小到大依次恢复###


    [root@yoon data]# mysqlbinlog -d sakila --stop-datetime='2015-08-14 04:42:00' mysql-bin.000025 > recover_sakila.sql


    [root@yoon data]# mysql -uroot -p < recover_sakila.sql 
    Enter password: 


    mysql> select * from yoon;
    +----+------+
    | id | name |
    +----+------+
    |  1 | yoon |
    |  7 | aaa  |
    +----+------+
    2 rows in set (0.00 sec)

    过滤方法:(因为测试中只有一个表,而生产环境中就会有多张表)
    [root@yoon data]# more recover_sakila.sql | grep --ignore-case -E 'insert|update|select|delete' -A2 -B2 | grep yoon

    如果表名包含yoon_log,yoon_order,只想导出yoon表的话,+个-w
    [root@yoon data]# more recover_sakila.sql | grep --ignore-case -E 'insert' -A2 -B2 | grep -w yoon > yoon.sql
    insert into yoon(name) values ('yoon')
    insert into yoon(name) values ('aaa')


    [root@yoon data]# more recover_sakila.sql | grep --ignore-case -E 'create' -A2 -B2 | grep yoon                           
    create table yoon (id int(11) unsigned NOT NULL AUTO_INCREMENT,name varchar(20),PRIMARY KEY (`id`))

    用sed可导出所有语句不会截断:

    sed -n "/insert into yoon/,/;/p" yoon.sql |sed 's#/*!*/##'

  • 相关阅读:
    Linux下查找软件,rpm命令 dpkg命令 apt命令
    python3 requests的content和text方法
    python3爬虫超简单实例
    python3 爬取深圳主板公司名称,公司网址
    python3 类的学习
    ubuntu16.04同时使用 pyhton2.7和3.5,并随意切换
    (4)puppet常用的资源及其常用属性
    (3)puppet清单定义资源的语法
    (2)puppet单机测试命令apply
    lvs+keepalived高可用负载均衡
  • 原文地址:https://www.cnblogs.com/hankyoon/p/5169656.html
Copyright © 2020-2023  润新知