MySQL錯誤日志是記錄MySQL 運行過程中較為嚴重的警告和錯誤信息,以及MySQL每次啟動和關閉的詳細信息。錯誤日志的命名通常為hostname.err。其中,hostname表示服務器主機名。
The error log contains information indicating when mysqld was started and stopped and also any critical errors that occur while the server is running. If mysqld notices a table that needs to be automatically checked or repaired, it writes a message to the error log. On some operating systems, the error log contains a stack trace if mysqld exits abnormally. The trace can be used to determine where mysqld exited. See Section 24.5, “Debugging and Porting MySQL”.
查看錯誤日志的位置
錯誤日志默認存放位置為數據目錄下,你也可以用下面命令查看。例如下面是我測試服務的情況。
mysql> show variables like '%log_error%';
+---------------+------------------------------------------------------+
| Variable_name | Value |
+---------------+------------------------------------------------------+
| log_error | /home/WDPM/MysqlData/mysql/DB-Server.localdomain.err |
+---------------+------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
修改錯誤日志的位置
錯誤日志所記錄的信息是可以通過log-error和log-warnings來定義的,其中log_error是定義是否啟用錯誤日志的功能和錯誤日志的存儲位置,log-warnings是定義是否將警告信息也定義至錯誤日志中。可以在啟動MySQL時,指定log_error的值。如下所示
[root@DB-Server ~]# /etc/init.d/mysql start --log_error=/tmp/DB-Server.localdomain.err
Starting MySQL..........[ OK ]
[root@DB-Server ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.20-enterprise-commercial-advanced-log MySQL Enterprise Server - Advanced Edition (Commercial)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like '%log_error%';
+---------------+--------------------------------+
| Variable_name | Value |
+---------------+--------------------------------+
| log_error | /tmp/DB-Server.localdomain.err |
+---------------+--------------------------------+
1 row in set (0.00 sec)
如果關閉MySQL,然後重啟MySQL,你會發現log_error的值又變為了/home/WDPM/MysqlData/mysql/DB-Server.localdomain.err 。也就是說這樣設置只是對本次有效,如果需要永久修改,那麼就必須在my.cnf中指定log_error參數的值。
錯誤日志可以任意命名嗎? 答案是可以,例如我在/etc/my.cnf配置文件中,添加了參數log_error=/u02/mysql/mysql.err,重新啟動MySQL,如下所示
如果配置文件中設置了參數log_error,啟動MySQL時也設置參數log_error,那麼那個優先級高呢?我測試驗證了一下,my.cnf的參數優先級別要高於啟動是的參數log_error
/etc/init.d/mysql start log_error=/tmp/DB-Server.localdomain.err
錯誤日志歸檔備份
錯誤日志如果不清理或刪除,那麼它會一直增長。在MySQL 5.5.7之前,可以通過mysqladmin –uroot –p flush-logs命令刪除錯誤日志。MySQL 5.5.7以及之後,只能通過下面方式來歸檔、備份錯誤日志.
shell> mv host_name.err host_name.err-old
shell> mysqladmin -u root -p flush-logs
shell> mv host_name.err-old backup-directory