问题描述
现象
5.6 和 5.7 时区设置相同,select now()也显示当前时间
5.7 error log 中时间和当前时间差8小时
问题分析
5.6 写 error log 函数如下
取时间的函数是localtime_r(&skr, &tm_tmp)
日志中时间和系统时区相同
static void print_buffer_to_file(enum loglevel level, const char *buffer, size_t length) { time_t skr; struct tm tm_tmp; struct tm *start; DBUG_ENTER("print_buffer_to_file"); DBUG_PRINT("enter",("buffer: %s", buffer)); mysql_mutex_lock(&LOCK_error_log); skr= my_time(0); localtime_r(&skr, &tm_tmp); start=&tm_tmp; fprintf(stderr, "%d-%02d-%02d %02d:%02d:%02d %lu [%s] %.*s\n", start->tm_year + 1900, start->tm_mon + 1, start->tm_mday, start->tm_hour, start->tm_min, start->tm_sec, current_pid, (level == ERROR_LEVEL ? "ERROR" : level == WARNING_LEVEL ? "Warning" : "Note"), (int) length, buffer); fflush(stderr); mysql_mutex_unlock(&LOCK_error_log); DBUG_VOID_RETURN; }
复制
5.7 写 error log 函数如下
取时间的函数是 make_iso8601_timestamp(my_timestamp)
static void print_buffer_to_file(enum loglevel level, const char *buffer, size_t length) { DBUG_ENTER("print_buffer_to_file"); DBUG_PRINT("enter",("buffer: %s", buffer)); char my_timestamp[iso8601_size]; my_thread_id thread_id= 0; /* If the thread system is up and running and we're in a connection, add the connection ID to the log-line, otherwise 0. */ if (THR_THD_initialized && (current_thd != NULL)) thread_id= current_thd->thread_id(); make_iso8601_timestamp(my_timestamp); /* This must work even if the mutex has not been initialized yet. At that point we should still be single threaded so that it is safe to write without mutex. */ if (error_log_initialized) mysql_mutex_lock(&LOCK_error_log); if (error_log_buffering) { // Logfile not open yet, buffer messages for now. if (buffered_messages == NULL) buffered_messages= new (std::nothrow) std::string(); std::ostringstream s; s << my_timestamp << " " << thread_id; if (level == ERROR_LEVEL) s << " [ERROR] "; else if (level == WARNING_LEVEL) s << " [Warning] "; else s << " [Note] "; s << buffer << std::endl; buffered_messages->append(s.str()); } else { fprintf(stderr, "%s %u [%s] %.*s\n", my_timestamp, thread_id, (level == ERROR_LEVEL ? "ERROR" : level == WARNING_LEVEL ? "Warning" : "Note"), (int) length, buffer); fflush(stderr); } if (error_log_initialized) mysql_mutex_unlock(&LOCK_error_log); DBUG_VOID_RETURN; }
复制
make_iso8601_timestamp 中有代码段如下
参数 opt_log_timestamps 控制时间
if (opt_log_timestamps == 0) gmtime_r(&seconds, &my_tm); else { localtime_r(&seconds, &my_tm);
复制
opt_log_timestamps 对应 sys_vars.cc 中的 log_timestamps
取值 const char *timestamp_type_names[]= {“UTC”, “SYSTEM”, NullS};
log_timestamps = 0 时,日志中是 UTC 时区
log_timestamps = 1 时,日志中是 SYSTEM 时区
5.7 默认 log_timestamps = 0
5.7 error log 使用系统时区
set global log_timestamps = 1;
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
国内独家|阿里云首发MongoDB 8.0,性能提升“快”人一步
阿里云瑶池数据库
215次阅读
2025-04-24 09:53:13
SQL 优化之 OR 子句改写
xiongcc
136次阅读
2025-04-21 00:08:06
揭秘PostgreSQL SELECT查询中的意外“写”行为
PolarDB
53次阅读
2025-04-15 09:56:40
秒级修改字段
胖橘
44次阅读
2025-04-23 11:33:45
PolarDB MySQL之列存索引(IMCI)性能
xiaozhuo
42次阅读
2025-04-21 16:11:53
PolarDB MySQL之在只读节点上创建自定义临时表
xiaozhuo
42次阅读
2025-04-21 15:49:05
PolarDB MySQL之列存索引测试方法
xiaozhuo
41次阅读
2025-04-21 16:06:42
PostgreSQL 虚拟文件描述符
PolarDB
40次阅读
2025-04-25 10:10:46
登顶TPC-C|云原生数据库PolarDB技术揭秘:高可用-无感切换篇
阿里云瑶池数据库
39次阅读
2025-04-11 15:35:14
现已开源|阿里云RDS发布MCP Server能力,解锁数据库“对话即运维”新体验
阿里云瑶池数据库
37次阅读
2025-04-23 11:19:36
热门文章
深度学习训练系统的I/O缓存机制 :《Shade: Enable Fundamental Cacheability for Distributed Deep Learning Training》导读(一)
2023-06-24 439浏览
PolarDB · InnoDB 全文索引简介(一)
2023-06-25 418浏览
PolarDB · InnoDB 全文索引简介(二)
2023-06-25 335浏览
POLARDB · 理论基础 · 敢问路在何方 — 论B+树索引的演进方向(二)
2023-07-25 261浏览
PolarDB ·引擎特性· DDL中MDL锁的优化和演进(四)
2023-06-24 228浏览