关于Mysql时区的那点事儿
- 查看当前数据库时区
Methods(either):
select @@global.time_zone, @@session.time_zone;
select timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00'));
select if(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone);
select timediff(NOW(), UTC_TIMESTAMP);
-- 系统时区
select @@system_time_zone;
- 设置当前数据库时区
Methods(either):
set time_zone = '+08:00';
set global time_zone = '+08:00' //设置时区为 UTC+8
set global time_zone = 'Asia/Shanghai';
set @@global.time_zone = '+08:00';
set @@session.time_zone = "+08:00"; //对特定会话更改时区
or 修改配置文件:
在配置文件"my.cnf"中的[mysqld] 部分增加如下语句:
default-time-zone='+08:00'
or 客户端数据源连接字符串设置:
jdbc:mysql://***:3306/db?useUnicode=yes&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=Asia/Shanghai
- 实例查看时区及Timestamp与Datetime的不同
create table foo (tstamp timestamp, dt datetime);
insert into foo (tstamp, dt) values (now(), now());
select * from foo;
set @@session.time_zone = "+00:00";
select * from foo;