千家信息网

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

发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,本篇内容介绍了"怎么解决从Mysql数据库获取timestamp比正常时间早8小时问题"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!
千家信息网最后更新 2024年09月22日怎么解决从Mysql数据库获取timestamp比正常时间早8小时问题

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

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

服务器环境:centos8,mysql8.0.21

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

(一)date 查看/设置系统时间1、将日期设置为2020年11月6日[root@centos7 ~]# date -s 11/06/202、将时间设置为11点12分13秒[root@centos7 ~]# date -s 11:12:133、将时间设置为2020年11月6日11点12分13秒(MMDDhhmmYYYY.ss)[root@centos7 ~]# date 1106111220.13(二)hwclock/clock 查看/设置硬件时间1、查看系统硬件时钟(以下两个一样效果)[root@centos7 ~]# hwclock  --show[root@centos7 ~]# clock  --show2、设置硬件时间(以下两个一样效果)[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%2B8spring:    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]# dateMon Nov 23 13:43:52 CST 2020# 容器时间[root@centos8]# docker exec e8573a89fb94 dateMon 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  -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小时问题"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0