日誌是日記中的一種,多指非個人的,一般是記載每天所做的工作。在計算機科學中,日誌是指伺服器等電腦設備或軟體的運作記錄(Server log)。在電腦設備和軟體出現問題時,日誌是我們在排查問題的一個重要依據。查詢日誌是用戶記錄從客戶端收到所有資料庫或作業系統的查詢,同時也包括了每一個客戶端的連結和斷開連結。
基本介紹
- 中文名:查詢日誌
- 外文名:Query Log
- 學科:計算機科學
- 定義:用戶所有查詢的記錄
- 套用:資料庫、計算機系統
- 作用:便於問題分析、查詢
- 有關術語:日誌
概述
日誌
日誌的作用
日誌記錄類型
日誌的管理
慢查詢日誌
mysql> use mysqlmysql> selectuser,host,password from user where user="root";+------+------------------+----------+| user | host | password |+------+------------------+----------+| root |localhost | || root |stu18.magedu.com | || root |127.0.0.1 | || root | ::1 | |+------+------------------+----------+4 rows in set (0.08sec) #查詢時間為0.08mysql> systemmore /mydata/data/stu18_slow.log #查詢慢查詢日誌記錄信息/usr/local/mysql/bin/mysqld,Version: 5.5.33-log (Source distribution). startedwith:Tcp port: 3306 Unix socket: /tmp/mysql.sockTime Id Command Argument>>>>>>>>>>>>>>>>部分已省略>>>>>>>>>>>>>># Time: 13100723:46:33# User@Host:root[root] @ localhost []# Query_time:0.108459 Lock_time: 0.000216 Rows_sent:4 Rows_examined: 6SETtimestamp=1381160793;selectuser,host,password from user where user="root";
通用查詢日誌
a、啟用通用查詢日誌 root@localhost[(none)]> show variables like '%version%'; +-------------------------+------------------------------+ | Variable_name | Value | +-------------------------+------------------------------+ | innodb_version | 5.5.39 | | protocol_version | 10 | | slave_type_conversions | | | version | 5.5.39-log | | version_comment | MySQL Community Server (GPL) | | version_compile_machine | x86_64 | | version_compile_os | Linux | +-------------------------+------------------------------+ --查看系統變數 root@localhost[(none)]> show variables like '%general%'; +------------------+----------------------------+ | Variable_name | Value | +------------------+----------------------------+ | general_log | OFF | | general_log_file | /var/lib/mysql/suse11b.log | +------------------+----------------------------+ --查看當前的通用日誌,顯示無日誌檔案 root@localhost[(none)]> system ls /var/lib/mysql/suse11b.log ls: cannot access /var/lib/mysql/suse11b.log: No such file or directory --設定變數general_log以開啟通用查詢日誌 root@localhost[(none)]> set @@global.general_log=1; Query OK, 0 rows affected (0.00 sec) --再次查看通用日誌檔案已存在 root@localhost[(none)]> system ls /var/lib/mysql/suse11b.log /var/lib/mysql/suse11b.log root@localhost[(none)]> select * from tempdb.tb1; --執行查詢 +------+------+ | id | val | +------+------+ | 1 | jack | +------+------+ --查看通用日誌檔案內容 root@localhost[(none)]> system more /var/lib/mysql/suse11b.log /usr/sbin/mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with: Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock Time Id Command Argument 141003 16:18:12 4 Query show variables like '%general%' 141003 16:18:55 4 Query select * from tempdb.tb1 b、更改通用查詢日誌位置 root@localhost[(none)]> exit Bye suse11b:~ # service mysql stop Shutting down MySQL... done suse11b:~ # mysqld --general_log_file=/tmp/suse11b.log --user=mysql & [1] 47009 suse11b:~ # ps -ef|grep mysql|grep -v grep mysql 47009 44514 1 16:22 pts/0 00:00:00 mysqld --general_log_file=/tmp/suse11b.log --user=mysql root 47053 44514 0 16:22 pts/0 00:00:00 grep mysql suse11b:~ # mysql root@localhost[(none)]> system ls /tmp/suse11b.log ls: cannot access /tmp/suse11b.log: No such file or directory root@localhost[(none)]> show variables like '%gener%'; +------------------+------------------+ | Variable_name | Value | +------------------+------------------+ | general_log | OFF | | general_log_file | /tmp/suse11b.log | +------------------+------------------+ root@localhost[(none)]> set global general_log=on; Query OK, 0 rows affected (0.01 sec) --此時從系統變數看出,通用日誌已經到/tmp目錄下 root@localhost[(none)]> show variables like '%gener%'; +------------------+------------------+ | Variable_name | Value | +------------------+------------------+ | general_log | ON | | general_log_file | /tmp/suse11b.log | +------------------+------------------+ --發布查詢 root@localhost[(none)]> select count(*) from tempdb.tb1; +----------+ | count(*) | +----------+ | 1 | +----------+ --查看通用日誌檔案內容 root@localhost[(none)]> system more /tmp/suse11b.log mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with: Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock Time Id Command Argument 141003 16:30:03 1 Query show variables like '%gener%' 141003 16:30:09 1 Query select count(*) from tempdb.tb1 c、通用查詢日誌輸出方式 --可以輸出為檔案,表以及不輸出,即TABLE,FILE,NONE --系統變數log_output root@localhost[(none)]> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | FILE | +---------------+-------+ --下面修改為輸出為表方式 root@localhost[(none)]> set global log_output='TABLE'; Query OK, 0 rows affected (0.00 sec) root@localhost[(none)]> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | TABLE | +---------------+-------+ --發布查詢 root@localhost[(none)]> select * from tempdb.tb1; +------+------+ | id | val | +------+------+ | 1 | jack | +------+------+ --Author: Leshami --Blog : http://blog.csdn.net/leshami root@localhost[(none)]> system more /tmp/suse11b.log mysqld, Version: 5.5.39-log (MySQL Community Server (GPL)). started with: Tcp port: 3306 Unix socket: /var/lib/mysql/mysql.sock Time Id Command Argument 141003 16:30:03 1 Query show variables like '%gener%' 141003 16:30:09 1 Query select count(*) from tempdb.tb1 141003 16:31:00 1 Query show variables like 'log_output' 141003 17:00:48 1 Query set global log_output='TABLE' #通用查詢日誌輸出到檔案僅僅記錄到全局變數的修改 --mysql.general_log記錄了通用查詢日誌的信息 root@localhost[(none)]> desc mysql.general_log; +--------------+------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +--------------+------------------+------+-----+-------------------+-----------------------------+ | event_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | | user_host | mediumtext | NO | | NULL | | | thread_id | int(11) | NO | | NULL | | | server_id | int(10) unsigned | NO | | NULL | | | command_type | varchar(64) | NO | | NULL | | | argument | mediumtext | NO | | NULL | | +--------------+------------------+------+-----+-------------------+-----------------------------+ --從通用查詢日誌表里查看通用查詢日誌的內容 root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log; +-----------+--------------+---------------------------------------------------------------+ | thread_id | command_type | argument | +-----------+--------------+---------------------------------------------------------------+ | 1 | Query | show variables like 'log_output' | | 1 | Query | select * from tempdb.tb1 | | 1 | Query | desc mysql.general_log | | 1 | Query | select thread_id,command_type,argument from mysql.general_log | +-----------+--------------+---------------------------------------------------------------+ root@localhost[(none)]> show variables like 'log_output'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | log_output | TABLE | +---------------+-------+ --使用FILE,TABLE 2者混合輸出通用日誌 root@localhost[(none)]> set global log_output='file,table'; Query OK, 0 rows affected (0.00 sec) root@localhost[(none)]> select @@global.log_output; +---------------------+ | @@global.log_output | +---------------------+ | FILE,TABLE | +---------------------+ root@localhost[(none)]> insert into tempdb.tb1 values(2,'robinson'); Query OK, 1 row affected (0.06 sec) root@localhost[(none)]> commit; Query OK, 0 rows affected (0.01 sec) --驗證結果,表和檔案裡邊存在通用的日誌記錄 root@localhost[(none)]> system tail /tmp/suse11b.log|grep robinson 141003 17:41:54 2 Query insert into tempdb.tb1 values(2,'robinson') root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log -> where argument like '%robinson%'; +-----------+--------------+------------------------------------------------------------------------+ | thread_id | command_type | argument | +-----------+--------------+------------------------------------------------------------------------+ | 2 | Query | insert into tempdb.tb1 values(2,'robinson') | | 2 | Query | select thread_id,command_type,argument from mysql.general_log | | | | where argument like ''robinson'' | +-----------+--------------+------------------------------------------------------------------------+ d、關閉通用查詢日誌 --可以通過設定系統變數general_log來關閉通用查詢日誌,此時日誌輸出設定為FILE,TABLE root@localhost[(none)]> show variables like 'log_output'; +---------------+------------+ | Variable_name | Value | +---------------+------------+ | log_output | FILE,TABLE | +---------------+------------+ root@localhost[(none)]> set global general_log=off; Query OK, 0 rows affected (0.01 sec) root@localhost[(none)]> show variables like '%gener%'; +------------------+------------------+ | Variable_name | Value | +------------------+------------------+ | general_log | OFF | | general_log_file | /tmp/suse11b.log | +------------------+------------------+ root@localhost[(none)]> delete from tempdb.tb1 where id=2; Query OK, 1 row affected (0.12 sec) root@localhost[(none)]> commit; Query OK, 0 rows affected (0.00 sec) root@localhost[(none)]> system tail -n 1 /tmp/suse11b.log 141003 17:45:13 2 Query set global general_log=off root@localhost[(none)]> select thread_id,command_type,argument from mysql.general_log -> where argument like '%delete%'; Empty set (0.00 sec) --從上面的演示可知,儘管我們設定了log_output為FILE,TABLE,但general_log為OFF,通用日誌無任何記錄產生 root@localhost[(none)]> set global log_output=none; Query OK, 0 rows affected (0.00 sec) root@localhost[(none)]> set global general_log=1; Query OK, 0 rows affected (0.00 sec) root@localhost[(none)]> truncate table tempdb.tb1; Query OK, 0 rows affected (0.01 sec) root@localhost[(none)]> system tail -n 1 /tmp/suse11b.log Time Id Command Argument