mysqlデータリカバリ、binlog詳細

16687 ワード

binlogログはデータを復元し、誤った操作とデータの破損を救うために、binglogを認識し、使用することは技術者にとって必要である.binlogは一般的に
  • マスタスレーブレプリケーション中masterノードbinlogを開いてバイナリログをslaveノードに渡しマスタスレーブデータ一致
  • 第2のポイントはもちろんデータリカバリに使用され、mysqlbinlogツールを使用してデータをリカバリ
  • 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が1を開いているかどうかを確認し、ONは2を開いていることを示し、より多くの内容を表示することができる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)
    

    2、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)
    

    3、logログを更新すると、新しい番号のbinlogログファイルが生成される
    mysql> flush logs;
    

    4、すべてのbinlogログをリセット(クリア)する
    mysql> reset master;
    

    三、binlogログの内容を表示する
    1、mysqlbinlogコマンドで表示する
      binlog      ,            ,       mysqlbinlog    
    *mysqlbinlog binlog.000002   mysqlbinlog    *
    

    2、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`
    

    3、クエリ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$f8Zs\ZhY(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)

    4、クエリ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コマンドが表示され、開始位置があり、データのリカバリに非常に役立ちます.5、最初の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文を実行することに相当します.ログ・リカバリの文、例えば、ライブラリにテーブル・ログが存在し、このテーブルの作成が実行されている場合は、必ず実行できません.したがって、特定の場所のリカバリ2を指定し、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
    

    3、指定時間復旧自分がどの時間帯に誤操作したかを知る
    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