代码改变世界

MySQL错误日志总结

2016-05-11 23:49  潇湘隐者  阅读(40334)  评论(2编辑  收藏  举报

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)

clip_image001

如果关闭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,如下所示

clip_image002

如果配置文件中设置了参数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