同大多數關系型數據庫一樣,日志文件是MySQL數據庫的重要組成部分。MySQL有幾種不同的日志文件,通常包括錯誤日志文件,二進制日志,通用日志,慢查詢日志,等等。這些日志可以幫助我們定位mysqld內部發生的事件,數據庫性能故障,記錄數據的變更歷史,用戶恢復數據庫等等。本文主要描述錯誤日志文件。
1、MySQL日志文件系統的組成
a、錯誤日志:記錄啟動、運行或停止mysqld時出現的問題。
b、通用日志:記錄建立的客戶端連接和執行的語句。
c、更新日志:記錄更改數據的語句。該日志在MySQL 5.1中已不再使用。
d、二進制日志:記錄所有更改數據的語句。還用於復制。
e、慢查詢日志:記錄所有執行時間超過long_query_time秒的所有查詢或不使用索引的查詢。
f、Innodb日志:innodb redo log
缺省情況下,所有日志創建於mysqld數據目錄中。
可以通過刷新日志,來強制mysqld來關閉和重新打開日志文件(或者在某些情況下切換到一個新的日志)。
當你執行一個FLUSH LOGS語句或執行mysqladmin flush-logs或mysqladmin refresh時,則日志被老化。
對於存在MySQL復制的情形下,從復制服務器將維護更多日志文件,被稱為接替日志。
2、錯誤日志
錯誤日志是一個文本文件。
錯誤日志記錄了MySQL Server每次啟動和關閉的詳細信息以及運行過程中所有較為嚴重的警告和錯誤信息。
可以用--log-error[=file_name]選項來開啟mysql錯誤日志,該選項指定mysqld保存錯誤日志文件的位置。
對於指定--log-error[=file_name]選項而未給定file_name值,mysqld使用錯誤日志名host_name.err 並在數據目錄中寫入日志文件。
在mysqld正在寫入錯誤日志到文件時,執行FLUSH LOGS 或者mysqladmin flush-logs時,服務器將關閉並重新打開日志文件。
建議在flush之前手動重命名錯誤日志文件,之后mysql服務將使用原始文件名打開一個新文件。
以下為錯誤日志備份方法:
shell> mv host_name.err host_name.err-old
shell> mysqladmin flush-logs
shell> mv host_name.err-old backup-directory
3、實戰演示
#啟用錯誤日志,缺省情況下文件名為: hostname.err
#下面2種方式均可進行錯誤日志的配置
--log-error=file_name #命令行選項(command option)
log-error=file_Name #配置文件(configure file)
#查看當前的錯誤日志配置,缺省情況下位於數據目錄
mysql> show variables like 'log_error';
+---------------+-------------------------+
| Variable_name | Value |
+---------------+-------------------------+
| log_error | /var/lib/mysql/SZDB.err |
+---------------+-------------------------+
1 row in set (0.00 sec)
#查看當前mysql server錯誤日志文件
SZDB:/var/lib/mysql # tail SZDB.err
140906 22:06:45 InnoDB: Completed initialization of buffer pool
140906 22:06:45 InnoDB: highest supported file format is Barracuda.
140906 22:06:45 InnoDB: Waiting for the background threads to start
140906 22:06:46 InnoDB: 5.5.37 started; log sequence number 1605345
140906 22:06:47 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
140906 22:06:47 [Note] - '0.0.0.0' resolves to '0.0.0.0';
140906 22:06:47 [Note] Server socket created on IP: '0.0.0.0'.
140906 22:06:47 [Note] Event Scheduler: Loaded 0 events
140906 22:06:47 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.37-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
#停止mysql服務器
SZDB:~ # service mysql stop
Shutting down MySQL.... done
#使用配置文件來設置log-error參數
SZDB:~ # echo "log-error=/tmp/SZDB.err">>/etc/my.cnf
SZDB:~ # echo "skip_opt">>/etc/my.cnf #添加一個異常參數skip_opt
SZDB:~ # grep -v ^# /etc/my.cnf
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log-error=/tmp/SZDB.err
skip_opt
#Author : Leshami
#Blog : http://blog.csdn.net/leshami
#啟動mysql服務器
SZDB:~ # mysqld_safe --user=mysql &
[1] 7315
SZDB:~ # 140907 13:40:33 mysqld_safe Logging to '/tmp/SZDB.err'.
140907 13:40:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
140907 13:40:33 mysqld_safe mysqld from pid file /var/lib/mysql/SZDB.pid ended
[1]+ Done mysqld_safe --user=mysql
SZDB:~ # more /tmp/SZDB.err
140907 13:40:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
140907 13:40:33 [ERROR] /usr/sbin/mysqld: ambiguous option '--skip-opt' (--skip-optimizer_prune_level)
140907 13:40:33 [ERROR] Aborting #出現錯誤提示為有歧義的參數,實例終止
140907 13:40:33 mysqld_safe mysqld from pid file /var/lib/mysql/SZDB.pid ended
#修改my.cnf,刪除skip-opt選項
SZDB:~ # vi /etc/my.cnf
SZDB:~ # grep -v ^# /etc/my.cnf
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log-error=/tmp/SZDB.err
#再次啟動mysql服務器
SZDB:~ # mysqld_safe --user=mysql &
[1] 7511
SZDB:~ # 140907 13:43:23 mysqld_safe Logging to '/tmp/SZDB.err'.
140907 13:43:23 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
SZDB:~ # more /tmp/SZDB.err
140907 13:40:33 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
140907 13:40:33 [ERROR] /usr/sbin/mysqld: ambiguous option '--skip-opt' (--skip-optimizer_prune_level)
140907 13:40:33 [ERROR] Aborting
#以下內容為正常啟動的相關信息
140907 13:40:33 mysqld_safe mysqld from pid file /var/lib/mysql/SZDB.pid ended
140907 13:43:23 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
140907 13:43:23 [Note] Plugin 'FEDERATED' is disabled.
140907 13:43:23 InnoDB: The InnoDB memory heap is disabled
140907 13:43:23 InnoDB: Mutexes and rw_locks use GCC atomic builtins
140907 13:43:23 InnoDB: Compressed tables use zlib 1.2.3
140907 13:43:23 InnoDB: Using Linux native AIO
140907 13:43:23 InnoDB: Initializing buffer pool, size = 128.0M
140907 13:43:23 InnoDB: Completed initialization of buffer pool
140907 13:43:23 InnoDB: highest supported file format is Barracuda.
140907 13:43:23 InnoDB: Waiting for the background threads to start
140907 13:43:24 InnoDB: 5.5.37 started; log sequence number 1620641
140907 13:43:25 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
140907 13:43:25 [Note] - '0.0.0.0' resolves to '0.0.0.0';
140907 13:43:25 [Note] Server socket created on IP: '0.0.0.0'.
140907 13:43:25 [Note] Event Scheduler: Loaded 0 events
140907 13:43:25 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.5.37-log' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL)
從上面的錯誤日可以看出,錯誤日志文件的格式,通常如下:
時間 [錯誤級別] 錯誤信息
有些日志信息不一定包含錯誤級別