同大多數關系型數據庫一樣,日志文件是MySQL數據庫的重要組成部分。MySQL有幾種不同的日志文件,通常包括錯誤日志文件,二進制日志,通用日志,慢查詢日志,等等。這些日志可以幫助我們定位mysqld內部發(fā)生的事件,數據庫性能故障,記錄數據的變更歷史,用戶恢復數據庫等等。本文主要描述通用查詢日志。
1、MySQL日志文件系統(tǒng)的組成
a、錯誤日志:記錄啟動、運行或停止mysqld時出現的問題。
b、通用日志:記錄建立的客戶端連接和執(zhí)行的語句。
c、更新日志:記錄更改數據的語句。該日志在MySQL 5.1中已不再使用。
d、二進制日志:記錄所有更改數據的語句。還用于復制。
e、慢查詢日志:記錄所有執(zhí)行時間超過long_query_time秒的所有查詢或不使用索引的查詢。
f、Innodb日志:innodb redo log
缺省情況下,所有日志創(chuàng)建于mysqld數據目錄中。
可以通過刷新日志,來強制mysqld來關閉和重新打開日志文件(或者在某些情況下切換到一個新的日志)。
當你執(zhí)行一個FLUSH LOGS語句或執(zhí)行mysqladmin flush-logs或mysqladmin refresh時,則日志被老化。
對于存在MySQL復制的情形下,從復制服務器將維護更多日志文件,被稱為接替日志。
2、通用查詢日志
通用查詢日志可以存放到一個文本文件或者表中,所有連接和語句被記錄到該日志文件或表,缺省未開啟該日志。
通過--log[=file_name]或-l [file_name]選項啟動它。如果沒有給定file_name的值, 默認名是host_name.log。
mysqld按照它接收的順序記錄語句到查詢日志。這可能與執(zhí)行的順序不同。
不同于更新日志和二進制日志,它們在查詢執(zhí)行后,但是任何一個鎖釋放之前記錄日志。
查詢日志包含所有語句,而二進制日志不包含只查詢數據的語句。
服務器重新啟動和日志刷新不會產生新的一般查詢日志文件。
3、通用查詢日志的系統(tǒng)變量
log_output=[none|file|table|file,table] #通用查詢日志輸出格式
general_log=[on|off] #是否啟用通用查詢日志
general_log_file[=filename] #通用查詢日志位置及名字
4、通用查詢日志的備份
在Linux或Unix中,你可以通過下面的命令重新命名文件
并創(chuàng)建一個新文件:
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中,服務器打開日志文件期間不能重新命名日志文件。必須先停止服務器然后重新命名日志文件。然后重啟服務器來創(chuàng)建新日志文件。
5、演示通用查詢日志的使用
[sql]view
plaincopy
print?a、啟用通用查詢日志
--演示環(huán)境
root@localhost[(none)]>showvariableslike'%version%';
+-------------------------+------------------------------+
|Variable_name|Value|
+-------------------------+------------------------------+
|innodb_version|5.5.39|
|protocol_version|10|
|slave_type_conversions||
|version|5.5.39-log|
|version_comment|MySQLCommunityServer(GPL)|
|version_compile_machine|x86_64|
|version_compile_os|Linux|
+-------------------------+------------------------------+
--查看系統(tǒng)變量
root@localhost[(none)]>showvariableslike'%general%';
+------------------+----------------------------+
|Variable_name|Value|
+------------------+----------------------------+
|general_log|OFF|
|general_log_file|/var/lib/mysql/suse11b.log|
+------------------+----------------------------+
--查看當前的通用日志,顯示無日志文件
root@localhost[(none)]>systemls/var/lib/mysql/suse11b.log
ls:cannotaccess/var/lib/mysql/suse11b.log:Nosuchfileordirectory
--設置變量general_log以開啟通用查詢日志
root@localhost[(none)]>set@@global.general_log=1;
QueryOK,0rowsaffected(0.00sec)
--再次查看通用日志文件已存在
root@localhost[(none)]>systemls/var/lib/mysql/suse11b.log
/var/lib/mysql/suse11b.log
root@localhost[(none)]>select*fromtempdb.tb1;--執(zhí)行查詢
+------+------+
|id|val|
+------+------+
|1|jack|
+------+------+
--查看通用日志文件內容
root@localhost[(none)]>systemmore/var/lib/mysql/suse11b.log
/usr/sbin/mysqld,Version:5.5.39-log(MySQLCommunityServer(GPL)).startedwith:
Tcpport:3306Unixsocket:/var/lib/mysql/mysql.sock
TimeIdCommandArgument
14100316:18:124Queryshowvariableslike'%general%'
14100316:18:554Queryselect*fromtempdb.tb1
b、更改通用查詢日志位置
root@localhost[(none)]>exit
Bye
suse11b:~#servicemysqlstop
ShuttingdownMySQL...done
suse11b:~#mysqld--general_log_file=/tmp/suse11b.log--user=mysql&
[1]47009
suse11b:~#ps-ef|grepmysql|grep-vgrep
mysql4700944514116:22pts/000:00:00mysqld--general_log_file=/tmp/suse11b.log--user=mysql
root4705344514016:22pts/000:00:00grepmysql
suse11b:~#mysql
root@localhost[(none)]>systemls/tmp/suse11b.log
ls:cannotaccess/tmp/suse11b.log:Nosuchfileordirectory
root@localhost[(none)]>showvariableslike'%gener%';
+------------------+------------------+
|Variable_name|Value|
+------------------+------------------+
|general_log|OFF|
|general_log_file|/tmp/suse11b.log|
+------------------+------------------+
root@localhost[(none)]>setglobalgeneral_log=on;
QueryOK,0rowsaffected(0.01sec)
--此時從系統(tǒng)變量看出,通用日志已經到/tmp目錄下
root@localhost[(none)]>showvariableslike'%gener%';
+------------------+------------------+
|Variable_name|Value|
+------------------+------------------+
|general_log|ON|
|general_log_file|/tmp/suse11b.log|
+------------------+------------------+
--發(fā)布查詢
root@localhost[(none)]>selectcount(*)fromtempdb.tb1;
+----------+
|count(*)|
+----------+
|1|
+----------+
--查看通用日志文件內容
root@localhost[(none)]>systemmore/tmp/suse11b.log
mysqld,Version:5.5.39-log(MySQLCommunityServer(GPL)).startedwith:
Tcpport:3306Unixsocket:/var/lib/mysql/mysql.sock
TimeIdCommandArgument
14100316:30:031Queryshowvariableslike'%gener%'
14100316:30:091Queryselectcount(*)fromtempdb.tb1
c、通用查詢日志輸出方式
--可以輸出為文件,表以及不輸出,即TABLE,FILE,NONE
--系統(tǒng)變量log_output
root@localhost[(none)]>showvariableslike'log_output';
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|log_output|FILE|
+---------------+-------+
--下面修改為輸出為表方式
root@localhost[(none)]>setgloballog_output='TABLE';
QueryOK,0rowsaffected(0.00sec)
root@localhost[(none)]>showvariableslike'log_output';
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|log_output|TABLE|
+---------------+-------+
--發(fā)布查詢
root@localhost[(none)]>select*fromtempdb.tb1;
+------+------+
|id|val|
+------+------+
|1|jack|
+------+------+
--Author:Leshami
--Blog:http://blog.csdn.net/leshami
root@localhost[(none)]>systemmore/tmp/suse11b.log
mysqld,Version:5.5.39-log(MySQLCommunityServer(GPL)).startedwith:
Tcpport:3306Unixsocket:/var/lib/mysql/mysql.sock
TimeIdCommandArgument
14100316:30:031Queryshowvariableslike'%gener%'
14100316:30:091Queryselectcount(*)fromtempdb.tb1
14100316:31:001Queryshowvariableslike'log_output'
14100317:00:481Querysetgloballog_output='TABLE'#通用查詢日志輸出到文件僅僅記錄到全局變量的修改
--mysql.general_log記錄了通用查詢日志的信息
root@localhost[(none)]>descmysql.general_log;
+--------------+------------------+------+-----+-------------------+-----------------------------+
|Field|Type|Null|Key|Default|Extra|
+--------------+------------------+------+-----+-------------------+-----------------------------+
|event_time|timestamp|NO||CURRENT_TIMESTAMP|onupdateCURRENT_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)]>selectthread_id,command_type,argumentfrommysql.general_log;
+-----------+--------------+---------------------------------------------------------------+
|thread_id|command_type|argument|
+-----------+--------------+---------------------------------------------------------------+
|1|Query|showvariableslike'log_output'|
|1|Query|select*fromtempdb.tb1|
|1|Query|descmysql.general_log|
|1|Query|selectthread_id,command_type,argumentfrommysql.general_log|
+-----------+--------------+---------------------------------------------------------------+
root@localhost[(none)]>showvariableslike'log_output';
+---------------+-------+
|Variable_name|Value|
+---------------+-------+
|log_output|TABLE|
+---------------+-------+
--使用FILE,TABLE2者混合輸出通用日志
root@localhost[(none)]>setgloballog_output='file,table';
QueryOK,0rowsaffected(0.00sec)
root@localhost[(none)]>select@@global.log_output;
+---------------------+
|@@global.log_output|
+---------------------+
|FILE,TABLE|
+---------------------+
root@localhost[(none)]>insertintotempdb.tb1values(2,'robinson');
QueryOK,1rowaffected(0.06sec)
root@localhost[(none)]>commit;
QueryOK,0rowsaffected(0.01sec)
--驗證結果,表和文件里邊存在通用的日志記錄
root@localhost[(none)]>systemtail/tmp/suse11b.log|greprobinson
14100317:41:542Queryinsertintotempdb.tb1values(2,'robinson')
root@localhost[(none)]>selectthread_id,command_type,argumentfrommysql.general_log
->whereargumentlike'%robinson%';
+-----------+--------------+------------------------------------------------------------------------+
|thread_id|command_type|argument|
+-----------+--------------+------------------------------------------------------------------------+
|2|Query|insertintotempdb.tb1values(2,'robinson')|
|2|Query|selectthread_id,command_type,argumentfrommysql.general_log|
|||whereargumentlike''robinson''|
+-----------+--------------+------------------------------------------------------------------------+
d、關閉通用查詢日志
--可以通過設置系統(tǒng)變量general_log來關閉通用查詢日志,此時日志輸出設置為FILE,TABLE
root@localhost[(none)]>showvariableslike'log_output';
+---------------+------------+
|Variable_name|Value|
+---------------+------------+
|log_output|FILE,TABLE|
+---------------+------------+
root@localhost[(none)]>setglobalgeneral_log=off;
QueryOK,0rowsaffected(0.01sec)
root@localhost[(none)]>showvariableslike'%gener%';
+------------------+------------------+
|Variable_name|Value|
+------------------+------------------+
|general_log|OFF|
|general_log_file|/tmp/suse11b.log|
+------------------+------------------+
root@localhost[(none)]>deletefromtempdb.tb1whereid=2;
QueryOK,1rowaffected(0.12sec)
root@localhost[(none)]>commit;
QueryOK,0rowsaffected(0.00sec)
root@localhost[(none)]>systemtail-n1/tmp/suse11b.log
14100317:45:132Querysetglobalgeneral_log=off
root@localhost[(none)]>selectthread_id,command_type,argumentfrommysql.general_log
->whereargumentlike'%delete%';
Emptyset(0.00sec)
--從上面的演示可知,盡管我們設置了log_output為FILE,TABLE,但general_log為OFF,通用日志無任何記錄產生
root@localhost[(none)]>setgloballog_output=none;
QueryOK,0rowsaffected(0.00sec)
root@localhost[(none)]>setglobalgeneral_log=1;
QueryOK,0rowsaffected(0.00sec)
root@localhost[(none)]>truncatetabletempdb.tb1;
QueryOK,0rowsaffected(0.01sec)
root@localhost[(none)]>systemtail-n1/tmp/suse11b.log
TimeIdCommandArgument
--通過上面的演示,在log_output=none,general_log=on的清下下無任何通用日志輸出。