MySQL 錯誤記錄檔(Error Log),mysqllog

來源:互聯網
上載者:User

MySQL 錯誤記錄檔(Error Log),mysqllog

    同大多數關係型資料庫一樣,記錄檔是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)

從上面的錯誤日可以看出,錯誤記錄檔檔案的格式,通常如下:
  時間  [錯誤層級]  錯誤資訊
  有些日誌資訊不一定包含錯誤層級

 

 


ubuntu怎查看mysql的錯誤記錄檔?

是否啟用了日誌
mysql>show variables like 'log_%';
怎樣知道當前的日誌
mysql> show master status;
顯示二進位日誌數目
mysql> show master logs;
看二進位記錄檔用mysqlbinlog
shell>mysqlbinlog mail-bin.000001
或者shell>mysqlbinlog mail-bin.000001 | tail
在設定檔中指定log的輸出位置.
Linux 的設定檔為 my.cnf ,一般在 /etc 下。
在linux下:
Sql代碼
# 在[mysqld] 中輸入
#log
log-error=/usr/local/mysql/log/error.log
log=/usr/local/mysql/log/mysql.log
long_query_time=2
log-slow-queries= /usr/local/mysql/log/slowquery.log

# 在[mysqld] 中輸入 #log
log-error=/usr/local/mysql/log/error.log
log=/usr/local/mysql/log/mysql.log
long_query_time=2
log-slow-queries= /usr/local/mysql/log/slowquery.log
 
mysql錯誤記錄檔在什地方

如果你什麼都沒改過,
如果是windows下,一般是安裝目錄下的data目錄下 副檔名是.err那個檔案,你可以開啟安裝目錄下的my.ini檔案檢查一下
如果是linux下,一般是/var/log/mysqld.log,你最好用cat /etc/my.cnf看看
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.