一、log_timestamps

1.1、官方解释

log_timestamps: Log timestamp format. Added in MySQL 5.7.2.
This variable controls the timestamp time zone of error log messages, and of 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.
Permitted log_timestamps values are UTC (the default) and SYSTEM (local system time zone).

简单来说log_timestamps变量控制着error log、general log、slow log这些文件中的时间信息。log_timestamps有效的值为UTC(默认)和SYSTEM(本地系统时区)

1.2、使用样例

可以使用下面方式设置log_timestamps

# 启动命令
--log_timestamps=[UTC|SYSTEM]
# 配置文件
log_timestamps=[UTC|SYSTEM]
# 运行期间
set global log_timestamps = ['UTC'|'SYSTEM']

查看并修改log_timestamps的值

# 查看log_timestamps
mydba@192.168.85.132,3306 [replcrash]> show variables like '%log_timestamps%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| log_timestamps | UTC |
+----------------+-------+
row in set (0.01 sec)
# 设置log_timestamps为system
mydba@192.168.85.132,3306 [replcrash]> set global log_timestamps = 'system';
Query OK, 0 rows affected (0.03 sec) # 默认UTC时间的日志信息
2018-01-10T02:45:37.093284Z 21 [Note] Access denied for user 'mydba'@'192.168.85.132' (using password: YES)
# 设置log_timestamps为system(东八区)的日志信息
2018-01-10T10:46:07.031340+08:00 22 [Note] Access denied for user 'mydba'@'192.168.85.132' (using password: YES)

可以看到日志中时间格式为:YYYY-MM-DDThh:mm:ss.uuuuuu+Z(UTC)/±hh:mm(相对UTC的偏移量)

二、time_zone

2.1、官方解释

The current time zone. This variable is used to initialize the time zone for each client that connects. By default, the initial value of this is 'SYSTEM' (which means, “use the value of system_time_zone”).

2.2、使用样例

可以使用下面方式设置global server time zone

# 启动命令
--default-time-zone=timezone
# 配置文件
default-time-zone=timezone
# 运行期间
set global time_zone = timezone timezone values can be given in several formats, none of which are case sensitive:
• The value 'SYSTEM' indicates that the time zone should be the same as the system time zone.
• The value can be given as a string indicating an offset from UTC, such as '+10:00' or '-6:00'.
• The value can be given as a named time zone, such as 'Europe/Helsinki', 'US/Eastern', or 'MET'. Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.

查看并修改time_zone的值

# 查看MySQL当前时区、时间
mydba@192.168.85.132,3306 [replcrash]> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.01 sec) mydba@192.168.85.132,3306 [replcrash]> select now();
+---------------------+
| now() |
+---------------------+
| 2018-01-10 11:29:25 |
+---------------------+
1 row in set (0.00 sec)
time_zone说明MySQL使用system的时区,system_time_zone说明system使用CST时区 # 修改MySQL全局时区为西八区
mydba@192.168.85.132,3306 [replcrash]> set global time_zone = '-8:00';
Query OK, 0 rows affected (0.00 sec)
# 修改当前会话时区
mydba@192.168.85.132,3306 [replcrash]> set time_zone = '-8:00';
Query OK, 0 rows affected (0.00 sec)
# 查看MySQL当前时区、时间
mydba@192.168.85.132,3306 [replcrash]> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | -08:00 |
+------------------+--------+
2 rows in set (0.02 sec) mydba@192.168.85.132,3306 [replcrash]> select now();
+---------------------+
| now() |
+---------------------+
| 2018-01-09 19:31:15 |
+---------------------+
1 row in set (0.00 sec) # 当前的time_zone
mydba@192.168.85.132,3306 [replcrash]> select @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| -08:00 | -08:00 |
+--------------------+---------------------+
1 row in set (0.00 sec)

The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval.
The current time zone setting does not affect values displayed by functions such as UTC_TIMESTAMP() or values in DATE, TIME, or DATETIME columns. Nor are values in those data types stored in UTC; the time zone applies for them only when converting from TIMESTAMP values. If you want locale-specific arithmetic for DATE, TIME, or DATETIME values, convert them to UTC, perform the arithmetic, and then convert back.
time_zone变量对时区敏感的time values有影响,比如now()、curtime()、timestamp columns(TIMESTAMP列,在库中存储的是TIMESTAMP(一串数字))

三、system_time_zone

3.1、官方解释

The server system time zone. When the server begins executing, it inherits a time zone setting from the machine defaults, possibly modified by the environment of the account used for running the server or the startup script. The value is used to set system_time_zone. Typically the time zone is specified by the TZ environment variable. It also can be specified using the --timezone option of the mysqld_safe script.
The system_time_zone variable differs from time_zone. Although they might have the same value, the latter variable is used to initialize the time zone for each client that connects.

前面提到的log_timestamps可以设置为SYSTEM,time_zone默认初始化为SYSTEM,这些SYSTEM就是指system_time_zone。实例启动时默认会继承当前服务器的时区,我们可以通过TZ环境变量或者mysqld_safe --timezone选项设置system_time_zone

3.2、跨时区主从复制的潜在问题

跨时区主从复制的潜在问题参考这里

# 主库执行(东六区)
mysql> insert into mytable values(now())
mysql> select * from mytable --返回2018-01-10 13:50:33 # 从库应用relay-log(东八区)
mysql> select * from mytable --返回2018-01-10 13:50:33 # 我们希望从库查询的结果是
mysql> select * from mytable --返回2018-01-10 15:50:33 东六区的 2018-01-10 13:50:33 和东八区的 2018-01-10 15:50:33 才是同一时间,我们就是要解决这样的问题

这种问题应该只存在于SBR环境,RBR的binlog中记录的是具体数值,不会记录now()之类的函数
By default, master and slave servers assume that they are in the same time zone. If you are replicating between servers in different time zones, the time zone must be set on both master and slave. Otherwise, statements depending on the local time on the master are not replicated properly, such as statements that use the NOW() or FROM_UNIXTIME() functions. Set the time zone in which MySQL server runs by using the --timezone=timezone_name option of the mysqld_safe script or by setting the TZ environment variable. 
下面测试在主、从设置time zone后是否如你所愿

# mysqld_safe --timezone
shell> /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3306/my.cnf --timezone=UTC &
shell> /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql3308/my.cnf --timezone=MET &
影响的是system_time_zone变量,主从复制,statement格式下,now()复制到从库
-- ::->-- :: # mysqld --default-time-zone
shell> /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --default-time-zone=+: &
shell> /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3308/my.cnf --default-time-zone=+: &
影响的是time_zone变量,主从复制,statement格式下,now()复制到从库
-- ::->-- :: 原因:mysqld_safe --timezone参数,在从库的binlog可以看到
SET TIMESTAMP=;
SET @@session.time_zone='SYSTEM'; #会根据system_time_zone中的时区去设置
insert ... select now(); mysqld --default-time-zone参数,在从库的binlog可以看到
SET TIMESTAMP=;
SET @@session.time_zone='+06:00'; #直接复制主库,时间不会变化
insert ... select now(); # time_zone、timestamp对now()的影响
mysql> set time_zone='+6:00';set timestamp=;select now();
mysql> set time_zone='+8:00';set timestamp=;select now();
# 恢复timestamp
mysql> set timestamp=;select now(); # timezone_name需要手动load
mysql> SELECT CONVERT_TZ('2018-01-10 12:00:00','+8:00','+6:00');
mysql> SELECT CONVERT_TZ('2018-01-10 12:00:00','US/Eastern','US/Central');
mysql> SELECT FROM_UNIXTIME(),UNIX_TIMESTAMP(now());

可以看出只有主、从设置system_time_zone后,之前的潜在问题才会按我们预期的结果显示~

四、参考文档

MySQL Server Time Zone Support:https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html
Replication and System Functions:https://dev.mysql.com/doc/refman/5.7/en/replication-features-functions.html
Replication and Time Zones:https://dev.mysql.com/doc/refman/5.7/en/replication-features-timezone.html

MySQL中几个关于时间/时区的变量的更多相关文章

  1. MySQL中的日期和时间:使用和说明,以及常用函数

    1.首先需要注意: 1.1 MySQL中把日期和时间是分开的. 以字符串2007-12-31 00:59:59为例: 日期部分:2007-12-31.这部分也可以叫做一个日期表达式. 时间部分:00: ...

  2. 开发过程中 的一些 补充知识点 + 关于mysql中的日期和时间函数?

    参考: https://www.jb51.net/article/23966.htm https://yq.aliyun.com/articles/260389 mysql中的 日期格式是: HHHH ...

  3. mysql中TIMESTAMP设置默认时间为当前时间

    在我们保存数据进入到数据库中时多半会使用像php之类的脚本来获取一个时间保存到mysql中,其实在mysql可以直接使用TIMESTAMP 数据类型来实现默认类型了,下面一起来看看.   很多时候,为 ...

  4. mysql中的timestamp类型时间比较:unix_timestamp函数

    在mysql中,某字段的类型设置为了timestamp,那么我们现在希望取出指定时间段的记录,该如何做呢? 在php中有time()和strtotime()来进行日期和时间戳的格式化,而在mysql中 ...

  5. mysql中几个日期时间类型之间的区别和使用

    MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型           含义 date                     只存 ...

  6. MySQL中的运算符和时间运算

    一.MySQL中运算符的分类 算术运算符,比较运算符,逻辑运算符,按位运算符 二.算数运算符 符号                            作用 + 加法   - 减法   * 乘法   ...

  7. MySQL中的两个时间函数,用来做两个时间之间的对比

    TIMESTAMPDIFF,(如果当期时间和之前时间的分钟数相比较.大于1天,即等于1:小于1天,则等于0) select TIMESTAMPDIFF(DAY,'2016-11-16 10:13:42 ...

  8. MySQL中关于日期、时间的数据类型和函数

    一.日期相关的数据类型 1.datetime 占用8字节,既显示了日期,又显示了时间.其表示的日期范围为“1000-01-01 00:00:00”到“9999-12-31 23:59:59” 2.da ...

  9. MySQL中的日期和时间函数

    常用日期函数如下: 函   数 功   能 CURDATE() 获取当前日期 CURTIME() 获取当前时间 NOW() 获取当前的日期和时间 UNIX_TIMESTAMP(date) 获取日期的U ...

随机推荐

  1. 百度/头条合作命中注定!中国新BAT要来了

    据外媒报道,今日头条母公司字节跳动(ByteDace)将为中国互联网传统BAT的格局,带来一些新的活力.这家增速飞快的新闻.视频App“制造者”已经估值高达750亿美元,与三巨头之一的百度平起平坐,后 ...

  2. 华为5G在印度被禁

    前段时间,澳大利亚政府以“担心外国渗透”为由,决定禁止华为为建设新的5G网络提供设备.这让大家不禁猜测,难道华为的5G真的被国外市场禁入了? 对此,华为表达出了极大的失落感,并在一份声明中称,“政府告 ...

  3. tomcat 查看和修改内存

    为了解决tomcat在大进行大并发请求时,出现内存溢出的问题,请修改tomcat的内存大小,其中分为以下两种方式: 一.使用 catalina.bat 等命令行方式运行的 tomcat 查看系统最大支 ...

  4. BZOJ1468Tree——点分治

    题目描述 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K 输入 N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是k 输出 一行,有多 ...

  5. python numpy 数组拼接

    我就写一下我遇到的,更多具体的请看Python之Numpy数组拼接,组合,连接 >>> aarray([0, 1, 2],       [3, 4, 5],       [6, 7, ...

  6. binary search模板总结

    二分查找算法是最常用的一种高效算法,所以本文将常见的情形做一个总结,得到一个二分查找的模板,方便应对各种二分查找中的问题. 当前有一个有序的数列: 1, 5, 9 [每个数字都是唯一的] 1, 2, ...

  7. luogu2149 Elaxia的路线 (dijkstra+拓扑dp)

    先标记上一个人所有最短路上的边(同时也要标记反向边) 然后拿着另一个人最短路上的边(会构成一个DAG)去做拓扑dp,记从原点到某个点的最大的某个路径的被标记的边的个数 #include<bits ...

  8. JavaScript -- throw、try 和 catch

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. 很想java哦. <!DOCTYPE html> <html> <head& ...

  9. MVC中权限的知识点及具体实现代码

    一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...

  10. xor定理证明

    xor 证明: 0 xor 0=0 0 xor 1=1 1 xor 0=1 1 xor 1=0 0 xor 其它数,数值不会改变1 xor 其它数,数值会反转 所以x个数0和y个数1进行xor运算(0 ...