mysql基礎之日志管理(查詢日志、慢查詢日志、錯誤日志、二進制日志、中繼日志、事務日志)


  日志文件記錄了MySQL數據庫的各種類型的活動,MySQL數據庫中常見的日志文件有 查詢日志,慢查詢日志,錯誤日志,二進制日志,中繼日志 ,事務日志

  修改配置或者想要使配置永久生效需將內容寫入配置文件中:/etc/my.cnf.d/server.cnf

一、查詢日志

  查詢日志在mysql中稱為general log(通用日志),查詢日志記錄了數據庫執行的命令,不管這些語句是否正確,都會被記錄。由於數據庫操作命令有可能非常多而且執行比較頻繁,所以開啟了查詢日志以后,數據庫可能需要不停的寫入查詢日志,這樣會增大服務器的IO壓力,增加很多系統開銷,影響數據庫的性能,所以默認情況下是關閉的,也不建議開啟。

存儲查詢日志的方式:

  方式1:將查詢日志存放於指定的日志文件中;

  方式2:將查詢日志存放於mysql.general_log表中;

  方式3:將查詢日志同時存放於指定的日志文件與mysql庫的general_log表中。

1、查看查詢日志的相關參數

MariaDB [mysql]> show global variables like '%gen%log%';
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| general_log      | OFF      |
| general_log_file | ren7.log |
+------------------+----------+
2 rows in set (0.00 sec)

MariaDB [mysql]> show variables where variable_name like '%general_log%' or variable_name='log_output';
+------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| general_log      | OFF      |
| general_log_file | ren7.log |
| log_output       | FILE     |
+------------------+----------+
3 rows in set (0.00 sec)

2、查詢日志變量詳解

1 general_log:                指定是否開啟查詢日志(ON表示開啟,OFF表示未開啟,默認OFF)
2 general_log_file:           當log_output設置為“FILE”時,指定將查詢日志保存成哪個文件、叫什么名,默認與主機名相同,一般位於/var/lib/mysql目錄下
3 log_output:           指定將記錄到的查詢保存到什么位置(“NONE”、“FILE”、“TABLE”、“FILE,TABLE”)
4 file:                       保存成一個文件
5 table:                      保存成一張表
6 none:                       不記錄

 二、慢查詢日志**

  慢查詢日志用來記錄響應時間超過閾值的SQL語句,所以我們可以設置一個閾值,將運行時間超過該值的所有SQL語句都記錄到慢查詢日志文件中。該閾值可以通過參數 slow_launch_time來設置,默認為2秒。

1、查看慢查詢日志的變量

MariaDB [mysql]> show global variables like '%slow%';
+---------------------------+--------------------------------------------------------------------------------------------------------------+
| Variable_name             | Value                                                                                                        |
+---------------------------+--------------------------------------------------------------------------------------------------------------+
| log_slow_admin_statements | ON                                                                                                           |
| log_slow_filter           | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk |
| log_slow_rate_limit       | 1                                                                                                            |
| log_slow_slave_statements | ON                                                                                                           |
| log_slow_verbosity        |                                                                                                              |
| slow_launch_time          | 2                                                                                                            |
| slow_query_log            | OFF                                                                                                          |
| slow_query_log_file       | ren7-slow.log                                                                                                |
+---------------------------+--------------------------------------------------------------------------------------------------------------+
8 rows in set (0.00 sec)

2、變量詳解

1 slow_query_log = OFF|ON0|1)      #開啟慢查詢日志
2 slow_query_log_file = LOCALHOST-SLOW.log #慢查詢日志的文件路徑
3 long_query_time                 #慢查詢時長;默認是10s
4 log_slow_rate_limit              #如果要記錄的慢查詢日志非常多的話,會按照速率來記錄,默認1秒記錄一個
5 log_slow_verbosity=full | query_plan    #記錄的詳細級別
6 log_output                    #指定將記錄到的查詢保存到什么位置

3、開啟慢查詢日志及測試

MariaDB [(none)]> set global slow_query_log=on;  #當前會話不生效,需重新連接數據庫
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select @@global.slow_query_log;
+-------------------------+
| @@global.slow_query_log |
+-------------------------+
|                       1 |
+-------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> select sleep(15);  #測試4次
+-----------+
| sleep(15) |
+-----------+
|         0 |
+-----------+
1 row in set (15.00 sec)
[root@ren7 mysql]# tailf /var/lib/mysql/ren7-slow.log  #查看慢查詢日志的文件(默認保存類型是FILE)
 # Time: 190907 15:47:18
# User@Host: root[root] @ localhost []
# Thread_id: 10  Schema:   QC_hit: No
# Query_time: 15.008583  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
# Rows_affected: 0
SET timestamp=1567842438;
select sleep(15);
MariaDB [ren]> show global status like '%slow_queries%';  #查看一共記錄了多少條慢查詢語句
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries  | 4     |
+---------------+-------+
1 row in set (0.00 sec)

4、mysqldumpslow命令

  mysqldumpslow是mysql自帶的慢查詢日志統計分析工具,可以對慢查詢日志進行排序、查找、統計(只有我們將log_output的值設置為“FILE”或者“FILE,TABLE”時,mysqldumpslow才可以用)

(1)使用

[root@ren7 ~]# mysqldumpslow -s t /var/lib/mysql/ren7-slow.log 

Reading mysql slow query log from /var/lib/mysql/ren7-slow.log
Count: 4  Time=15.01s (60s)  Lock=0.00s (0s)  Rows_sent=1.0 (4), Rows_examined=0.0 (0), Rows_affected=0.0 (0), root[root]@localhost
  select sleep(N)

(2)參數說明

[root@ren7 ~]# mysqldumpslow --help
Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]

Parse and summarize the MySQL slow query log. Options are

  --verbose    verbose
  --debug      debug
  --help       write this text to standard output

  -v           verbose
  -d           debug
  -s ORDER     what to sort by (aa, ae, al, ar, at, a, c, e, l, r, t), 'at' is default
                aa: average rows affected
                ae: aggregated rows examined
                al: average lock time(平均鎖定時間)
                ar: average rows sent(平均返回記錄數)
                at: average query time(平均執行時間)
                 a: rows affected
                 c: count(執行計數)
                 e: rows examined 
                 l: lock time(鎖定時間)
                 r: rows sent(返回記錄)
                 t: query time(執行時間)  
  -r           reverse the sort order (largest last instead of first)
  -t NUM       just show the top n queries(指定只查看多少條統計信息)
  -a           don't abstract all numbers to N and strings to 'S'
  -n NUM       abstract numbers with at least n digits within names
  -g PATTERN   grep: only consider stmts that include this string(正則表達式)
  -h HOSTNAME  hostname of db server for *-slow.log filename (can be wildcard),
               default is '*', i.e. match all
  -i NAME      name of server instance (if using mysql.server startup script)
  -l           don't subtract lock time from total time

三、錯誤日志

主要記錄:(很重要的信息日志文件)

(1)mysqld啟動和關閉過程中輸出的事件信息
(2)mysqld運行中產生的錯誤信息
(3)event scheduler 運行一個event時產生的日志信息
(4)在主從復制架構中的從服務器IO復制線程時產生的信息

1、查看參數

MariaDB [mysql]> show variables like '%log_error%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_error     |       |
+---------------+-------+
1 row in set (0.00 sec)

MariaDB [mysql]> show variables like '%log_warnings%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_warnings  | 2     |
+---------------+-------+
1 row in set (0.01 sec)

2、參數詳解

1 log_error = /var/log/mysql_error.log  #指定錯誤日志的輸出位置
2 log_warnings 為0, 表示不記錄告警信息。
3 log_warnings 為1, 表示告警信息寫入錯誤日志。
4 log_warnings 大於1, 表示各類告警信息,例如有關網絡故障的信息和重新連接信息寫入錯誤日志。(默認為2)

四、二進制日志***

在mysql中二進制日志為binlog,它記錄了對數據庫執行更改的所有操作,但是不包括 select 和 show 這類操作,因為這類操作對數據本身並沒有修改,如果你還想記錄select和show操作,那只能使用查詢日志了,而不是二進制日志。(增刪改的SQL語句)

二進制還包括了執行數據庫更改操作的時間和執行時間等信息,它主要用於時間點恢復(備份恢復)以及主從復制結構

  • 恢復(recovery) : 某些數據的恢復需要二進制日志,如當一個數據庫全備文件恢復后,我們可以通過二進制的日志進行 point-in-time 的恢復
  • 復制(replication) : 通過復制和執行二進制日志使得一台遠程的 MySQL 數據庫(一般是slave 或者 standby) 與一台MySQL數據庫(一般為master或者primary) 進行實時同步
  • 審計(audit) :用戶可以通過二進制日志中的信息來進行審計,判斷是否有對數據庫進行注入攻擊

1、查看變量

MariaDB [mysql]> show global variables like '%bin%';
+-----------------------------------------+--------------------------------+
| Variable_name                           | Value                          |
+-----------------------------------------+--------------------------------+
| binlog_annotate_row_events              | ON                             |
| binlog_cache_size                       | 32768                          |
| binlog_checksum                         | CRC32                          |
| binlog_commit_wait_count                | 0                              |
| binlog_commit_wait_usec                 | 100000                         |
| binlog_direct_non_transactional_updates | OFF                            |
| binlog_format                           | MIXED                          |
| binlog_optimize_thread_scheduling       | ON                             |
| binlog_row_image                        | FULL                           |
| binlog_stmt_cache_size                  | 32768                          |
| encrypt_binlog                          | OFF                            |
| gtid_binlog_pos                         | 0-1-1                          |
| gtid_binlog_state                       | 0-1-1                          |
| innodb_locks_unsafe_for_binlog          | OFF                            |
| log_bin                                 | ON                             |
| log_bin_basename                        | /var/lib/mysql/mysql-bin       |
| log_bin_compress                        | OFF                            |
| log_bin_compress_min_len                | 256                            |
| log_bin_index                           | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators         | OFF                            |
| max_binlog_cache_size                   | 18446744073709547520           |
| max_binlog_size                         | 1073741824                     |
| max_binlog_stmt_cache_size              | 18446744073709547520           |
| read_binlog_speed_limit                 | 0                              |
| sql_log_bin                             | ON                             |
| sync_binlog                             | 0                              |
| wsrep_forced_binlog_format              | NONE                           |
+-----------------------------------------+--------------------------------+
27 rows in set (0.00 sec)

2、變量詳解

二進制日志文件的構成:

(1)日志文件:mysql-bin.xxxxxx,二進制格式

(2)索引文件:mysql-bin,index,索引文件(十進制文件)

log_bin = LOG_NAME:                        (只讀變量)只能通過修改配置文件來指定是否啟用二進制日志(全局的)
     #my.cnf配置文件中沒有log_bin的配置,表示未開啟二進制日志,如果存在log_bin的配置,則表示開啟了二進制日志,同時,二進制日志文件的名稱將會以log_bin對應的值為文件名前綴,文件默認位置在/var/lib/mysql/下,二進制日志文件的后綴名會進行自動編號,每次日志滾動后,后綴名編號自動加1. log_bin_basename = /var/lib/mysql/mysql-bin
: 指定二進制日志的文件的基名 log_bin_index = /var/lib/mysql/mysql-bin.index:指定二進制日志文件的索引文件 binlog_format = STATEMENT|ROW|MIXED:   指定基於哪種方式進行記錄
          STATEMENT:           基於“語句”記錄
          ROW:              基於“行”記錄
          MIXED:              讓系統自行判定該基於哪種方式記錄
sync_binlog = 1|0: 設定是否啟動二進制日志同步功能
-->每次提交事務,會將緩存中的內存刷新到二進制日志文件中。 -->默認每個sql語句是一個事務,而且默認事務會自動提交,所以,默認的性能很差
        -->此值為0時,表示當事務提交后,不會立即將內存中的binlog刷新到磁盤中;安全性最差,性能最高
        -->此值為1時,表示每一次事務提交后,都會立即將內存中的二進制文件同步到磁盤中;安全性最高,性能最差
        -->還能設置為N,當設置為3時,表示每3次事務提交后,將binlog從內存刷寫到磁盤一次,值設置的越大,有可能丟失的日志數據越多,但性能會越好
max_binlog_size = SIZE: 指定二進制日志文件的上限,超過上限會滾動,以字節為單位(默認為1G,為1073741824B) max_binlog_cache_size = SIZE: 指定二進制日志緩存空間大小,空間被填滿,會自動滾動 sql_log_off = on|off: 是否將一般的查詢操作記錄到二進制日志中 sql_log_bin = ON |OFF:  指定是否啟用二進制日志(會話級別) log_bin_trust_function_creators = on|off:  指定是否允許創建可能導致不安全的函數

3、查看二進制日志文件列表及事件

  SHOW {BINARY | MASTER} LOGS
  SHOW BINLOG EVENTS [IN 'log_name']
  show master status;

--修改配置文件
[
root@ren7 ~]# vim /etc/my.cnf.d/server.cnf ############################# [server] log_bin = mysql-binlog ##############################
--重啟服務
[root@ren7 ~]# systemctl restart mariadb [root@ren7 ~]# mysql -uroot -proot -D ren; --查看二進制日志文件列表
MariaDB
[ren]> show master logs; +---------------------+-----------+ | Log_name | File_size | +---------------------+-----------+ | mysql-binlog.000001 | 331 | +---------------------+-----------+ 1 row in set (0.00 sec) MariaDB [ren]> show binary logs; +---------------------+-----------+ | Log_name | File_size | +---------------------+-----------+ | mysql-binlog.000001 | 331 | +---------------------+-----------+ 1 row in set (0.00 sec) --查看當前正在使用的二進制日志文件
MariaDB
[ren]> show master status; +---------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------------+----------+--------------+------------------+ | mysql-binlog.000001 | 331 | | | +---------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
--修改數據庫的文件
MariaDB [ren]> drop table test4;
Query OK, 0 rows affected (0.00 sec)

MariaDB [ren]> delete from test where name='李連傑';
Query OK, 1 row affected (0.00 sec)

MariaDB [ren]> insert into test set name='漩渦鳴人';
Query OK, 1 row affected (0.01 sec)
--再次查看二進制日志文件
MariaDB [ren]> show master status;
+---------------------+----------+--------------+------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| mysql-binlog.000001 |      840 |              |                  |
+---------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
--查看二進制日志文件中的事件(查看binlog內容)
MariaDB [ren]> show binlog events; MariaDB [ren]> show binlog events in 'mysql-binlog.000001'; +---------------------+-----+-------------------+-----------+-------------+---------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +---------------------+-----+-------------------+-----------+-------------+---------------------------------------------------------+ | mysql-binlog.000001 | 4 | Format_desc | 1 | 256 | Server ver: 10.2.26-MariaDB-log, Binlog ver: 4 | | mysql-binlog.000001 | 256 | Gtid_list | 1 | 285 | [] | | mysql-binlog.000001 | 285 | Binlog_checkpoint | 1 | 331 | mysql-binlog.000001 | | mysql-binlog.000001 | 331 | Gtid | 1 | 373 | GTID 0-1-1 | | mysql-binlog.000001 | 373 | Query | 1 | 483 | use `ren`; DROP TABLE `test4` /* generated by server */ | | mysql-binlog.000001 | 483 | Gtid | 1 | 525 | BEGIN GTID 0-1-2 | | mysql-binlog.000001 | 525 | Query | 1 | 630 | use `ren`; delete from test where name='李連傑' | | mysql-binlog.000001 | 630 | Xid | 1 | 661 | COMMIT /* xid=22 */ | | mysql-binlog.000001 | 661 | Gtid | 1 | 703 | BEGIN GTID 0-1-3 | | mysql-binlog.000001 | 703 | Query | 1 | 809 | use `ren`; insert into test set name='漩渦鳴人' | | mysql-binlog.000001 | 809 | Xid | 1 | 840 | COMMIT /* xid=23 */ | +---------------------+-----+-------------------+-----------+-------------+---------------------------------------------------------+ 11 rows in set (0.00 sec) MariaDB [ren]> show binlog events in 'mysql-binlog.000001' from 256; +---------------------+-----+-------------------+-----------+-------------+---------------------------------------------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +---------------------+-----+-------------------+-----------+-------------+---------------------------------------------------------+ | mysql-binlog.000001 | 256 | Gtid_list | 1 | 285 | [] | | mysql-binlog.000001 | 285 | Binlog_checkpoint | 1 | 331 | mysql-binlog.000001 | | mysql-binlog.000001 | 331 | Gtid | 1 | 373 | GTID 0-1-1 | | mysql-binlog.000001 | 373 | Query | 1 | 483 | use `ren`; DROP TABLE `test4` /* generated by server */ | | mysql-binlog.000001 | 483 | Gtid | 1 | 525 | BEGIN GTID 0-1-2 | | mysql-binlog.000001 | 525 | Query | 1 | 630 | use `ren`; delete from test where name='李連傑' | | mysql-binlog.000001 | 630 | Xid | 1 | 661 | COMMIT /* xid=22 */ | | mysql-binlog.000001 | 661 | Gtid | 1 | 703 | BEGIN GTID 0-1-3 | | mysql-binlog.000001 | 703 | Query | 1 | 809 | use `ren`; insert into test set name='漩渦鳴人' | | mysql-binlog.000001 | 809 | Xid | 1 | 840 | COMMIT /* xid=23 */ | +---------------------+-----+-------------------+-----------+-------------+---------------------------------------------------------+ 10 rows in set (0.00 sec) MariaDB [ren]> show binlog events in 'mysql-binlog.000001' limit 1,2; +---------------------+-----+-------------------+-----------+-------------+---------------------+ | Log_name | Pos | Event_type | Server_id | End_log_pos | Info | +---------------------+-----+-------------------+-----------+-------------+---------------------+ | mysql-binlog.000001 | 256 | Gtid_list | 1 | 285 | [] | | mysql-binlog.000001 | 285 | Binlog_checkpoint | 1 | 331 | mysql-binlog.000001 | +---------------------+-----+-------------------+-----------+-------------+---------------------+ 2 rows in set (0.00 sec)

4、二進制日志滾動

(1)flush logs;

(2)文件超出指定大小;

(3)重啟數據庫(service mariadb restart / systemctl restart mariadb)

5、查看二進制日志文件(mysqlbinlog命令)

  除了前面提到的可以通過show binlog events命令在mysql中查看日志內容,還可以通過mysqlbinlog命令在文件系統下查看對應的二進制日志文件。

[root@ren7 mysql]# pwd
/var/lib/mysql
[root@ren7 mysql]# mysqlbinlog mysql-binlog.000001
/*!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
#190907 17:15:09 server id 1  end_log_pos 256 CRC32 0x16b33f7c     Start: binlog v 4, server v 10.2.26-MariaDB-log created 190907 17:15:09 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG '
HXVzXQ8BAAAA/AAAAAABAAABAAQAMTAuMi4yNi1NYXJpYURCLWxvZwAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAddXNdEzgNAAgAEgAEBAQEEgAA5AAEGggAAAAICAgCAAAACgoKAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAEEwQADQgICAoKCgF8P7MW
'/*!*/;
# at 256
#190907 17:15:09 server id 1  end_log_pos 285 CRC32 0x6a3abc7d     Gtid list []
# at 285
#190907 17:15:09 server id 1  end_log_pos 331 CRC32 0x01d5789f     Binlog checkpoint mysql-binlog.000001
# at 331
#190907 17:27:38 server id 1  end_log_pos 373 CRC32 0x17565195     GTID 0-1-1 ddl
/*!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*//*!*/;
# at 373
#190907 17:27:38 server id 1  end_log_pos 483 CRC32 0xc4f951a5     Query    thread_id=9    exec_time=0    error_code=0
use `ren`/*!*/;
SET TIMESTAMP=1567848458/*!*/;
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_checks=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=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
DROP TABLE `test4` /* generated by server */
/*!*/;
# at 483
#190907 17:28:11 server id 1  end_log_pos 525 CRC32 0x331814aa     GTID 0-1-2 trans
/*!100001 SET @@session.gtid_seq_no=2*//*!*/;
BEGIN
/*!*/;
# at 525
#190907 17:28:11 server id 1  end_log_pos 630 CRC32 0x2a7828ea     Query    thread_id=9    exec_time=0    error_code=0
SET TIMESTAMP=1567848491/*!*/;
delete from test where name='李連傑'
/*!*/;
# at 630
#190907 17:28:11 server id 1  end_log_pos 661 CRC32 0x13fd72a8     Xid = 22
COMMIT/*!*/;
# at 661
#190907 17:30:06 server id 1  end_log_pos 703 CRC32 0x4fd1715e     GTID 0-1-3 trans
/*!100001 SET @@session.gtid_seq_no=3*//*!*/;
BEGIN
/*!*/;
# at 703
#190907 17:30:06 server id 1  end_log_pos 809 CRC32 0xd387e70f     Query    thread_id=9    exec_time=0    error_code=0
SET TIMESTAMP=1567848606/*!*/;
insert into test set name='漩渦鳴人'
/*!*/;
# at 809
#190907 17:30:06 server id 1  end_log_pos 840 CRC32 0x62252207     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*/;
二進制日志格式:
    #190613 14:17:32 server id 1  end_log_pos 666 CRC32 0xeb1cde6b  Query   thread_id=9     exec_time=
    0       error_code=0
    use `testdb`/*!*/;
    事件發生的日期和時間:190613 14:17:32
    事件發生的服務器標識:server id 1
    事件的結束位置:end_log_pos 666
    事件的類型:Query
    事件發生時所在服務器執行此事件的線程ID:thread_id=9
    語句的時間戳與將其寫入二進制文件中的時間差:exec_time=0
    錯誤代碼:error_code=0
    事件內容:
    GTID:Global Transaction ID;
        專屬屬性:GTID
mysqlbinlog:客戶端命令工具
    mysqlbinlog [options] log_file ...
    --start-datetime=
    --stop-datetime=    
    --start-position=
    --stop-position=

五、中繼日志

  復制架構中,備服務器用於保存主服務器的二進制日志中讀取到的事件;用於實現mysql的主從復制。

1、查看中繼日志變量

MariaDB [ren]> show global variables like "%relay%";
+-----------------------+----------------+
| Variable_name         | Value          |
+-----------------------+----------------+
| max_relay_log_size    | 1073741824     |
| relay_log             |                |
| relay_log_basename    |                |
| relay_log_index       |                |
| relay_log_info_file   | relay-log.info |
| relay_log_purge       | ON             |
| relay_log_recovery    | OFF            |
| relay_log_space_limit | 0              |
| sync_relay_log        | 10000          |
| sync_relay_log_info   | 10000          |
+-----------------------+----------------+
10 rows in set (0.00 sec)

2、變量詳解

1 relay_log fileName:       指定中繼日志的文件名。【文件名為空,表示禁用了中繼日志】
2 relay_log_index:          索引表
3 relay_log_info_file:      記錄中繼日志文件的相關信息
4 relay_log_purge:          指定是否自動刪除無用的中繼日志文件
5 relay_log_recovery:       是否可以對中繼日志做自動恢復相關的配置
6 relay_log_space_limit:    指定中繼日志可以占用的空間大小(0表示不限制)

3、SQL線程應用中繼日志流程

六、事務日志

事務日志:transaction log(ib_logfile0,ib_logfile1)

1、查看參數

MariaDB [ren]> show global variables like '%innodb%log%';
+-------------------------------------------+------------+
| Variable_name                             | Value      |
+-------------------------------------------+------------+
| innodb_encrypt_log                        | OFF        |
| innodb_flush_log_at_timeout               | 1          |
| innodb_flush_log_at_trx_commit            | 1          |
| innodb_locks_unsafe_for_binlog            | OFF        |
| 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   |
| innodb_log_files_in_group                 | 2          |
| innodb_log_group_home_dir                 | ./         |
| innodb_log_optimize_ddl                   | ON         |
| innodb_log_write_ahead_size               | 8192       |
| innodb_max_undo_log_size                  | 10485760   |
| innodb_mirrored_log_groups                | 0          |
| innodb_online_alter_log_max_size          | 134217728  |
| innodb_scrub_log                          | OFF        |
| innodb_scrub_log_speed                    | 256        |
| innodb_track_redo_log_now                 | OFF        |
| innodb_undo_log_truncate                  | OFF        |
| innodb_undo_logs                          | 128        |
| innodb_use_global_flush_log_at_trx_commit | OFF        |
+-------------------------------------------+------------+
26 rows in set (0.00 sec)

2、部分參數詳解

innodb_buffer_pool_size                    一般設置成為物理內存的3/4,或者4/5
innodb_log_files_in_group = 2              事務日志文件的個數,默認為2個事務日志文件
innodb_log_file_size = 50331648(48m)      事務日志文件的單個大小48m
innodb_log_group_home_dir = ./             事務日志文件的所在路徑,默認就在mariadb的數據目錄/var/lib/mysql
事務型存儲引擎自行管理和使用(Innodb,myisam引擎是不支持事務,外鍵,行級鎖)
    redo log : 重做日志
    undo log :撤銷日志
buffer_pool:緩沖池(一般而言,裝完數據庫第一個要調的參數)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM