MySQL/MariaDB數據庫的各類日誌管理

    MySQL/MariaDB數據庫的各類日誌管理
node

                          做者:尹正傑 mysql

版權聲明:原創做品,謝絕轉載!不然將追究法律責任。sql

 

 

一.事務日誌 (transaction log)數據庫

1>.Innodb事務日誌相關配置vim

MariaDB [yinzhengjie]> SHOW VARIABLES LIKE '%innodb_log%';
+-------------------------------+------------+
| Variable_name                 | Value      |
+-------------------------------+------------+
| innodb_log_arch_dir           |            |
| innodb_log_arch_expire_sec    | 0          |
| innodb_log_archive            | OFF        |
| innodb_log_block_size         | 0          |      #數據塊大小 
| innodb_log_buffer_size        | 16777216   |
| innodb_log_checksum_algorithm | DEPRECATED |
| innodb_log_checksums          | ON         |
| innodb_log_compressed_pages   | ON         |
| innodb_log_file_size          | 50331648   |      #每一個日誌文件總大小,MariaDB 10.2.x默認50M,生產環境能夠適當調大。
| innodb_log_files_in_group     | 2          |      #日誌組成員個數
| innodb_log_group_home_dir     | ./         |      #事務文件路徑,是數據目錄的相對路徑
| innodb_log_optimize_ddl       | ON         |
| innodb_log_write_ahead_size   | 8192       |
+-------------------------------+------------+
13 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 

2>.事務型存儲引擎自行管理和使用(建議和數據文件分開存放)安全

[root@node105.yinzhengjie.org.cn ~]# install -d /data/logs -o mysql -g mysql    #建立目錄並指定屬主和屬組
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /data/logs/ -d
drwxr-xr-x 2 mysql mysql 6 Nov  4 11:30 /data/logs/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /data/logs/ -a
total 0
drwxr-xr-x 2 mysql mysql  6 Nov  4 11:30 .
drwxr-xr-x 4 root  root  31 Nov  4 11:30 ..
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# install -d /data/logs -o mysql -g mysql    #建立目錄並指定屬主和屬組
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf
[mysqld]
innodb_log_group_home_dir = /data/logs           #指定事物日誌存放路徑爲"/data/logs"
innodb_log_file_size      = 10M                   #指定每一個日誌文件大小爲10M
innodb_log_files_in_group = 3                     #指定日誌組成員個數爲3個
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
autocommit              = 1
skip_name_resolve        = 1
userstat               = ON
port                   = 3306
datadir                 = /mysql/3306/data
socket                 = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid

[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      80                                          :::3306                                                    :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW VARIABLES LIKE '%innodb_log%';
+-------------------------------+------------+
| Variable_name                 | Value      |
+-------------------------------+------------+
| innodb_log_arch_dir           |            |
| innodb_log_arch_expire_sec    | 0          |
| innodb_log_archive            | OFF        |
| innodb_log_block_size         | 0          |
| innodb_log_buffer_size        | 16777216   |
| innodb_log_checksum_algorithm | DEPRECATED |
| innodb_log_checksums          | ON         |
| innodb_log_compressed_pages   | ON         |
| innodb_log_file_size          | 10485760   |
| innodb_log_files_in_group     | 3          |
| innodb_log_group_home_dir     | /data/logs |
| innodb_log_optimize_ddl       | ON         |
| innodb_log_write_ahead_size   | 8192       |
+-------------------------------+------------+
13 rows in set (0.00 sec)

MariaDB [(none)]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #修改默認事務日誌相關參數
[root@node105.yinzhengjie.org.cn ~]# ll /data/logs/        #重啓MySQL實例後,事務日誌被單獨存放啦,很顯然配置生效啦~
total 30720
-rw-rw---- 1 mysql mysql 10485760 Nov  4 11:39 ib_logfile0
-rw-rw---- 1 mysql mysql 10485760 Nov  4 11:39 ib_logfile1
-rw-rw---- 1 mysql mysql 10485760 Nov  4 11:39 ib_logfile2
[root@node105.yinzhengjie.org.cn ~]# 

3>.刷新日誌設置(innodb_flush_log_at_trx_commit)服務器

innodb_flush_log_at_trx_commit
  說明:
    設置爲1,同時sync_binlog = 1表示最高級別的容錯 innodb_use_global_flush_log_at_trx_commit的值肯定是否可使用SET語句重置此變量
    設置爲1默認狀況下,日誌緩衝區將寫入日誌文件,並在每次事務後執行刷新到磁盤。 這是徹底遵照ACID特性
    設置爲0提交時沒有任何操做; 而是每秒執行一第二天志緩衝區寫入和刷新。 這樣能夠提供更好的性能,但服務器崩潰能夠清除最後一秒的事務
    設置爲2每次提交後都會寫入日誌緩衝區,但每秒都會進行一次刷新。 性能比0略好一些,但操做系統或停電可能致使最後一秒的交易丟失  
    設置爲3模擬MariaDB 5.5組提交(每組提交3個同步),此項MariaDB 10.0支持
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE '%innodb_flush_log_at_trx_commit%';    #默認設置爲1
+--------------------------------+-------+
| Variable_name                  | Value |
+--------------------------------+-------+
| innodb_flush_log_at_trx_commit | 1     |
+--------------------------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE '%innodb_flush_log_at_trx_commit%';    #默認設置爲1

 

二.錯誤日誌 (error log)網絡

1>.錯誤日誌記錄內容session

  1.mysqld啓動和關閉過程當中輸出的事件信息;
  2.mysqld運行中產生的錯誤信息;
  3.event scheduler運行一個event時產生的日誌信息;
  4.在主從複製架構中的從服務器上啓動從服務器線程時產生的信息;

2>.錯誤日誌相關配置架構

錯誤文件路徑
  log_error=/PATH/TO/LOG_ERROR_FILE
是否記錄警告信息至錯誤日誌文件
  log_warnings 爲0, 表示不記錄告警信息。   log_warnings 爲1, 表示告警信息寫入錯誤日誌。   log_warnings 大於1, 表示各種告警信息,例若有關網絡故障的信息和從新鏈接信息寫入錯誤日誌(MariaDB 10.2.x版本默認爲2)。
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf        #指定錯誤日誌存放路徑,在啓動MySQL實例後"log_error"的路徑不可被修改,由於它是隻讀的。
[mysqld]
innodb_log_group_home_dir = /data/logs
innodb_log_file_size      = 10M
innodb_log_files_in_group = 3
log_warnings              = 10
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
autocommit          = 1
skip_name_resolve      = 1
userstat          = ON
port              = 3306
datadir              = /mysql/3306/data
socket              = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'log_error';
+---------------+-----------------------------+
| Variable_name | Value                       |
+---------------+-----------------------------+
| log_error     | /mysql/3306/log/mariadb.log |
+---------------+-----------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE 'log_warnings';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_warnings  | 10    |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [(none)]> 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf         #指定錯誤日誌存放路徑,在啓動MySQL實例後"log_error"的路徑不可被修改,由於它是隻讀的。

 

三.通用日誌(general log)

1>.通用日誌記錄內容

  記錄對數據庫的通用操做,包括錯誤的SQL語句
  存儲類型支持三種,即FILE/TABLE/NONE  
    文件:
file,默認值
    表:table
  用途:
    通常不建議開啓,除非數據庫須要優化,經過該日誌分析數據庫可能存在的問題,若數據庫優化完畢依然建議關閉它,開啓該功能存在IO操做,會影響服務器性能。

2>.通用日誌相關設置

general_log=ON|OFF
  指定通用日誌是否開啓。 general_log_file
=HOSTNAME.log
  指定通用日誌存放文件格式的名稱,默認文件名是:"主機名.log",存儲在MySQL數據庫同目錄中。 log_output
=TABLE|FILE|NONE
  指定輸出類型,默認爲FILE,固然也能夠指定爲TABLE,表示將日誌存放在mysql數據庫的默認general_log表中。
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET GLOBAL general_log = ON;      #臨時開啓通用日誌功能
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'general_log';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| general_log   | ON    |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET GLOBAL general_log = ON;      #臨時開啓通用日誌功能
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'general_log_file';      #查看默認的通用日誌名稱
+------------------+-------------+
| Variable_name    | Value       |
+------------------+-------------+
| general_log_file | node105.log |
+------------------+-------------+
row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/data/node105.log
/usr/local/mysql/bin/mysqld, Version: 10.2.19-MariaDB (MariaDB Server). started with:
Tcp port: 3306  Unix socket: /mysql/3306/socket/mysql.sock
Time                 Id Command    Argument
191104 16:40:38        9 Query    SHOW GLOBAL VARIABLES LIKE 'general_log'
191104 16:40:46       10 Query    SHOW TABLES
191104 16:41:39        9 Query    SHOW GLOBAL VARIABLES LIKE 'general_log_file'
191104 16:49:52        9 Query    SHOW GLOBAL VARIABLES LIKE 'log_output'
191104 16:49:55        9 Query    SHOW GLOBAL VARIABLES LIKE 'log_output'
191104 16:50:11        9 Query    SET GLOBAL log_output = table
191104 16:50:20        9 Query    SHOW GLOBAL VARIABLES LIKE 'log_output'
191104 16:50:53        9 Query    SET GLOBAL log_output = 'table'
191104 16:56:22       11 Quit    
191104 16:56:59       10 Query    SHOW VARIABLES LIKE '%innodb_log%'
191104 16:57:10       10 Query    SHOW GLOBAL VARIABLES LIKE 'log_output'
191104 16:58:17       10 Query    SHOW GLOBAL VARIABLES LIKE 'general_log_file'
191104 16:59:26       10 Quit    
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'general_log_file';      #查看默認的通用日誌名稱
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'log_output';    #查看默認的日誌輸出類型
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET GLOBAL log_output = 'table';
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | TABLE |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM mysql.general_log\G    #查看錶中的通用日誌記錄信息
*************************** 1. row ***************************
  event_time: 2019-11-04 16:50:55.178466
   user_host: root[root] @ localhost []
   thread_id: 9
   server_id: 1
command_type: Query
    argument: SHOW GLOBAL VARIABLES LIKE 'log_output'
*************************** 2. row ***************************
  event_time: 2019-11-04 16:51:24.556260
   user_host: root[root] @ localhost []
   thread_id: 9
   server_id: 1
command_type: Query
    argument: SELECT * FROM mysql.general_log
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW GLOBAL VARIABLES LIKE 'log_output';      #查看默認的日誌輸出類型
MariaDB [yinzhengjie]> SELECT * FROM mysql.general_log;
+----------------------------+---------------------------+-----------+-----------+--------------+-----------------------------------------+
| event_time                 | user_host                 | thread_id | server_id | command_type | argument                                |
+----------------------------+---------------------------+-----------+-----------+--------------+-----------------------------------------+
| 2019-11-04 16:50:55.178466 | root[root] @ localhost [] |         9 |         1 | Query        | SHOW GLOBAL VARIABLES LIKE 'log_output' |
| 2019-11-04 16:51:24.556260 | root[root] @ localhost [] |         9 |         1 | Query        | SELECT * FROM mysql.general_log         |
| 2019-11-04 16:54:30.403657 | root[root] @ localhost [] |         9 |         1 | Quit         |                                         |
| 2019-11-04 16:55:56.817442 | [root] @ localhost []     |        11 |         1 | Connect      | root@localhost as anonymous on          |
| 2019-11-04 16:55:56.820207 | root[root] @ localhost [] |        11 |         1 | Query        | select @@version_comment limit 1        |
| 2019-11-04 16:56:00.271469 | root[root] @ localhost [] |        11 |         1 | Query        | SELECT DATABASE()                       |
| 2019-11-04 16:56:00.271777 | root[root] @ localhost [] |        11 |         1 | Init DB      | yinzhengjie                             |
| 2019-11-04 16:56:18.084201 | root[root] @ localhost [] |        11 |         1 | Query        | SET GLOBAL log_output = 'file'          |
+----------------------------+---------------------------+-----------+-----------+--------------+-----------------------------------------+
8 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT argument,COUNT(*) FROM mysql.general_log GROUP BY argument;    #找出查詢此處最多的語句能夠來建立相關索引。
+-----------------------------------------+----------+
| argument                                | COUNT(*) |
+-----------------------------------------+----------+
|                                         |        1 |
| root@localhost as anonymous on          |        1 |
| SELECT * FROM mysql.general_log         |        1 |
| select @@version_comment limit 1        |        1 |
| SELECT DATABASE()                       |        1 |
| SET GLOBAL log_output = 'file'          |        1 |
| SHOW GLOBAL VARIABLES LIKE 'log_output' |        1 |
| yinzhengjie                             |        1 |
+-----------------------------------------+----------+
8 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
通常不建議開啓,除非數據庫須要優化,經過該日誌分析數據庫可能存在的問題,若數據庫優化完畢依然建議關閉它,會影響服務器性能。

 

四.慢查詢日誌 (slow query log)

1>.慢查詢日誌記錄內容

  記錄執行查詢時長超出指定時長的操做

2>.慢查詢相關設置

slow_query_log=ON|OFF 
  開啓或關閉慢查詢

long_query_time=N 
  慢查詢的閥值,單位秒

slow_query_log_file=HOSTNAME-slow.log 
  慢查詢日誌文件

log_slow_filter = admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk
  上述查詢類型且查詢時長超過long_query_time,則記錄日誌

log_queries_not_using_indexes=ON 
  不使用索引或使用全索引掃描,不管是否達到慢查詢閥值的語句是否記錄日誌,默認OFF,即不記錄

log_slow_rate_limit = 1 
  多少次查詢才記錄,mariadb特有

log_slow_verbosity= Query_plan,explain 
  記錄內容

log_slow_queries = OFF 
  同slow_query_log 新版已廢棄

3>.慢查詢相關參數使用案例

[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf
[mysqld]
innodb_log_group_home_dir = /data/logs
innodb_log_file_size      = 10M
innodb_log_files_in_group = 3
log_warnings              = 10
general_log           = ON
slow_query_log          = ON      #開啓慢查詢日誌
long_query_time          = 5      #指定慢查詢超時時間爲5秒就記錄到文件
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
autocommit          = 1
skip_name_resolve      = 1
userstat          = ON
port              = 3306
datadir              = /mysql/3306/data
socket              = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.19-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> SELECT SLEEP(1) FROM yinzhengjie.teachers;    #有多少條記錄這裏就會休眠幾秒,這裏4條記錄僅休眠4秒,而咱們定義的慢查詢日誌超時時間爲5秒,所以該條記錄不會被記錄。
+----------+
| SLEEP(1) |
+----------+
|        0 |
|        0 |
|        0 |
|        0 |
+----------+
4 rows in set (4.01 sec)

MariaDB [(none)]> 
MariaDB [(none)]> SELECT SLEEP(1) FROM yinzhengjie.students;    #同理,在yinzhengjie.students表有25行記錄,所以休眠5秒,符合咱們定義慢日誌查詢超時時間爲5秒,所以該條記錄會被記錄到慢查詢日誌中。
+----------+
| SLEEP(1) |
+----------+
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
|        0 |
+----------+
25 rows in set (25.03 sec)

MariaDB [(none)]> 
MariaDB [(none)]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/data/node105-slow.log       #不難發現和咱們上面分析的同樣,只有一條慢查詢日誌,固然慢查詢日誌不只僅包含SELECT語句喲,包括的是DML語句,存儲引擎等的執行超過指定秒數也會被記錄呢!
/usr/local/mysql/bin/mysqld, Version: 10.2.19-MariaDB-log (MariaDB Server). started with:
Tcp port: 3306  Unix socket: /mysql/3306/socket/mysql.sock
Time                 Id Command    Argument
# Time: 191104 17:24:14
# User@Host: root[root] @ localhost []
# Thread_id: 8  Schema:   QC_hit: No
# Query_time: 25.025535  Lock_time: 0.000404  Rows_sent: 25  Rows_examined: 25
# Rows_affected: 0
SET timestamp=1572859454;
SELECT SLEEP(1) FROM yinzhengjie.students;
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
詳情請戳我

4>.經過profiling工具分析慢查詢日誌

MariaDB [yinzhengjie]> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET profiling = ON;      #開啓profiling功能
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET profiling = ON;      #開啓profiling功能
MariaDB [yinzhengjie]> SHOW profiles;          #須要開啓profiling功能後,執行的SQL語句會被記錄
+----------+-------------+-------------------------------------------+
| Query_ID | Duration    | Query                                     |
+----------+-------------+-------------------------------------------+
|        1 |  0.00013446 | SELECT @@profiling                        |
|        2 |  4.00646956 | SELECT SLEEP(1) FROM yinzhengjie.teachers |
|        3 | 25.02319266 | SELECT SLEEP(1) FROM yinzhengjie.students |
+----------+-------------+-------------------------------------------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW PROFILE FOR QUERY 2;    #咱們查看上面第2條語句,對其分析查詢慢日誌的緣由
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000085 |
| checking permissions | 0.000007 |
| Opening tables       | 0.000017 |
| After opening tables | 0.000005 |
| System lock          | 0.000004 |
| Table lock           | 0.000006 |
| init                 | 0.000011 |
| optimizing           | 0.000007 |
| statistics           | 0.000020 |
| preparing            | 0.000018 |
| executing            | 0.000004 |
| Sending data         | 0.000022 |
| User sleep           | 1.002767 |      #不難發現,耗時最長的應該就是在sleep過程啦!
| User sleep           | 1.001184 |
| User sleep           | 1.000412 |
| User sleep           | 1.001772 |
| end                  | 0.000026 |
| query end            | 0.000022 |
| closing tables       | 0.000008 |
| Unlocking tables     | 0.000023 |
| freeing items        | 0.000011 |
| updating status      | 0.000031 |
| cleaning up          | 0.000008 |
+----------------------+----------+
23 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 

 

五.二進制日誌 (binary log,Oracle稱爲歸檔日誌)

1>.二進制日誌記錄內容

  記錄內容:
    記錄致使數據改變或潛在致使數據改變的SQL語句(即只記錄增刪改操做)
    記錄已提交的日誌
    不依賴於存儲引擎類型
  功能:
    經過「重放」日誌文件中的事件來生成數據副本
  舒適提示:
    建議生產環境中二進制日誌和數據文件分開存放,當數據文件存放目錄損壞,可經過二進制文件恢復。

2>.二進制日誌記錄格式

二進制日誌記錄三種格式:
  基於「語句」記錄:
    statement,只記錄語句,默認模式。該模式存在弊端,好比執行"UPDATE students SET birth = now();"沒法保存具體的時間戳,若按照該語句進行還原數據準確性確定出現問題。   基於「行」記錄:
    row,只記錄數據,即直接將數據存儲下來,但日誌量較大。適合數據相對來講重要的場景。推薦使用這種模式,數據恢復時準確的最高,但帶來的代價就是得犧牲更多的磁盤空間。建議健康磁盤剩餘空間進行及時擴充。   混合模式:
    mixed,相對來講比較折中的方式,讓系統自行斷定該基於哪一種方式進行。 格式配置:   SHOW VARIABLES
LIKE 'binlog_format';
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'binlog_format';    `    #查看二進制默認的記錄格式
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'binlog_format';    `    #查看MariaDB10.2.x二進制默認的記錄格式

3>.二進制日誌文件的構成

日誌文件:
  mysql|mariadb-bin.文件名後綴,二進制格式,如: mariadb-bin.000001

索引文件:
  mysql|mariadb-bin.index,文本格式,記錄在當前有效的二進制文件名稱

4>.二進制日誌相關的服務器變量

sql_log_bin=ON|OFF
  是否記錄二進制日誌,默認ON,默認啓用二進制文件功能,該變量是會話(session)級別無需重啓服務就可生效,可很靈活的控制二進制日誌的禁用和啓用。
  在批量導入大量數據時,咱們此時可用選擇不記錄二進制文件從而節省必定的磁盤空間使用,這個時候咱們就可用將該值設置爲OFF,可用臨時禁用二進制日誌功能。
log_bin
=/PATH/BIN_LOG_FILE:
  指定文件位置;默認OFF,表示不啓用二進制日誌功能,上述兩項(sql_log_bin和log_bin)都開啓纔可
binlog_format
=STATEMENT|ROW|MIXED:
  二進制日誌記錄的格式,MariaDB 5.5.x默認STATEMENT,而MariaDB 10.2.x默認爲MIXED。
max_binlog_size
=1073741824
  單個二進制日誌文件的最大致積,到達最大值會自動滾動,默認爲1G   說明:文件達到上限時的大小未必爲指定的精確值
sync_binlog
=1|0
  設定是否啓動二進制日誌即時同步磁盤功能,默認0,由操做系統負責同步日誌到磁盤
expire_logs_days
=N:
  二進制日誌能夠自動刪除的天數。 默認爲0,即不自動刪除
[root@node105.yinzhengjie.org.cn ~]# install -d /data/logbin -o mysql -g mysql    #建立二進制日誌存放目錄
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll -d /data/logbin/
drwxr-xr-x 2 mysql mysql 6 Nov  4 18:45 /data/logbin/
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# install -d /data/logbin -o mysql -g mysql    #建立二進制日誌存放目錄
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf    #配置二進制文件的默認存放路徑
[mysqld]
log_bin                 = /data/logbin/mysql-bin      #指定二進制日誌存放路徑及文件名稱前綴
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
port                   = 3306
datadir                 = /mysql/3306/data
socket                 = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /data/logbin/
total 0
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld start
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /data/logbin/        #啓動服務後二進制日誌存放到指定路徑中
total 8
-rw-rw---- 1 mysql mysql 328 Nov  4 18:52 mysql-bin.000001
-rw-rw---- 1 mysql mysql  30 Nov  4 18:52 mysql-bin.index
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /data/logbin/mysql-bin.index   #查看如今全部可用的二進制文件名稱
/data/logbin/mysql-bin.000001
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf    #配置二進制文件的默認存放路徑
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #配置二進制文件的默認格式
[mysqld]
log_bin                  = /data/logbin/mysql-bin
binlog_format            = ROW      #修改二進制的格式爲基於行的,這意味着須要更多的佔用磁盤使用空間。
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
port                   = 3306
datadir                 = /mysql/3306/data
socket                 = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# ll /data/logbin/              #每次重啓MySQL實例都會滾動二進制日誌文件
total 12
-rw-rw---- 1 mysql mysql 351 Nov  4 19:07 mysql-bin.000001
-rw-rw---- 1 mysql mysql 328 Nov  4 19:07 mysql-bin.000002
-rw-rw---- 1 mysql mysql  60 Nov  4 19:07 mysql-bin.index
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /data/logbin/mysql-bin.index     #查看全部可用的二進制文件
/data/logbin/mysql-bin.000001
/data/logbin/mysql-bin.000002
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf      #配置二進制文件的默認格式

5>.二進制日誌相關配置

查看mariadb自行管理使用中的二進制日誌文件列表,及大小:
  SHOW {BINARY | MASTER} LOGS

查看使用中的二進制日誌文件:
  SHOW MASTER STATUS

切換日誌文件:   FLUSH LOGS 查看二進制文件中的指定內容:   SHOW BINLOG EVENTS [IN
'log_name'] [FROM pos] [LIMIT [offset,] row_count]     如:SHOW BINLOG EVENTS IN 'mysql-bin.000003' FROM 285 LIMIT 3,4
  以上命令查看的信息並不詳細推薦使用mysqlbinlog工具進行查看。
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       328 |
+------------------+-----------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       328 |
+------------------+-----------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |      328 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       328 |
+------------------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> FLUSH LOGS;
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |       371 |
+------------------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/                  #使用SYSTEM調用系統命令查看操做系統的確也有對應的文件生成啦。
total 16
-rw-rw---- 1 mysql mysql 351 Nov  4 19:07 mysql-bin.000001
-rw-rw---- 1 mysql mysql 375 Nov  4 19:16 mysql-bin.000002
-rw-rw---- 1 mysql mysql 371 Nov  4 19:16 mysql-bin.000003
-rw-rw---- 1 mysql mysql  90 Nov  4 19:16 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> FLUSH LOGS;
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |       371 |
+------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 16
-rw-rw---- 1 mysql mysql 351 Nov  4 19:07 mysql-bin.000001
-rw-rw---- 1 mysql mysql 375 Nov  4 19:16 mysql-bin.000002
-rw-rw---- 1 mysql mysql 371 Nov  4 19:16 mysql-bin.000003
-rw-rw---- 1 mysql mysql  90 Nov  4 19:16 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yinzhengjie',18,'M');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Jason Yin     |  26 | M      |
|   6 | yinzhengjie   |  18 | M      |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |       688 |
+------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      688 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINLOG EVENTS IN 'mysql-bin.000003';    #查看二進制文件的內容
+------------------+-----+-------------------+-----------+-------------+-------------------------------------------------------------------------
------------------+| Log_name         | Pos | Event_type        | Server_id | End_log_pos | Info                                                                    
                  |+------------------+-----+-------------------+-----------+-------------+-------------------------------------------------------------------------
------------------+| mysql-bin.000003 |   4 | Format_desc       |         1 |         256 | Server ver: 10.2.19-MariaDB-log, Binlog ver: 4                          
                  || mysql-bin.000003 | 256 | Gtid_list         |         1 |         285 | []                                                                      
                  || mysql-bin.000003 | 285 | Binlog_checkpoint |         1 |         328 | mysql-bin.000002                                                        
                  || mysql-bin.000003 | 328 | Binlog_checkpoint |         1 |         371 | mysql-bin.000003                                                        
                  || mysql-bin.000003 | 371 | Gtid              |         1 |         413 | BEGIN GTID 0-1-1                                                        
                  || mysql-bin.000003 | 413 | Annotate_rows     |         1 |         525 | INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yin
zhengjie',18,'M') |
            | mysql-bin.000003 | 525 | Table_map         |         1 |         590 | table_id: 22 (yinzhengjie.teachers)                                     
                  || mysql-bin.000003 | 590 | Write_rows_v1     |         1 |         657 | table_id: 22 flags: STMT_END_F                                          
                  || mysql-bin.000003 | 657 | Xid               |         1 |         688 | COMMIT /* xid=16 */                                                     
                  |+------------------+-----+-------------------+-----------+-------------+-------------------------------------------------------------------------
------------------+

9 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINLOG EVENTS IN 'mysql-bin.000003';    #查看二進制文件的內容,查看的內容不詳細
MariaDB [yinzhengjie]> SHOW BINLOG EVENTS IN 'mysql-bin.000003';
+------------------+-----+-------------------+-----------+-------------+-------------------------------------------------------------------------
------------------+| Log_name         | Pos | Event_type        | Server_id | End_log_pos | Info                                                                    
                  |+------------------+-----+-------------------+-----------+-------------+-------------------------------------------------------------------------
------------------+| mysql-bin.000003 |   4 | Format_desc       |         1 |         256 | Server ver: 10.2.19-MariaDB-log, Binlog ver: 4                          
                  || mysql-bin.000003 | 256 | Gtid_list         |         1 |         285 | []                                                                      
                  || mysql-bin.000003 | 285 | Binlog_checkpoint |         1 |         328 | mysql-bin.000002                                                        
                  || mysql-bin.000003 | 328 | Binlog_checkpoint |         1 |         371 | mysql-bin.000003                                                        
                  || mysql-bin.000003 | 371 | Gtid              |         1 |         413 | BEGIN GTID 0-1-1                                                        
                  || mysql-bin.000003 | 413 | Annotate_rows     |         1 |         525 | INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yin
zhengjie',18,'M') |
           | mysql-bin.000003 | 525 | Table_map         |         1 |         590 | table_id: 22 (yinzhengjie.teachers)                                     
                  || mysql-bin.000003 | 590 | Write_rows_v1     |         1 |         657 | table_id: 22 flags: STMT_END_F                                          
                  || mysql-bin.000003 | 657 | Xid               |         1 |         688 | COMMIT /* xid=16 */                                                     
                  |+------------------+-----+-------------------+-----------+-------------+-------------------------------------------------------------------------
------------------+
9 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINLOG EVENTS IN 'mysql-bin.000003' FROM 285 LIMIT 2;
+------------------+-----+-------------------+-----------+-------------+------------------+
| Log_name         | Pos | Event_type        | Server_id | End_log_pos | Info             |
+------------------+-----+-------------------+-----------+-------------+------------------+
| mysql-bin.000003 | 285 | Binlog_checkpoint |         1 |         328 | mysql-bin.000002 |
| mysql-bin.000003 | 328 | Binlog_checkpoint |         1 |         371 | mysql-bin.000003 |
+------------------+-----+-------------------+-----------+-------------+------------------+
2 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINLOG EVENTS IN 'mysql-bin.000003' FROM 285 LIMIT 3,4;    #從指定位置查看相應的信息
+------------------+-----+---------------+-----------+-------------+-----------------------------------------------------------------------------
--------------+| Log_name         | Pos | Event_type    | Server_id | End_log_pos | Info                                                                        
              |+------------------+-----+---------------+-----------+-------------+-----------------------------------------------------------------------------
--------------+| mysql-bin.000003 | 413 | Annotate_rows |         1 |         525 | INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yinzhen
gjie',18,'M') |
         | mysql-bin.000003 | 525 | Table_map     |         1 |         590 | table_id: 22 (yinzhengjie.teachers)                                         
              || mysql-bin.000003 | 590 | Write_rows_v1 |         1 |         657 | table_id: 22 flags: STMT_END_F                                              
              || mysql-bin.000003 | 657 | Xid           |         1 |         688 | COMMIT /* xid=16 */                                                         
              |+------------------+-----+---------------+-----------+-------------+-----------------------------------------------------------------------------
--------------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINLOG EVENTS IN 'mysql-bin.000003' FROM 285 LIMIT 3,4;  #從指定位置查看相應的信息

6>.二進制日誌的客戶端命令工具(mysqlbinlog)

命令格式:
  mysqlbinlog [OPTIONS] log_file…
  --start-position=# 指定開始位置
  --stop-position=#
  --start-datetime=
  --stop-datetime=
  時間格式:YYYY-MM-DD hh:mm:ss
  --base64-output[=name]
  -v -vvv
[root@node105.yinzhengjie.org.cn ~]# ls -l /data/logbin/
total 16
-rw-rw---- 1 mysql mysql 351 Nov  4 19:07 mysql-bin.000001
-rw-rw---- 1 mysql mysql 375 Nov  4 19:16 mysql-bin.000002
-rw-rw---- 1 mysql mysql 688 Nov  4 19:25 mysql-bin.000003
-rw-rw---- 1 mysql mysql  90 Nov  4 19:16 mysql-bin.index
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000003   #可用查看二進制文件,但有基於Base64編碼的加密信息。
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 19:16:46 server id 1  end_log_pos 256 CRC32 0x4e4a71e5     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 19:16:46
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
ngjAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgHlcUpO
'/*!*/;
# at 256
#191104 19:16:46 server id 1  end_log_pos 285 CRC32 0x11540620     Gtid list []
# at 285
#191104 19:16:46 server id 1  end_log_pos 328 CRC32 0x3b612022     Binlog checkpoint mysql-bin.000002
# at 328
#191104 19:16:46 server id 1  end_log_pos 371 CRC32 0xce9f3b39     Binlog checkpoint mysql-bin.000003
# at 371
#191104 19:25:44 server id 1  end_log_pos 413 CRC32 0xbc3f3fe1     GTID 0-1-1 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=1*//*!*/;
BEGIN
/*!*/;
# at 413
# at 525
#191104 19:25:44 server id 1  end_log_pos 525 CRC32 0xe7b70b5b     Annotate_rows:
#Q> INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yinzhengjie',18,'M')
#191104 19:25:44 server id 1  end_log_pos 590 CRC32 0xd48ca5db     Table_map: `yinzhengjie`.`teachers` mapped to number 22
# at 590
#191104 19:25:44 server id 1  end_log_pos 657 CRC32 0x1a95ea95     Write_rows: table id 22 flags: STMT_END_F

BINLOG '
uArAXRMBAAAAQQAAAE4CAAAAABYAAAAAAAEAC3lpbnpoZW5namllAAh0ZWFjaGVycwAEAg8B/gQs
AfcBCNuljNQ=
uArAXRcBAAAAQwAAAJECAAAAABYAAAAAAAEABP/wBQAJAEphc29uIFlpbhoC8AYACwB5aW56aGVu
Z2ppZRICleqVGg==
'/*!*/;
# at 657
#191104 19:25:44 server id 1  end_log_pos 688 CRC32 0x6b6a92b0     Xid = 16
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000003   #可用查看二進制文件
[root@node105.yinzhengjie.org.cn ~]# ls -l /data/logbin/
total 16
-rw-rw---- 1 mysql mysql 351 Nov  4 19:07 mysql-bin.000001
-rw-rw---- 1 mysql mysql 375 Nov  4 19:16 mysql-bin.000002
-rw-rw---- 1 mysql mysql 688 Nov  4 19:25 mysql-bin.000003
-rw-rw---- 1 mysql mysql  90 Nov  4 19:16 mysql-bin.index
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000003 --verbose  #查看詳細信息
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 19:16:46 server id 1  end_log_pos 256 CRC32 0x4e4a71e5     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 19:16:46
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
ngjAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgHlcUpO
'/*!*/;
# at 256
#191104 19:16:46 server id 1  end_log_pos 285 CRC32 0x11540620     Gtid list []
# at 285
#191104 19:16:46 server id 1  end_log_pos 328 CRC32 0x3b612022     Binlog checkpoint mysql-bin.000002
# at 328
#191104 19:16:46 server id 1  end_log_pos 371 CRC32 0xce9f3b39     Binlog checkpoint mysql-bin.000003
# at 371
#191104 19:25:44 server id 1  end_log_pos 413 CRC32 0xbc3f3fe1     GTID 0-1-1 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=1*//*!*/;
BEGIN
/*!*/;
# at 413
# at 525
#191104 19:25:44 server id 1  end_log_pos 525 CRC32 0xe7b70b5b     Annotate_rows:
#Q> INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yinzhengjie',18,'M')
#191104 19:25:44 server id 1  end_log_pos 590 CRC32 0xd48ca5db     Table_map: `yinzhengjie`.`teachers` mapped to number 22
# at 590
#191104 19:25:44 server id 1  end_log_pos 657 CRC32 0x1a95ea95     Write_rows: table id 22 flags: STMT_END_F

BINLOG '
uArAXRMBAAAAQQAAAE4CAAAAABYAAAAAAAEAC3lpbnpoZW5namllAAh0ZWFjaGVycwAEAg8B/gQs
AfcBCNuljNQ=
uArAXRcBAAAAQwAAAJECAAAAABYAAAAAAAEABP/wBQAJAEphc29uIFlpbhoC8AYACwB5aW56aGVu
Z2ppZRICleqVGg==
'/*!*/;
### INSERT INTO `yinzhengjie`.`teachers`
### SET
###   @1=5
###   @2='Jason Yin'
###   @3=26
###   @4=2
### INSERT INTO `yinzhengjie`.`teachers`
### SET
###   @1=6
###   @2='yinzhengjie'
###   @3=18
###   @4=2
# at 657
#191104 19:25:44 server id 1  end_log_pos 688 CRC32 0x6b6a92b0     Xid = 16
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000003 --verbose  #查看詳細信息
MariaDB [yinzhengjie]> SELECT @@binlog_format;
+-----------------+
| @@binlog_format |
+-----------------+
| ROW             |
+-----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Jason Yin     |  26 | M      |
|   6 | yinzhengjie   |  18 | M      |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> UPDATE teachers SET gender='M';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 6  Changed: 2  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | M      |
|   4 | Lin Chaoying  |  93 | M      |
|   5 | Jason Yin     |  26 | M      |
|   6 | yinzhengjie   |  18 | M      |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      688 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000003 --verbose
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 19:16:46 server id 1  end_log_pos 256 CRC32 0x4e4a71e5     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 19:16:46
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
ngjAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgHlcUpO
'/*!*/;
# at 256
#191104 19:16:46 server id 1  end_log_pos 285 CRC32 0x11540620     Gtid list []
# at 285
#191104 19:16:46 server id 1  end_log_pos 328 CRC32 0x3b612022     Binlog checkpoint mysql-bin.000002
# at 328
#191104 19:16:46 server id 1  end_log_pos 371 CRC32 0xce9f3b39     Binlog checkpoint mysql-bin.000003
# at 371
#191104 19:25:44 server id 1  end_log_pos 413 CRC32 0xbc3f3fe1     GTID 0-1-1 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=1*//*!*/;
BEGIN
/*!*/;
# at 413
# at 525
#191104 19:25:44 server id 1  end_log_pos 525 CRC32 0xe7b70b5b     Annotate_rows:
#Q> INSERT INTO teachers (name,age,gender) VALUES ('Jason Yin',26,'M'),('yinzhengjie',18,'M')
#191104 19:25:44 server id 1  end_log_pos 590 CRC32 0xd48ca5db     Table_map: `yinzhengjie`.`teachers` mapped to number 22
# at 590
#191104 19:25:44 server id 1  end_log_pos 657 CRC32 0x1a95ea95     Write_rows: table id 22 flags: STMT_END_F

BINLOG '
uArAXRMBAAAAQQAAAE4CAAAAABYAAAAAAAEAC3lpbnpoZW5namllAAh0ZWFjaGVycwAEAg8B/gQs
AfcBCNuljNQ=
uArAXRcBAAAAQwAAAJECAAAAABYAAAAAAAEABP/wBQAJAEphc29uIFlpbhoC8AYACwB5aW56aGVu
Z2ppZRICleqVGg==
'/*!*/;
### INSERT INTO `yinzhengjie`.`teachers`
### SET
###   @1=5
###   @2='Jason Yin'
###   @3=26
###   @4=2
### INSERT INTO `yinzhengjie`.`teachers`
### SET
###   @1=6
###   @2='yinzhengjie'
###   @3=18
###   @4=2
# at 657
#191104 19:25:44 server id 1  end_log_pos 688 CRC32 0x6b6a92b0     Xid = 16
COMMIT/*!*/;
# at 688
#191104 19:47:27 server id 1  end_log_pos 730 CRC32 0x740f1f3c     GTID 0-1-2 trans
/*!100001 SET @@session.gtid_seq_no=2*//*!*/;
BEGIN
/*!*/;
# at 730
# at 783
#191104 19:47:27 server id 1  end_log_pos 783 CRC32 0x782ca82d     Annotate_rows:
#Q> UPDATE teachers SET gender='M'
#191104 19:47:27 server id 1  end_log_pos 848 CRC32 0x37af4016     Table_map: `yinzhengjie`.`teachers` mapped to number 22
# at 848
#191104 19:47:27 server id 1  end_log_pos 960 CRC32 0xc306f092     Update_rows: table id 22 flags: STMT_END_F

BINLOG '
zw/AXRMBAAAAQQAAAFADAAAAABYAAAAAAAEAC3lpbnpoZW5namllAAh0ZWFjaGVycwAEAg8B/gQs
AfcBCBZArzc=
zw/AXRgBAAAAcAAAAMADAAAAABYAAAAAAAEABP//8AMADQBNaWVqdWUgU2hpdGFpTQHwAwANAE1p
ZWp1ZSBTaGl0YWlNAvAEAAwATGluIENoYW95aW5nXQHwBAAMAExpbiBDaGFveWluZ10CkvAGww==
'/*!*/;
### UPDATE `yinzhengjie`.`teachers`
### WHERE
###   @1=3
###   @2='Miejue Shitai'
###   @3=77
###   @4=1
### SET
###   @1=3
###   @2='Miejue Shitai'
###   @3=77
###   @4=2
### UPDATE `yinzhengjie`.`teachers`
### WHERE
###   @1=4
###   @2='Lin Chaoying'
###   @3=93
###   @4=1
### SET
###   @1=4
###   @2='Lin Chaoying'
###   @3=93
###   @4=2
# at 960
#191104 19:47:27 server id 1  end_log_pos 991 CRC32 0x70750a35     Xid = 38
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
查看"binlog_format=ROW"格式的二進制文件信息(佔用更多磁盤空間,但有利於數據恢復。從數據安全角度來講,生產環境推薦使用該格式)
[root@node105.yinzhengjie.org.cn ~]# vim /mysql/3306/etc/my.cnf
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat /mysql/3306/etc/my.cnf
[mysqld]
log_bin              = /data/logbin/mysql-bin
binlog_format          = STATEMENT
character-set-server      = utf8mb4
default_storage_engine    = InnoDB
port              = 3306
datadir              = /mysql/3306/data
socket              = /mysql/3306/socket/mysql.sock


[mysqld_safe]
log-error        = /mysql/3306/log/mariadb.log
pid-file        = /mysql/3306/pid/mariadb.pid
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# /mysql/3306/mysqld restart
Restarting MySQL...
Stoping MySQL...
Starting MySQL...
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie -S /mysql/3306/socket/mysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.19-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
MariaDB [(none)]> use yinzhengjie
Database changed
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | M      |
|   4 | Lin Chaoying  |  93 | M      |
|   5 | Jason Yin     |  26 | M      |
|   6 | yinzhengjie   |  18 | M      |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> UPDATE teachers SET gender='F';
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6  Changed: 6  Warnings: 0

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | F      |
|   2 | Zhang Sanfeng |  94 | F      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
|   5 | Jason Yin     |  26 | F      |
|   6 | yinzhengjie   |  18 | F      |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |      1014 |
| mysql-bin.000004 |       519 |
+------------------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      519 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT @@binlog_format;
+-----------------+
| @@binlog_format |
+-----------------+
| STATEMENT       |
+-----------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> QUIT
Bye
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000004 --verbose
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 19:52:54 server id 1  end_log_pos 256 CRC32 0xdbb83886     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 19:52:54 at startu
p# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
FhHAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAWEcBdEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgGGOLjb
'/*!*/;
# at 256
#191104 19:52:54 server id 1  end_log_pos 299 CRC32 0x82b331c3     Gtid list [0-1-2]
# at 299
#191104 19:52:54 server id 1  end_log_pos 342 CRC32 0xc2e3f301     Binlog checkpoint mysql-bin.000004
# at 342
#191104 19:53:18 server id 1  end_log_pos 384 CRC32 0x85f8e293     GTID 0-1-3 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=3*//*!*/;
BEGIN
/*!*/;
# at 384
#191104 19:53:18 server id 1  end_log_pos 488 CRC32 0xb6fc6c2b     Query    thread_id=9    exec_time=0    error_code=0
use `yinzhengjie`/*!*/;
SET TIMESTAMP=1572868398/*!*/;
SET @@session.pseudo_thread_id=9/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1, @@session.check_constraint_c
hecks=1/*!*/;SET @@session.sql_mode=1411383296/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=45,@@session.collation_connection=45,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
UPDATE teachers SET gender='F'
/*!*/;
# at 488
#191104 19:53:18 server id 1  end_log_pos 519 CRC32 0x636e91ac     Xid = 5
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
查看"binlog_format=STATEMENT"格式的二進制文件信息(佔用較少磁盤空間,但不利於數據恢復)
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000004 -v --start-position=751 --stop-position=941;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 19:52:54 server id 1  end_log_pos 256 CRC32 0xdbb83886     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 19:52:54 at startu
p# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
FhHAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAWEcBdEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgGGOLjb
'/*!*/;
# at 751
#191104 21:32:38 server id 1  end_log_pos 868 CRC32 0xac80a98f     Query    thread_id=11    exec_time=0    error_code=0
use `yinzhengjie`/*!*/;
SET TIMESTAMP=1572874358/*!*/;
SET @@session.pseudo_thread_id=11/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1, @@session.check_constraint_c
hecks=1/*!*/;SET @@session.sql_mode=1411383296/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=45,@@session.collation_connection=45,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
UPDATE teachers SET gender='M' WHERE tid =4
/*!*/;
# at 868
#191104 21:32:38 server id 1  end_log_pos 899 CRC32 0x3dd0af4d     Xid = 20
COMMIT/*!*/;
# at 899
#191104 21:32:41 server id 1  end_log_pos 941 CRC32 0x42fa3228     GTID 0-1-6 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=6*//*!*/;
BEGIN
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
根據二進制的起始結束位置查看相應日誌記錄信息
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000004 -v --start-datetime='2019-11-04 21:35:00'
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 19:52:54 server id 1  end_log_pos 256 CRC32 0xdbb83886     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 19:52:54 at startu
p# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
FhHAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAWEcBdEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgGGOLjb
'/*!*/;
# at 1089
#191104 21:38:40 server id 1  end_log_pos 1131 CRC32 0x7bb72842     GTID 0-1-7 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=7*//*!*/;
BEGIN
/*!*/;
# at 1131
#191104 21:38:40 server id 1  end_log_pos 1225 CRC32 0x02f0ddbe     Query    thread_id=11    exec_time=0    error_code=0
use `yinzhengjie`/*!*/;
SET TIMESTAMP=1572874720/*!*/;
SET @@session.pseudo_thread_id=11/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1, @@session.check_constraint_c
hecks=1/*!*/;SET @@session.sql_mode=1411383296/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8mb4 *//*!*/;
SET @@session.character_set_client=45,@@session.collation_connection=45,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
DELETE FROM teachers
/*!*/;
# at 1225
#191104 21:38:40 server id 1  end_log_pos 1256 CRC32 0x96cf1a5c     Xid = 23
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
根據二進制日誌的時間查看相應的日誌記錄信息

7>.經過二進制日誌數據恢復案例

MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |      1014 |
| mysql-bin.000004 |      1256 |
+------------------+-----------+
4 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> FLUSH LOGS;      #爲了測試方便,我這裏世界使用一個新的日誌
Query OK, 0 rows affected (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |      1014 |
| mysql-bin.000004 |      1303 |
| mysql-bin.000005 |       385 |
+------------------+-----------+
5 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |      385 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> FLUSH LOGS;      #爲了測試方便,我這裏世界使用一個新的日誌
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |      385 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT @@binlog_format;
+-----------------+
| @@binlog_format |
+-----------------+
| STATEMENT       |
+-----------------+
row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | ON    |
+---------------+-------+
row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;
Empty set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO teachers VALUES (1,'Jason Yin',27,'F');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO teachers VALUES (2,'YinZhengjie',18,'F');
Query OK, 1 row affected (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> INSERT INTO teachers VALUES (3,'Jenny',20,'M');
Query OK, 1 row affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;
+-----+-------------+-----+--------+
| TID | Name        | Age | Gender |
+-----+-------------+-----+--------+
|   1 | Jason Yin   |  27 | F      |
|   2 | YinZhengjie |  18 | F      |
|   3 | Jenny       |  20 | M      |
+-----+-------------+-----+--------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> DELETE FROM teachers;
Query OK, 3 rows affected (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |     1141 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
執行DML語句
[root@node105.yinzhengjie.org.cn ~]# ls -l /data/logbin/
total 24
-rw-rw---- 1 mysql mysql  351 Nov  4 19:07 mysql-bin.000001
-rw-rw---- 1 mysql mysql  375 Nov  4 19:16 mysql-bin.000002
-rw-rw---- 1 mysql mysql 1014 Nov  4 19:52 mysql-bin.000003
-rw-rw---- 1 mysql mysql 1303 Nov  4 21:53 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1141 Nov  4 22:06 mysql-bin.000005
-rw-rw---- 1 mysql mysql  150 Nov  4 21:53 mysql-bin.index
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000005 -v > /root/binlog.sql
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# vim binlog.sql 
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# cat binlog.sql 
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#191104 21:38:40 server id 1  end_log_pos 256 CRC32 0xe7202c47     Start: binlog v 4, server v 10.2.19-MariaDB-log created 191104 21:38:40
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
4CnAXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4xOS1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgFHLCDn
'/*!*/;
# at 256
#191104 21:38:40 server id 1  end_log_pos 299 CRC32 0xd816dae3     Gtid list [0-1-7]
# at 299
#191104 21:38:40 server id 1  end_log_pos 342 CRC32 0xd0a61645     Binlog checkpoint mysql-bin.000004
# at 342
#191104 21:53:37 server id 1  end_log_pos 385 CRC32 0x10d221ba     Binlog checkpoint mysql-bin.000005
# at 385
#191104 21:38:40 server id 1  end_log_pos 427 CRC32 0x85937970     GTID 0-1-7 trans
/*!100101 SET @@session.skip_parallel_replication=0*//*!*/;
/*!100001 SET @@session.gtid_domain_id=0*//*!*/;
/*!100001 SET @@session.server_id=1*//*!*/;
/*!100001 SET @@session.gtid_seq_no=7*//*!*/;
BEGIN
/*!*/;
# at 427
#191104 21:38:40 server id 1  end_log_pos 551 CRC32 0x60abf36f     Query    thread_id=9    exec_time=1636    error_code=0
use `yinzhengjie`/*!*/;
SET TIMESTAMP=1572874720/*!*/;
SET @@session.pseudo_thread_id=9/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1, @@session.check_constraint_c
hecks=1/*!*/;SET @@session.sql_mode=1411383296/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=45,@@session.collation_server=45/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
INSERT INTO teachers VALUES (1,'Jason Yin',27,'F')
/*!*/;
# at 551
#191104 21:38:40 server id 1  end_log_pos 582 CRC32 0x84f3837a     Xid = 106
COMMIT/*!*/;
# at 582
#191104 21:38:40 server id 1  end_log_pos 624 CRC32 0xc9e4ee56     GTID 0-1-8 trans
/*!100001 SET @@session.gtid_seq_no=8*//*!*/;
BEGIN
/*!*/;
# at 624
#191104 21:38:40 server id 1  end_log_pos 750 CRC32 0x3b86f49f     Query    thread_id=9    exec_time=1643    error_code=0
SET TIMESTAMP=1572874720/*!*/;
INSERT INTO teachers VALUES (2,'YinZhengjie',18,'F')
/*!*/;
# at 750
#191104 21:38:40 server id 1  end_log_pos 781 CRC32 0x9b4e967d     Xid = 107
COMMIT/*!*/;
# at 781
#191104 21:38:40 server id 1  end_log_pos 823 CRC32 0xea5e6c78     GTID 0-1-9 trans
/*!100001 SET @@session.gtid_seq_no=9*//*!*/;
BEGIN
/*!*/;
# at 823
#191104 21:38:40 server id 1  end_log_pos 943 CRC32 0x0a560dec     Query    thread_id=9    exec_time=1652    error_code=0
SET TIMESTAMP=1572874720/*!*/;
INSERT INTO teachers VALUES (3,'Jenny',20,'M')
/*!*/;
# at 943
#191104 21:38:40 server id 1  end_log_pos 974 CRC32 0x2f20a2ac     Xid = 108
COMMIT/*!*/;
# at 974
#191104 21:38:40 server id 1  end_log_pos 1016 CRC32 0xbd542f84     GTID 0-1-10 trans
/*!100001 SET @@session.gtid_seq_no=10*//*!*/;
BEGIN
/*!*/;
# at 1016
#191104 21:38:40 server id 1  end_log_pos 1110 CRC32 0x01e0a619     Query    thread_id=9    exec_time=1668    error_code=0
SET TIMESTAMP=1572874720/*!*/;
#DELETE FROM teachers        #將這條刪除語句給註釋掉,經過當前二進制文件進行還原。
/*!*/;
# at 1110
#191104 21:38:40 server id 1  end_log_pos 1141 CRC32 0x77eb3332     Xid = 110
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@node105.yinzhengjie.org.cn ~]# 
[root@node105.yinzhengjie.org.cn ~]# 
查看二進制日誌文件,並將DML這種的DELETE語句註釋掉
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | ON    |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET sql_log_bin=OFF;        #將二進制日誌關閉,由於咱們須要手動還原表中的數據。還原過程無需記錄日誌
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;        #查看該表數據未空
Empty set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SOURCE binlog.sql            #經過我們修改的數據進行還原
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Charset changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

ERROR at line 81 in file: 'binlog.sql': No query specified

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SELECT * FROM teachers;        #還原後發現表中的數據的確存在啦
+-----+-------------+-----+--------+
| TID | Name        | Age | Gender |
+-----+-------------+-----+--------+
|   1 | Jason Yin   |  27 | F      |
|   2 | YinZhengjie |  18 | F      |
|   3 | Jenny       |  20 | M      |
+-----+-------------+-----+--------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SET sql_log_bin=ON;          #作完數據恢復操做後記得將二進制日誌文件功能打開。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW VARIABLES LIKE 'sql_log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sql_log_bin   | ON    |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
基於編輯後的二進制文件進行數據表的恢復操做

8>.二進制日誌事件的格式

[root@node105.yinzhengjie.org.cn ~]# mysqlbinlog /data/logbin/mysql-bin.000005 -v
......
# at 1016 #191104 21:38:40 server id 1 end_log_pos 1110 CRC32 0x01e0a619 Query thread_id=9 exec_time=1668 error_code=0 SET TIMESTAMP=1572874720/*!*/; DELETE FROM teachers /*!*/; ......
以上記錄觀點點說明:   事件發生的日期和時間:
191104 21:38:40   事件發生的服務器標識:server id 1   事件的結束位置:end_log_pos 1110   事件的類型:Query   事件發生時所在服務器執行此事件的線程的ID:thread_id=9   語句的時間戳與將其寫入二進制文件中的時間差:exec_time=1668   錯誤代碼:error_code=0   事件內容:"DELETE FROM teachers"

9>.清除指定二進制日誌(生產環境中建議保留半個月以上的日誌)

MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       351 |
| mysql-bin.000002 |       375 |
| mysql-bin.000003 |      1014 |
| mysql-bin.000004 |      1303 |
| mysql-bin.000005 |      1141 |
+------------------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> PURGE BINARY LOGS TO 'mysql-bin.000003';          #刪除"mysql-bin.000003"以前的日誌
Query OK, 0 rows affected (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000003 |      1014 |
| mysql-bin.000004 |      1303 |
| mysql-bin.000005 |      1141 |
+------------------+-----------+
rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 16
-rw-rw---- 1 mysql mysql 1014 Nov  4 19:52 mysql-bin.000003
-rw-rw---- 1 mysql mysql 1303 Nov  4 21:53 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1141 Nov  4 22:06 mysql-bin.000005
-rw-rw---- 1 mysql mysql   90 Nov  4 22:21 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM cat /data/logbin/mysql-bin.index
/data/logbin/mysql-bin.000003
/data/logbin/mysql-bin.000004
/data/logbin/mysql-bin.000005
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> PURGE BINARY LOGS TO 'mysql-bin.000003';         #刪除"mysql-bin.000003"以前的日誌
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 16
-rw-rw---- 1 mysql mysql 1014 Nov  4 19:52 mysql-bin.000003
-rw-rw---- 1 mysql mysql 1303 Nov  4 21:53 mysql-bin.000004
-rw-rw---- 1 mysql mysql 1141 Nov  4 22:06 mysql-bin.000005
-rw-rw---- 1 mysql mysql   90 Nov  4 22:21 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000003 |      1014 |
| mysql-bin.000004 |      1303 |
| mysql-bin.000005 |      1141 |
+------------------+-----------+
3 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> PURGE BINARY LOGS BEFORE '2019-11-4 22:00:00';
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000005 |      1141 |
+------------------+-----------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 8
-rw-rw---- 1 mysql mysql 1141 Nov  4 22:06 mysql-bin.000005
-rw-rw---- 1 mysql mysql   30 Nov  4 22:27 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM cat /data/logbin/mysql-bin.index
/data/logbin/mysql-bin.000005
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> PURGE BINARY LOGS BEFORE '2019-11-4 22:00:00';     #刪除"2019-11-4 22:00:00"以前的日誌

10>.刪除全部二進制日誌,index文件從新記數

  RESET MASTER [TO #]; 刪除全部二進制日誌文件,並從新生成日誌文件,文件名從#開始記數,默認從1開始,通常是master主機第一次啓動時執行,MariaDB10.1.6開始支持TO #
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000005 |      1188 |
| mysql-bin.000006 |       432 |
| mysql-bin.000007 |       432 |
| mysql-bin.000008 |       432 |
| mysql-bin.000009 |       432 |
| mysql-bin.000010 |       432 |
| mysql-bin.000011 |       385 |
+------------------+-----------+
7 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 32
-rw-rw---- 1 mysql mysql 1188 Nov  4 22:30 mysql-bin.000005
-rw-rw---- 1 mysql mysql  432 Nov  4 22:30 mysql-bin.000006
-rw-rw---- 1 mysql mysql  432 Nov  4 22:30 mysql-bin.000007
-rw-rw---- 1 mysql mysql  432 Nov  4 22:30 mysql-bin.000008
-rw-rw---- 1 mysql mysql  432 Nov  4 22:30 mysql-bin.000009
-rw-rw---- 1 mysql mysql  432 Nov  4 22:30 mysql-bin.000010
-rw-rw---- 1 mysql mysql  385 Nov  4 22:30 mysql-bin.000011
-rw-rw---- 1 mysql mysql  210 Nov  4 22:30 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> RESET MASTER;                #刪除全部二進制文件,並從新生產日誌文件,文件名稱從默認從1開始計數。
Query OK, 0 rows affected (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 8
-rw-rw---- 1 mysql mysql 328 Nov  4 22:31 mysql-bin.000001
-rw-rw---- 1 mysql mysql  30 Nov  4 22:31 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       328 |
+------------------+-----------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> RESET MASTER;        #刪除全部二進制文件,並從新生產日誌文件,文件名稱從默認從1開始計數。
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       375 |
| mysql-bin.000002 |       418 |
| mysql-bin.000003 |       418 |
| mysql-bin.000004 |       418 |
| mysql-bin.000005 |       418 |
| mysql-bin.000006 |       418 |
| mysql-bin.000007 |       371 |
+------------------+-----------+
7 rows in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 32
-rw-rw---- 1 mysql mysql 375 Nov  4 22:33 mysql-bin.000001
-rw-rw---- 1 mysql mysql 418 Nov  4 22:33 mysql-bin.000002
-rw-rw---- 1 mysql mysql 418 Nov  4 22:33 mysql-bin.000003
-rw-rw---- 1 mysql mysql 418 Nov  4 22:33 mysql-bin.000004
-rw-rw---- 1 mysql mysql 418 Nov  4 22:33 mysql-bin.000005
-rw-rw---- 1 mysql mysql 418 Nov  4 22:33 mysql-bin.000006
-rw-rw---- 1 mysql mysql 371 Nov  4 22:33 mysql-bin.000007
-rw-rw---- 1 mysql mysql 210 Nov  4 22:33 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> RESET MASTER TO 3;        #刪除全部二進制日誌並指定起始文件名稱數字爲3
Query OK, 0 rows affected (0.01 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SYSTEM ls -l /data/logbin/
total 8
-rw-rw---- 1 mysql mysql 328 Nov  4 22:34 mysql-bin.000003
-rw-rw---- 1 mysql mysql  30 Nov  4 22:34 mysql-bin.index
MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> SHOW BINARY LOGS;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000003 |       328 |
+------------------+-----------+
1 row in set (0.00 sec)

MariaDB [yinzhengjie]> 
MariaDB [yinzhengjie]> RESET MASTER TO 3;        #刪除全部二進制日誌並指定起始文件名稱數字爲3

 

六.中繼日誌(reley log)

  主從複製架構中,從服務器用於保存從主服務器的二進制日誌中讀取的事件。
相關文章
相關標籤/搜索