zoukankan      html  css  js  c++  java
  • mysql数据恢复,binlog详解

    个人博客:mysql数据恢复,binlog详解

    binlog日志恢复数据,是挽救错误操作和数据损坏一根救命稻草,所以认识和使用binglog对于技术人员还是很有必要的

    binlog一般用于

    • 主从复制 中 master节点开启binlog把二进制日志传递给slave节点达到主从数据一致
    • 第二点自然是用于数据恢复了,使用mysqlbinlog工具来恢复数据

    因为我自己的网站遇到过mysql表被我误删操作,drop table后当时我还是挺淡定的,虽然我不清楚我能不能把数据拯救回来,对于个人网站而言可能都没有主从,也没有把mysql的各项配置设置完美,这种情形下最担心的当然是binlog是否开启了,如果没有开启binlog并且也没有做数据备份,我感觉基本上就GG了

    因为我mysql是通过docker容器安装的,所以具体 my.cnf 配置文件的放在哪也忘记了

    1. 使用 find / -name my.cnf 找到文件在哪
    root@0d5861775029:/# find / -name my.cnf
    
    find: '/proc/1/map_files': Operation not permitted
    find: '/proc/182/map_files': Operation not permitted
    find: '/proc/187/map_files': Operation not permitted
    find: '/proc/1601/map_files': Operation not permitted
    find: '/proc/1731/map_files': Operation not permitted
    find: '/proc/1741/map_files': Operation not permitted
    /etc/alternatives/my.cnf
    /etc/mysql/my.cnf
    /var/lib/dpkg/alternatives/my.cnf
    
    root@0d5861775029:/# 
    
    • 查看my.cnf配置找到binlog以及mysql数据存储的位置
    #
    # The MySQL  Server configuration file.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
    
    [mysqld]
    pid-file        = /var/run/mysqld/mysqld.pid
    socket          = /var/run/mysqld/mysqld.sock
    datadir         = /var/lib/mysql
    secure-file-priv= NULL
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    # Custom config should go here
    !includedir /etc/mysql/conf.d/
    
    • 可以看到mysql数据存储的目录是 datadir = /var/lib/mysql 目录中
    root@0d5861775029:/etc/mysql# cd /var/lib/mysql/
    root@0d5861775029:/var/lib/mysql# ls
    #innodb_temp   binlog.index	client-key.pem	ib_logfile1  mysql.ibd		 server-cert.pem  undo_002
    auto.cnf       ca-key.pem	db_blog		ibdata1      performance_schema  server-key.pem
    binlog.000001  ca.pem		ib_buffer_pool	ibtmp1	     private_key.pem	 sys
    binlog.000002  client-cert.pem	ib_logfile0	mysql	     public_key.pem	 undo_001
    

    上面的前戏都看完了,这其实并非binlog具体使用,而是我个人发现数据目录方式 接下来我将详细介绍binlog的使用

    一、开启binlog日志

    • 查看binlog是否开启
      • ON 表示已经开启
      • 查看更多内容可以这样 show variables like 'log_%';
    mysql> show variables like 'log_bin';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | log_bin       | ON    |
    +---------------+-------+
    1 row in set (0.00 sec)
    
    mysql> show variables like 'log_%';
    +----------------------------------------+----------------------------------------+
    | Variable_name                          | Value                                  |
    +----------------------------------------+----------------------------------------+
    | log_bin                                | ON                                     |
    | log_bin_basename                       | /var/lib/mysql/binlog                  |
    | log_bin_index                          | /var/lib/mysql/binlog.index            |
    | log_bin_trust_function_creators        | OFF                                    |
    | log_bin_use_v1_row_events              | OFF                                    |
    | log_error                              | stderr                                 |
    | log_error_services                     | log_filter_internal; log_sink_internal |
    | log_error_suppression_list             |                                        |
    | log_error_verbosity                    | 2                                      |
    | log_output                             | FILE                                   |
    | log_queries_not_using_indexes          | OFF                                    |
    | log_slave_updates                      | ON                                     |
    | log_slow_admin_statements              | OFF                                    |
    | log_slow_extra                         | OFF                                    |
    | log_slow_slave_statements              | OFF                                    |
    | log_statements_unsafe_for_binlog       | ON                                     |
    | log_throttle_queries_not_using_indexes | 0                                      |
    | log_timestamps                         | UTC                                    |
    +----------------------------------------+----------------------------------------+
    18 rows in set (0.00 sec)
    
    • 编辑my.cnf开启binlog
    在[mysqld] 区块
    设置/添加 log-bin=mysql-bin  确认是打开状态(值 mysql-bin 是日志的基本名或前缀名);
    

    然后重启mysql

    二、查看binlog日志操作命令

    1. 查看所有binlog日志列表
    mysql> show logs;
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'logs' at line 1
    mysql> show master logs;
    +---------------+-----------+-----------+
    | Log_name      | File_size | Encrypted |
    +---------------+-----------+-----------+
    | binlog.000001 |   3091158 | No        |
    | binlog.000002 | 141156437 | No        |
    +---------------+-----------+-----------+
    2 rows in set (0.17 sec)
    
    1. 查看master状态,也就是最新一个binlog日志编号名称和最后一个操作事件pos结束位置
    mysql> show master status;
    +---------------+-----------+--------------+------------------+-------------------+
    | File          | Position  | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +---------------+-----------+--------------+------------------+-------------------+
    | binlog.000002 | 141156437 |              |                  |                   |
    +---------------+-----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    1. 刷新log日志,将会产生一个新编号的binlog日志文件
    mysql> flush logs;
    
    1. 重置(清空)所有binlog日志
    mysql> reset master;
    

    三、查看binlog日志内容

    1. 使用mysqlbinlog命令查看

    因为binlog是二进制文件,普通文件查看器都无法打开,必须使用自带的mysqlbinlog命令查看

    • mysqlbinlog binlog.000002 使用mysqlbinlog不好观察
    1. 在mysql中查看binlog日志
    mysql> show binlog events [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count];
    
     选项解析:
       IN 'log_name'   指定要查询的binlog文件名(不指定就是第一个binlog文件)
       FROM pos        指定从哪个pos起始点开始查起(不指定就是从整个文件首个pos点开始算)
       LIMIT [offset,] 偏移量(不指定就是0)
       row_count       查询总条数(不指定就是所有行)
    
     截取部分查询结果:
     *************************** 20. row ***************************
        Log_name: mysql-bin.000021  ----------------------------------------------> 查询的binlog日志文件名
             Pos: 11197 ----------------------------------------------------------> pos起始点:
      Event_type: Query ----------------------------------------------------------> 事件类型:Query
       Server_id: 1 --------------------------------------------------------------> 标识是由哪台服务器执行的
     End_log_pos: 11308 ----------------------------------------------------------> pos结束点:11308(即:下行的pos起始点)
            Info: use `zyyshop`; INSERT INTO `team2` VALUES (0,345,'asdf8er5') ---> 执行的sql语句
     *************************** 21. row ***************************
        Log_name: mysql-bin.000021
             Pos: 11308 ----------------------------------------------------------> pos起始点:11308(即:上行的pos结束点)
      Event_type: Query
       Server_id: 1
     End_log_pos: 11417
            Info: use `zyyshop`; /*!40000 ALTER TABLE `team2` ENABLE KEYS */
     *************************** 22. row ***************************
        Log_name: mysql-bin.000021
             Pos: 11417
      Event_type: Query
       Server_id: 1
     End_log_pos: 11510
            Info: use `zyyshop`; DROP TABLE IF EXISTS `type`
    
    1. 指定查询 binlog.000002 日志
    mysql> show binlog events in 'binlog.000002' limit 10;
    +---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                                                                  |
    +---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | binlog.000002 |    4 | Format_desc    |         1 |         124 | Server ver: 8.0.16, Binlog ver: 4                                                                                                                                     |
    | binlog.000002 |  124 | Previous_gtids |         1 |         155 |                                                                                                                                                                       |
    | binlog.000002 |  155 | Anonymous_Gtid |         1 |         234 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
    | binlog.000002 |  234 | Query          |         1 |         482 | CREATE USER 'schwarzeni'@'localhost' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$H{;gmzB@[}K1i
    Bcce80ezg8j3o0qDdYocc1OxBkShlQyzmOV/c4rGP69' /* xid=7 */ |
    | binlog.000002 |  482 | Anonymous_Gtid |         1 |         561 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
    | binlog.000002 |  561 | Query          |         1 |         801 | CREATE USER 'cuishifeng'@'%' IDENTIFIED WITH 'caching_sha2_password' AS '$A$005$f8ZshY(9]HPTcaN83yCTNmHs/LQsa2DerCX.ZVgd4InrYiCpj75mA' /* xid=8 */         |
    | binlog.000002 |  801 | Anonymous_Gtid |         1 |         878 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
    | binlog.000002 |  878 | Query          |         1 |         968 | FLUSH PRIVILEGES                                                                                                                                                      |
    | binlog.000002 |  968 | Anonymous_Gtid |         1 |        1047 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                                                                  |
    | binlog.000002 | 1047 | Query          |         1 |        1256 | ALTER USER 'cuishifeng'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*10320381F36BE49A18F09B06A4BC005223975101' /* xid=12 */                                       |
    +---------------+------+----------------+-----------+-------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    10 rows in set (0.00 sec)
    
    
    1. 指定查询 binlog.000002 这个文件,从pos点:968开始查起
    mysql> show binlog events in 'binlog.000002' from 968 limit 10;
    +---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
    | Log_name      | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                            |
    +---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
    | binlog.000002 |  968 | Anonymous_Gtid |         1 |        1047 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
    | binlog.000002 | 1047 | Query          |         1 |        1256 | ALTER USER 'cuishifeng'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*10320381F36BE49A18F09B06A4BC005223975101' /* xid=12 */ |
    | binlog.000002 | 1256 | Anonymous_Gtid |         1 |        1333 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
    | binlog.000002 | 1333 | Query          |         1 |        1423 | flush privileges                                                                                                                |
    | binlog.000002 | 1423 | Anonymous_Gtid |         1 |        1500 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
    | binlog.000002 | 1500 | Query          |         1 |        1646 | GRANT ALL PRIVILEGES ON *.* TO 'cuishifeng'@'%' /* xid=70 */                                                                    |
    | binlog.000002 | 1646 | Anonymous_Gtid |         1 |        1723 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
    | binlog.000002 | 1723 | Query          |         1 |        1813 | flush privileges                                                                                                                |
    | binlog.000002 | 1813 | Anonymous_Gtid |         1 |        1890 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
    | binlog.000002 | 1890 | Query          |         1 |        1968 | FLUSH TABLES                                                                                                                    |
    +---------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
    10 rows in set (0.00 sec)
    
    

    从日志中可以看出执行的mysql命令 并且有起始位置,对于恢复数据非常有用

    1. 查询第一个(最早)的binlog日志

    mysql> show binlog events;

    四、现在从binlog日志恢复数据

    1. 常用命令
    恢复语法格式:
    # mysqlbinlog mysql-bin.0000xx | mysql -u用户名 -p密码 数据库名
     常用选项:
      --start-position=953                   起始pos点
      --stop-position=1437                   结束pos点
      --start-datetime="2013-11-29 13:18:54" 起始时间点
      --stop-datetime="2013-11-29 13:21:53"  结束时间点
      --database=zyyshop                     指定只恢复zyyshop数据库(一台主机上往往有多个数据库,只限本地log日志)
        
    不常用选项:    
      -u --user=name              Connect to the remote server as username.连接到远程主机的用户名
      -p --password[=name]        Password to connect to remote server.连接到远程主机的密码
      -h --host=name              Get the binlog from server.从远程主机上获取binlog日志
      --read-from-remote-server   Read binary logs from a MySQL server.从某个MySQL服务器上读取binlog日志
    
    小结:实际是将读出的binlog日志内容,通过管道符传递给mysql命令。这些命令、文件尽量写成绝对路径;
    
    

    日志恢复 相当于执行当时DDL语句,如果日志恢复的语句例如你库中存在某个表 日志又执行创建这个表 肯定是走不通的 所以最好指定具体位置恢复

    1. 查看binlog日志 确定从哪恢复
     mysql> show binlog events in 'mysql-bin.000023';
            
    以下为末尾片段:
    +------------------+------+------------+-----------+-------------+------------------------------------------------------------+
    | Log_name         | Pos  | Event_type | Server_id | End_log_pos | Info                                                       |
    +------------------+------+------------+-----------+-------------+------------------------------------------------------------+
    | mysql-bin.000023 |  922 | Xid        |         1 |         953 | COMMIT /* xid=3820 */                                      |
    | mysql-bin.000023 |  953 | Query      |         1 |        1038 | BEGIN                                                      |
    | mysql-bin.000023 | 1038 | Query      |         1 |        1164 | use `zyyshop`; update zyyshop.tt set name='李四' where id=4|
    | mysql-bin.000023 | 1164 | Xid        |         1 |        1195 | COMMIT /* xid=3822 */                                      |
    | mysql-bin.000023 | 1195 | Query      |         1 |        1280 | BEGIN                                                      |
    | mysql-bin.000023 | 1280 | Query      |         1 |        1406 | use `zyyshop`; update zyyshop.tt set name='小二' where id=2|
    | mysql-bin.000023 | 1406 | Xid        |         1 |        1437 | COMMIT /* xid=3823 */                                      |
    | mysql-bin.000023 | 1437 | Query      |         1 |        1538 | drop database zyyshop                                      |
    +------------------+------+------------+-----------+-------------+------------------------------------------------------------+
    
    通过分析,造成数据库破坏的pos点区间是介于 1437--1538 之间,只要恢复到1437前就可。
    
    mysqlbinlog  --start-position=953  --stop-position=1538 --database=zyyshop binlog.000002 | mysql -uroot -p123456 -v zyyshop
    
    1. 指定时间恢复
    • 知道自己在哪个时间段误操作了
    mysql> drop table tt;
    
    @ --start-datetime="2013-11-29 13:18:54"  起始时间点
    @ --stop-datetime="2013-11-29 13:21:53"   结束时间点
    
    # mysqlbinlog --start-datetime="2013-11-29 13:18:54" --stop-datetime="2013-11-29 13:21:53" --database=zyyshop binlog.000002 | mysql -uroot -p123456 -v zyyshop
    

    希望这篇文章能够帮助误删操作的朋友顺利恢复数据

  • 相关阅读:
    Django-model基础
    web框架
    django-MTV
    我与前端之间不得不说的三天两夜之jQuery
    vertical-align和line-height的深入应用
    我与前端之间不得说的三天两夜之css基础
    mysql数据库从删库到跑路之mysql:视图、触发器、事务、存储过程、函数
    我与前端之间不得不说的三天两夜之html基础
    hdu2206 IP地址的计算
    get方式中文乱码解决方法
  • 原文地址:https://www.cnblogs.com/NiceCui/p/11246156.html
Copyright © 2011-2022 走看看