最近測試MySQL 5.7.21 Community Server這個版本的MySQL資料庫時,發現其錯誤日誌的時間跟系統當前時間不一致,後面檢查發現日期時間格式都是UTC時間,查了一下相關資料,原來在MySQL 5.7.2 之後日誌文件裡面的時間戳從預設的本地系統時區改為了UTC格式。MySQL ... ...
最近測試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