MySQL中几个关于时间/时区的变量
一、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中几个关于时间/时区的变量的更多相关文章
- MySQL中的日期和时间:使用和说明,以及常用函数
1.首先需要注意: 1.1 MySQL中把日期和时间是分开的. 以字符串2007-12-31 00:59:59为例: 日期部分:2007-12-31.这部分也可以叫做一个日期表达式. 时间部分:00: ...
- 开发过程中 的一些 补充知识点 + 关于mysql中的日期和时间函数?
参考: https://www.jb51.net/article/23966.htm https://yq.aliyun.com/articles/260389 mysql中的 日期格式是: HHHH ...
- mysql中TIMESTAMP设置默认时间为当前时间
在我们保存数据进入到数据库中时多半会使用像php之类的脚本来获取一个时间保存到mysql中,其实在mysql可以直接使用TIMESTAMP 数据类型来实现默认类型了,下面一起来看看. 很多时候,为 ...
- mysql中的timestamp类型时间比较:unix_timestamp函数
在mysql中,某字段的类型设置为了timestamp,那么我们现在希望取出指定时间段的记录,该如何做呢? 在php中有time()和strtotime()来进行日期和时间戳的格式化,而在mysql中 ...
- mysql中几个日期时间类型之间的区别和使用
MySQL中有如下几个时间类型:date.time.datetime.timestamp.year MySQL数据类型 含义 date 只存 ...
- MySQL中的运算符和时间运算
一.MySQL中运算符的分类 算术运算符,比较运算符,逻辑运算符,按位运算符 二.算数运算符 符号 作用 + 加法 - 减法 * 乘法 ...
- MySQL中的两个时间函数,用来做两个时间之间的对比
TIMESTAMPDIFF,(如果当期时间和之前时间的分钟数相比较.大于1天,即等于1:小于1天,则等于0) select TIMESTAMPDIFF(DAY,'2016-11-16 10:13:42 ...
- MySQL中关于日期、时间的数据类型和函数
一.日期相关的数据类型 1.datetime 占用8字节,既显示了日期,又显示了时间.其表示的日期范围为“1000-01-01 00:00:00”到“9999-12-31 23:59:59” 2.da ...
- MySQL中的日期和时间函数
常用日期函数如下: 函 数 功 能 CURDATE() 获取当前日期 CURTIME() 获取当前时间 NOW() 获取当前的日期和时间 UNIX_TIMESTAMP(date) 获取日期的U ...
随机推荐
- appium学习记录 elements默认获取第一个元素
封装 初始配置时候 要当做参数传入 element 发送的是一个元组 2 下标定位 当用elements进行定位时候 同时又有很多元素时候 默认获取第一个,但我们的元素是在后面时候 例如第四个 el ...
- codeforces369A
Valera and Plates CodeForces - 369A Valera is a lazy student. He has m clean bowls and k clean plate ...
- BZOJ5371[Pkusc2018]星际穿越——可持久化线段树+DP
题目描述 有n个星球,它们的编号是1到n,它们坐落在同一个星系内,这个星系可以抽象为一条数轴,每个星球都是数轴上的一个点, 特别地,编号为i的星球的坐标是i. 一开始,由于科技上的原因,这n个星球的居 ...
- 有标号的DAG计数(FFT)
有标号的DAG计数系列 有标号的DAG计数I 题意 给定一正整数\(n\),对\(n\)个点有标号的有向无环图(可以不连通)进行计数,输出答案\(mod \ 10007\)的结果.\(n\le 500 ...
- Codeforces-gym-101020 problem C. Rectangles
题目链接:http://codeforces.com/gym/101020/problem/C C. Rectangles time limit per test 2.0 s memory limit ...
- ECMAScript 6 -- 数组的解构赋值
模式匹配:只要等号两边的模式相同,左边的变量就会被赋予对应的值. let [a, b, c] = [1, 2, 3]; 嵌套数组进行解构: let [foo, [[bar], baz]] = [1, ...
- SqlServer存储过程详解
SqlServer存储过程详解 1.创建存储过程的基本语法模板: if (exists (select * from sys.objects where name = 'pro_name')) dro ...
- [HEOI2013]SAO ——计数问题
题目大意: Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...
- Markdown语法整理
标题 语法格式:'#'+'空格'+'文本',一共6级 # 一级标题 ## 二级标题 ### 三级标题 #### 四级标题 ##### 五级标题 ###### 六级标题 斜体 语法格式:1个星号包裹,我 ...
- Faster RCNN代码理解(Python)
转自http://www.infocool.net/kb/Python/201611/209696.html#原文地址 第一步,准备 从train_faster_rcnn_alt_opt.py入: 初 ...