1. 程式人生 > >數據庫日誌系統分解

數據庫日誌系統分解

mysql 日誌

標題索引


  • 日誌分類

  • 日誌操作

  • 日誌分解


日誌分類

在數據庫系統中日誌主要分為6類,具體如下:

1、查詢日誌:主要記錄日常查詢的記錄;

2、慢查詢日誌:主要記錄查詢時長超過設置的數值時間的語句,方便DBA運維人員進行查詢;

3、錯誤日誌:主要用於記錄錯誤的事件,方便DBA運維人員對數據庫的操作和日常維護;

4、二進制日誌:主要用於記錄sql語句對數據庫的操作,方便DBA運維人員審計和備份恢復;

5、中繼日誌:主要用於數據庫集群架構設計中數據庫的主備;

6、事物日誌:主要用於防止數據庫服務器異常中斷時,事物的恢復。

日誌操作

1.查詢日誌,查詢日誌可以記錄在文件系統的文件中,也可以記錄在自身數據庫的表中,具體的記錄方式可通過全部變量進行查看或更改,默認只記錄在文件之中,建議關閉查詢日誌


MariaDB [(none)]>  show global variables like 'general_log%';
+------------------+----------------+
| Variable_name    | Value          |
+------------------+----------------+
| general_log      | ON             |
| general_log_file | nginxser02.log |   #相對目錄,相對數據文件安裝的目錄
+------------------+----------------+
2 rows in set (0.00 sec)
MariaDB [(none)]>  show global variables like 'log_output';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_output    | FILE  |               #值可以為file|table|none
+---------------+-------+
1 row in set (0.00 sec)

2.慢查詢日誌,執行時間超出指定查詢的日誌,慢查詢有可能數據庫設計的不合理或表被鎖定非設計原因,具體驗證或修改可根據如下示例

#查詢慢查詢設定時長,想修改時長並永久生效,寫配置文件
MariaDB [(none)]> show global variables like 'long_query_time'; 
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]> show global variables like 'slow_query%';
+---------------------+---------------------+
| Variable_name       | Value               |
+---------------------+---------------------+
| slow_query_log      | OFF                 |
| slow_query_log_file | nginxser02-slow.log |
+---------------------+---------------------+
2 rows in set (0.00 sec)
#慢查詢日誌記錄的策略設定
MariaDB [(none)]> show global variables like 'log_slow%';
+---------------------+--------------------------------------------------------------------------------------------------------------+
| Variable_name       | Value                                                                                                        |
+---------------------+--------------------------------------------------------------------------------------------------------------+
| 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_queries    | OFF                                                                                                          |
| log_slow_rate_limit | 1                                                                                                            |
| log_slow_verbosity  |                                                                                                              |
+---------------------+--------------------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)

3.錯誤日誌,錯誤日誌記錄所有數據庫生命周期中所有的錯誤信息,具體如下操作

MariaDB [(none)]> show global variables like 'log_error%';
+---------------+------------------------------+
| Variable_name | Value                        |
+---------------+------------------------------+
| log_error     | /var/log/mariadb/mariadb.log |
+---------------+------------------------------+
1 row in set (0.00 sec)
#設置錯誤警告信息,1表示記錄,0表示不記錄
MariaDB [(none)]> show global variables like 'log_war%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_warnings  | 1     |
+---------------+-------+
1 row in set (0.00 sec)

4.二進制日誌,文件也為二進制文件,可利用數據庫的恢復等,默認情況下是關閉的,且只能在配置文件中進行修改

MariaDB [(none)]> show global variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

修改配置文件驗證

[ root@centos7 ~ ]#vim /etc/my.cnf
[mysqld]z
  log-bin=mysql-bin
  binlog_format=mixed

再次登陸數據庫進行驗證

MariaDB [(none)]> show global variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> help show ;
Name: 'SHOW'
Description:
SHOW has many forms that provide information about databases, tables,
columns, or status information about the server. This section describes
those following:

SHOW AUTHORS
SHOW {BINARY | MASTER} LOGS
SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset,] row_count]
MariaDB [(none)]> show binary logs;  #查看正在使用中的二進制日誌
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |       264 |
| mysql-bin.000002 |       245 |
+------------------+-----------+
2 rows in set (0.00 sec)
MariaDB [(none)]> show binlog events in 'mysql-bin.000002';
+------------------+-----+-------------+-----------+-------------+-------------------------------------------+
| Log_name         | Pos | Event_type  | Server_id | End_log_pos | Info                                      |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------+
| mysql-bin.000002 |   4 | Format_desc |         1 |         245 | Server ver: 5.5.52-MariaDB, Binlog ver: 4 |
+------------------+-----+-------------+-----------+-------------+-------------------------------------------+
1 row in set (0.00 sec)









數據庫日誌系統分解