與大多數關係型數據庫,日誌文件是MySQL數據庫的一個重要組成部分。MySQL有幾種不一樣的日誌文件,一般包括錯誤日誌文件,二進制日誌,通用日誌。慢查詢日誌,等等。mysql
這些日誌可以幫助咱們定位mysqld內部發生的事件,數據庫性能故障。記錄數據的變動歷史,用戶恢復數據庫等等。web
本文主要描寫敘述通用查詢日誌。sql
一、MySQL日誌文件系統的組成
a、錯誤日誌:記錄啓動、運行或中止mysqld時出現的問題。
b、通用日誌:記錄創建的client鏈接和運行的語句。shell
c、更新日誌:記錄更改數據的語句。數據庫
該日誌在MySQL 5.1中已再也不使用。socket
d、二進制日誌:記錄所有更改數據的語句。還用於複製。
e、慢查詢日誌:記錄所有運行時間超過long_query_time秒的所有查詢或不使用索引的查詢。
f、Innodb日誌:innodb redo log
缺省狀況下,所有日誌建立於mysqld數據文件夾中。post
可以經過刷新日誌。來強制mysqld來關閉和又一次打開日誌文件(或者在某些狀況下切換到一個新的日誌)。性能
當你運行一個FLUSH LOGS語句或運行mysqladmin flush-logs或mysqladmin refresh時,則日誌被老化。
對於存在MySQL複製的情形下,從複製server將維護不少其它日誌文件,被稱爲接替日誌。spa
二、通用查詢日誌
通用查詢日誌可以存放到一個文本文件或者表中。所有鏈接和語句被記錄到該日誌文件或表,缺省未開啓該日誌。.net
經過--log[=file_name]或-l [file_name]選項啓動它。假設沒有給定file_name的值, 默認名是host_name.log。
mysqld依照它接收的順序記錄語句到查詢日誌。這可能與運行的順序不一樣。
不一樣於更新日誌和二進制日誌,它們在查詢運行後。但是不論什麼一個鎖釋放以前記錄日誌。
查詢日誌包括所有語句,而二進制日誌不包括僅僅查詢數據的語句。
server又一次啓動和日誌刷新不會產生新的通常查詢日誌文件。
三、通用查詢日誌的系統變量
log_output=[none|file|table|file,table] #通用查詢日誌輸出格式
general_log=[on|off] #是否啓用通用查詢日誌
general_log_file[=filename] #通用查詢日誌位置及名字
四、通用查詢日誌的備份
在Linux或Unix中。你可以經過如下的命令又一次命名文件
並建立一個新文件:
shell> mv hostname.log hostname-old.log
shell> mysqladmin flush-logs
shell> cp hostname-old.log to-backup-directory
shell> rm hostname-old.log
在Windows中,server打開日誌文件期間不能又一次命名日誌文件。必須先中止server而後又一次命名日誌文件。而後從新啓動server來建立新日誌文件。
五、演示通用查詢日誌的使用
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 --經過上面的演示,在log_output=none,general_log=on的清下下無不論什麼通用日誌輸出。