最近测试MySQL 5.7.21 Community Server这个版本的MySQL数据库时,发现其错误日志的时间跟系统当前时间不一致,后面检查发现日期时间格式都是UTC时间,查了一下相关资料,原来在MySQL 5.7.2 之后日志文件里面的时间戳从默认的本地系统时区改为了UTC格式。MySQL 5.7.2多了一个参数log_timestamps ,这个参数主要是控制错误日志、慢查询日志等日志中的显示时间。但它不会影响查询日志和慢日志写到表 (mysql.general_log, mysql.slow_log) 中的显示时间。在查询记录的时候,可以使用 CONVERT_TZ() 函数,或者设置会话级别的系统变量 time_zone 来转换成所需要的时区。官方资料详细介绍如下所示:
This variable controls the time zone of timestamps in messages written to the error log, and in general query log and slow query log messages written to files. It does not affect the time zone of general query log and slow query log messages written to tables(mysql.general_log, mysql.slow_log). Rows retrieved from those tables can be converted from the local system time zone to any desired time zone with CONVERT_TZ() or by setting the session time_zone system variable.
Permitted log_timestamps values are UTC (the default) and SYSTEM (local system time zone).
Timestamps are written using ISO 8601 / RFC 3339 format: YYYY-MM-DDThh:mm:ss.uuuuuu plus a tail value of Z signifying Zulu time (UTC) or ±hh:mm (an offset from UTC).
This variable was added in MySQL 5.7.2. Before 5.7.2, timestamps in log messages were written using the local system time zone by default, not UTC. If you want the previous log message time zone default, set log_timestamps=SYSTEM.
此参数是全局的,可以动态修改,修改参数log_timestamps的值非常简单,如下所示,不过最好在参数文件my.cnf设置该参数值,以防MySQL服务重启失效。
mysql> show variables like 'log_timestamps';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| log_timestamps | UTC |
+----------------+-------+
1 row in set (0.01 sec)
mysql> set global log_timestamps=system;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'log_timestamps';
+----------------+--------+
| Variable_name | Value |
+----------------+--------+
| log_timestamps | SYSTEM |
+----------------+--------+
1 row in set (0.01 sec)
mysql>
参考资料:
http://mysql.taobao.org/monthly/2017/01/09/
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_log_timestamps