怎么解决从Mysql数据库获取timestamp比正常时间早8小时问题

47次阅读
没有评论

本篇内容介绍了“怎么解决从 Mysql 数据库获取 timestamp 比正常时间早 8 小时问题”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

问题是:使用同一 Mysql 数据库,获取相同时间数据,window 下返回页面是正常时间,但在 linux 下却早了 8 小时。

服务器环境:centos8,mysql8.0.21

一、首先排查服务器时区设置及系统时钟和硬件时钟的同步:

(一)date  查看 / 设置系统时间
1、将日期设置为 2020 年 11 月 6 日
[root@centos7 ~]# date -s 11/06/20
2、将时间设置为 11 点 12 分 13 秒
[root@centos7 ~]# date -s 11:12:13
3、将时间设置为 2020 年 11 月 6 日 11 点 12 分 13 秒(MMDDhhmmYYYY.ss)[root@centos7 ~]# date 1106111220.13(二)hwclock/clock  查看 / 设置硬件时间
1、查看系统硬件时钟(以下两个一样效果)[root@centos7 ~]# hwclock --show
[root@centos7 ~]# clock --show
2、设置硬件时间(以下两个一样效果)[root@centos7 ~]# hwclock --set --date= 11/06/20 12:13  (月 / 日 / 年时: 分: 秒)[root@centos7 ~]# clock --set --date= 11/06/20 12:13  (月 / 日 / 年时: 分: 秒)(三)同步系统及硬件时钟
1、系统时间找硬件时间同步(以下两个一样效果)[root@centos7 ~]# hwclock --hctosys
[root@centos7 ~]# clock --hctosys 
备注:hc 代表硬件时间,sys 代表系统时间,以硬件时间为基准,系统时间找硬件时间同步
2、硬件时间找系统时间同步(以下两个一样效果)[root@centos7 ~]# hwclock --systohc
[root@centos7 ~]# clock --systohc 
备注:以系统时间为基准,硬件时间找系统时间同步(四)修改时区
#CentOS 和 Ubuntu 的时区文件是 /etc/localtime,但是在 CentOS7 以后 localtime 以及变成了一个链接文件
[root@centos7 ~]# ll /etc/localtime 
lrwxrwxrwx 1 root root 33 Nov 15 2020 /etc/localtime -  /usr/share/zoneinfo/Asia/Shanghai
#  如果错误,需要修改,有多种方法:[root@centos7 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#  最好的方法是使用 timedatectl 命令
[root@centos7 ~]# timedatectl list-timezones |grep Shanghai # 查找中国时区的完整名称
[root@centos7 ~]# timedatectl set-timezone Asia/Shanghai # 其他时区以此类推
#  或者直接手动创建软链接
[root@centos7 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

二、如果确定服务器存在问题,却又因系统地域等原因无法修改,也可以设置 mysql 的时区:

select now();
+---------------------+
| now() |
+---------------------+
| 2020-11-23 12:30:06 |
+---------------------+
1 row in set (0.00 sec)
  show variables like  %time_zone% 
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | EST |
| time_zone | SYSTEM |
+------------------+--------+
2 rows in set (0.00 sec)

time_zone 说明 mysql 使用 system 的时区
system_time_zone 说明 system 使用 EST 时区(PS:EST 美国时区,CST 世界标准世界)

# 临时修改,在 mysql 中执行
 set global time_zone =  +8:00  ## 修改 mysql 全局时区为北京时间,即我们所在的东 8 区
 set time_zone =  +8:00  ## 修改当前会话时区
 flush privileges; # 立即生效
#永久修改,退出 mysql 执行
[root@centos7 ~]# vim /etc/my.cnf ## 在 [mysqld] 区域中加上
[root@centos7 ~]# default-time_zone =  +8:00 
[root@centos7 ~]# /etc/init.d/mysqld restart ## 重启 mysql 使新时区生效

三、如果确定服务器存在问题,却又因客观因素无法修改服务器和数据库,也可以在数据库请求 URL 上修改:

application.yml 配置:(系统数据库参数配置文件,GMT%2B8 这个参数转义后 GMT+ 8 表示设置数据库时间为东八区 (北京) 时间,如果设置 GMT,可以在 Spring.jackson.time-zone 中设置 GMT+8,设置一处就可以)datasource:
 url: jdbc:mysql://localhost:3306/test-db?useUnicode=true characterEncoding=UTF-8 useSSL=false useTimezone=true serverTimezone=GMT%2B8
spring:
 jackson:
 date-format: yyyy-MM-dd HH:mm:ss
 time-zone: GMT+8

四、也有脑洞更大的配置,每处取值都要做注释很容易疏漏:

#  在实体类 Po 类的 Date 上设置,来接收数据库中的时间字段:@JsonFormat(pattern =  yyyy-MM-dd HH:mm:ss ,timezone= GMT+8)

进行一波改动后,回归正题:Docker 容器时间与主机时间不一致的问题,又是 Docker 在捣鬼!

#  主机时间
[root@centos8]# date
Mon Nov 23 13:43:52 CST 2020
#  容器时间
[root@centos8]# docker exec e8573a89fb94 date
Mon Nov 23 05:44:39 UTC 2020

CST 应该是指(China Shanghai Time,东八区时间)
UTC 应该是指(Coordinated Universal Time,标准时间)

所以,这 2 个时间实际上应该相差 8 个小时。(PS:所以没有设置过的容器, 一般跟宿主机时间相差 8h),必须统一两者的时区。

#  共享主机的 localtime (方法一)
#  创建容器的时候指定启动参数,挂载 localtime 文件到容器内,保证两者所采用的时区是一致的。[root@centos8]# docker run --name  name  -v /etc/localtime:/etc/localtime:ro 

#  复制主机的 localtime (方法二) [root@centos8]# docker cp /etc/localtime [containerId]:/etc/localtime
#  创建自定义的 dockerfile (方法三) [root@centos8]# RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \  echo  Asia/Shanghai   /etc/timezone \

“怎么解决从 Mysql 数据库获取 timestamp 比正常时间早 8 小时问题”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!