首页 > 数据库 >【转载】How to solve the problem that getting timestamp from Mysql database is 8 hours earlier than the

【转载】How to solve the problem that getting timestamp from Mysql database is 8 hours earlier than the

时间:2023-10-17 11:12:19浏览次数:52  
标签:zone getting timestamp earlier system centos7 time root localtime

This article introduces the relevant knowledge of "how to solve the problem of obtaining timestamp from Mysql database 8 hours earlier than the normal time". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and achieve something!

The problem is: using the same Mysql database to obtain the same time data, the return page under window is the normal time, but it is 8 hours earlier under linux.

Server environment: CentOS 8, mysql8.0.21

1, First, check the server time zone setting and the synchronization of system clock and hardware clock:

((I) date see/Set system time

1,Set the date to November 6, 2020
[root@centos7 ~]# date -s 11/06/20
2,Set the time to 11:12:13
[root@centos7 ~]# date -s 11:12:13
3,Set the time to 11:12:13 on November 6, 2020( MMDDhhmmYYYY.ss)
[root@centos7 ~]# date 1106111220.13

((II) hwclock/clock see/Set hardware time

1,View the system hardware clock (the following two have the same effect)
[root@centos7 ~]# hwclock  --show
[root@centos7 ~]# clock  --show
2,Set the hardware time (the following two have the same effect)
[root@centos7 ~]# Hwlock -- set -- date = "11 / 06 / 20 12:13" (month / day / year hour: minute: Second)
[root@centos7 ~]# clock --set --date="11/06/20 12:13" (month / day / year hour: minute: Second)

(3) Synchronization system and hardware clock

1,System time is synchronized with hardware time (the following two have the same effect)
[root@centos7 ~]# hwclock --hctosys
[root@centos7 ~]# clock --hctosys  
remarks: hc Represents hardware time, sys Represents the system time. Based on the hardware time, the system time is synchronized with the hardware time
2,Hardware time is synchronized with system time (the following two have the same effect)
[root@centos7 ~]# hwclock --systohc
[root@centos7 ~]# clock --systohc 
Note: Based on the system time, the hardware time is synchronized with the system time

(4) Modify time zone

#The time zone file of CentOS and Ubuntu is / etc/localtime, but after CentOS7, localtime and has become a linked file
[root@centos7 ~]# ll /etc/localtime 
lrwxrwxrwx 1 root root 33 Nov 15  2020 /etc/localtime -> /usr/share/zoneinfo/Asia/Shanghai

# There are several ways to correct errors:
[root@centos7 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
# The best way is to use the timedatectl command
[root@centos7 ~]# timedatectl list-timezones |grep Shanghai    #Find the full name of the Chinese time zone
[root@centos7 ~]# timedatectl set-timezone Asia/Shanghai    #Other time zones, and so on
# Or create soft links directly and manually
[root@centos7 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

2, If it is determined that there is a problem with the server, but it cannot be modified due to system region and other reasons, you can also set the time zone of 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 indicates the time zone of mysql system
system_time_zone description system uses EST time zone (PS: EST us time zone, CST world standard world)

#Temporary modification, executed in mysql
>set global time_zone = '+8:00'; ##Modify the mysql global time zone to Beijing time, that is, the East 8 area where we are located
>set time_zone = '+8:00'; ##Modify the current session time zone
>flush privileges; #Effective immediately

#Permanently modify and exit mysql execution
[root@centos7 ~]# vim /etc/my.cnf ##In the [mysqld] field, add
[root@centos7 ~]# default-time_zone = '+8:00'
[root@centos7 ~]# /etc/init.d/mysqld restart ##Restart mysql to make the new time zone take effect

3, If it is determined that there is a problem with the server, but the server and database cannot be modified due to objective factors, it can also be modified on the database request URL:

application.yml Configuration: (system database parameter configuration file, GMT%2B8 After this parameter is escaped GMT+8 Indicates that the database time is set to East Zone 8(Beijing)Time, if set GMT,Can be in Spring.jackson.time-zone Medium setting GMT+8,Just set one place)

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

4, There are also configurations with larger brain holes. Notes should be made for each value, which is easy to be omitted:

# Set on the Date of the entity class Po class to receive the time field in the database:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")

After a wave of changes, return to the topic: Docker container time is inconsistent with the host time, and Docker is making trouble again!

# Host time
[root@centos8]# date
Mon Nov 23 13:43:52 CST 2020

# Container time
[root@centos8]# docker exec e8573a89fb94 date
Mon Nov 23 05:44:39 UTC 2020

CST should mean (China Shanghai Time)
UTC should mean Coordinated Universal Time

Therefore, the difference between these two times should actually be 8 hours. (PS: therefore, for containers that have not been set, the time difference between them and the host computer is generally 8h). The time zones of the two must be unified.

# localtime of shared host (method 1)
# When creating a container, specify the startup parameters and mount the localtime file into the container to ensure that the time zones used by the two are consistent.
[root@centos8]# docker run --name <name> -v /etc/localtime:/etc/localtime:ro 


# Copy the localtime of the host (method 2)
[root@centos8]# docker cp /etc/localtime [containerId]:/etc/localtime


# Create a custom dockerfile (method 3)
[root@centos8]# RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo 'Asia/Shanghai' >/etc/timezone \

"How to solve the problem of obtaining timestamp from Mysql database 8 hours earlier than the normal time" is introduced here. Thank you for reading! Xiaobian will output more high-quality practical articles for you!

文章来自 https://programming.vip/docs/61abc69ead126.html

标签:zone,getting,timestamp,earlier,system,centos7,time,root,localtime
From: https://www.cnblogs.com/airven/p/17769230.html

相关文章

  • 关于Date、LocalDate、LocalDateTime、Timestamp等时间类型的区别?
    最近在代码的开发过程中发现,小组内对于实体类中的时间字段。有的用Date,有的用Timestamp,有的又用LocalDateTime,于是我就想整理一下这些时间类型有什么区别,是否可以统一?1、Date(不推荐)Date类型是Java8之前的时间处理类,存在一些问题比如说非线程安全问题。时区的处理比较麻烦等。Da......
  • PowerShell IDE - PowerShellPlus(1)- Getting Started功能介绍
    我们对PowerShellIDE之PowerShellPlus的总体印象已经有了,如下图:从本篇开始,我们对该IDE为我们提供的一些快捷Function和Script进行了解和掌握,从而促进我们更好的学习和掌握PowerShell的Cmdlet和Function。从图中我们可以看到GettingStarted分为6大部分(我的电脑、我的桌面、我的硬......
  • mysql 中 timestampdiff 一个让人费解的问题
    取当前时间 使用上面取到的时间与now()使用timestampdiff进行month级别比较 将上面取到的日期递增一个月再次比较 结果让人很费解 在此给自己提个醒,mysql单纯使用日期还是用 current_date吧......
  • kubelet.go 2466 Error getting node not found
    kubeadm初始化kubernetes集群报错,kubelet服务提示:kubelet.go:2466]"Errorgettingnode"err="node\"k8s3-master\"notfound处理记录。  0.ENV kubernetes1.22.x/1.23.x/1.24.x(不限于所列版本)CentOS7.x/Ubuntu22.04(不限于所列版本)  1.问题现象 kube......
  • 后端传递Timestamp类型时间前端自定义接收格式
    Vue项目中处理后端返回日期字符串在这个Vue项目中,后端接口RentalQueryAllServlet返回的租车记录数据中,有一个rentalTime字段,其值是日期字符串,如:"Sep27,20239:23:40AM"。1.获取数据组件中使用axios调用接口获取数据: js methods:{fetchData(){axio......
  • Docker - ERROR: failed to solve: golang:latest: error getting credentials - err:
    Dockerfile:FROMgolang:latestWORKDIR/appADD..RUNgoenv-wGOPROXY=https://goproxy.io,directRUNgogetRUNgobuild-oapp.CMD["/app/app"] zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$dockerbuild-ttest:v1.[+]Building1.3s(3/3)FINI......
  • oracle数据库使用to_timestamp格式化日期数据时,报错: ORA-01821: date format not reco
    今天偶然发现一个问题:我使用的数据库是11.2版本的sql语句:SELECTto_timestamp('2023-09-1315:43:29.943','yyyy-mm-ddhh24:mi:ss.fff')ASmydataFROMdual就会报错,项目出现问题,但很神奇的时使用oracle数据库12版本的就不会报错。 网上查了下,说是毫秒处是6位的,但只显示3位......
  • Python - unix timestamp 时间戳转换错误
    用python的时间转换函数,结果报错。想着这么基础的怎么会报错呢。fromdatetimeimportdatetime#timestampisnumberofsecondssince1970-01-01timestamp=1545730073#convertthetimestamptoadatetimeobjectinthelocaltimezonedt_object=datetime.from......
  • java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.lang.Str
    这个问题来自于想把从数据库查询的数据转化为字符串,方便后面做时间比较,显示格式转化错误 sql改造部分 as的左边为我的sql语句语法使用如下DATE_FORMAT((sql语句),'%Y-%m-%d%H:%i:%s')如果是涉及时间的计算,可以考虑如下方式BigDecimala=(BigDecimal)sprint......
  • MySQL将timestamp默认值设置为null
    问题以前MySQL创建表的的时候,timestamp类型的字段可以不加默认值。createTimetimestampCOMMENT'创建时间',这样创建是没有问题的。但是现在却报错,“Invaliddefaultvaluefor‘createTime’”问题原因:因为MySQL5.6以后timestamp设定默认值规则改变,不能为000000-0000:00:00......