新聞中心
本篇內(nèi)容介紹了“怎么解決從MySQL數(shù)據(jù)庫獲取timestamp比正常時間早8小時問題”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
目前創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、綿陽服務器托管、企業(yè)網(wǎng)站設(shè)計、衢江網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
問題是:使用同一Mysql數(shù)據(jù)庫,獲取相同時間數(shù)據(jù),window下返回頁面是正常時間,但在linux下卻早了8小時。
服務器環(huán)境:centos8,mysql8.0.21
一、首先排查服務器時區(qū)設(shè)置及系統(tǒng)時鐘和硬件時鐘的同步:
(一)date 查看/設(shè)置系統(tǒng)時間 1、將日期設(shè)置為2020年11月6日 [root@centos7 ~]# date -s 11/06/20 2、將時間設(shè)置為11點12分13秒 [root@centos7 ~]# date -s 11:12:13 3、將時間設(shè)置為2020年11月6日11點12分13秒(MMDDhhmmYYYY.ss) [root@centos7 ~]# date 1106111220.13 (二)hwclock/clock 查看/設(shè)置硬件時間 1、查看系統(tǒng)硬件時鐘(以下兩個一樣效果) [root@centos7 ~]# hwclock --show [root@centos7 ~]# clock --show 2、設(shè)置硬件時間(以下兩個一樣效果) [root@centos7 ~]# hwclock --set --date="11/06/20 12:13" (月/日/年時:分:秒) [root@centos7 ~]# clock --set --date="11/06/20 12:13" (月/日/年時:分:秒) (三)同步系統(tǒng)及硬件時鐘 1、系統(tǒng)時間找硬件時間同步(以下兩個一樣效果) [root@centos7 ~]# hwclock --hctosys [root@centos7 ~]# clock --hctosys 備注:hc代表硬件時間,sys代表系統(tǒng)時間,以硬件時間為基準,系統(tǒng)時間找硬件時間同步 2、硬件時間找系統(tǒng)時間同步(以下兩個一樣效果) [root@centos7 ~]# hwclock --systohc [root@centos7 ~]# clock --systohc 備注:以系統(tǒng)時間為基準,硬件時間找系統(tǒng)時間同步 (四)修改時區(qū) #CentOS和Ubuntu的時區(qū)文件是/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 #查找中國時區(qū)的完整名稱 [root@centos7 ~]# timedatectl set-timezone Asia/Shanghai #其他時區(qū)以此類推 # 或者直接手動創(chuàng)建軟鏈接 [root@centos7 ~]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
二、如果確定服務器存在問題,卻又因系統(tǒng)地域等原因無法修改,也可以設(shè)置mysql的時區(qū):
>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的時區(qū)
system_time_zone說明system使用EST時區(qū) ( PS:EST美國時區(qū),CST世界標準世界)
#臨時修改,在mysql中執(zhí)行 >set global time_zone = '+8:00'; ##修改mysql全局時區(qū)為北京時間,即我們所在的東8區(qū) >set time_zone = '+8:00'; ##修改當前會話時區(qū) >flush privileges; #立即生效 #永久修改,退出mysql執(zhí)行 [root@centos7 ~]# vim /etc/my.cnf ##在[mysqld]區(qū)域中加上 [root@centos7 ~]# default-time_zone = '+8:00' [root@centos7 ~]# /etc/init.d/mysqld restart ##重啟mysql使新時區(qū)生效
三、如果確定服務器存在問題,卻又因客觀因素無法修改服務器和數(shù)據(jù)庫,也可以在數(shù)據(jù)庫請求URL上修改:
application.yml配置:(系統(tǒng)數(shù)據(jù)庫參數(shù)配置文件,GMT%2B8這個參數(shù)轉(zhuǎn)義后GMT+8表示設(shè)置數(shù)據(jù)庫時間為東八區(qū)(北京)時間,如果設(shè)置GMT,可以在Spring.jackson.time-zone中設(shè)置GMT+8,設(shè)置一處就可以) 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上設(shè)置,來接收數(shù)據(jù)庫中的時間字段: @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,東八區(qū)時間)
UTC應該是指(Coordinated Universal Time,標準時間)
所以,這2個時間實際上應該相差8個小時。(PS:所以沒有設(shè)置過的容器, 一般跟宿主機時間相差8h),必須統(tǒng)一兩者的時區(qū)。
# 共享主機的localtime (方法一) # 創(chuàng)建容器的時候指定啟動參數(shù),掛載localtime文件到容器內(nèi),保證兩者所采用的時區(qū)是一致的。 [root@centos8]# docker run --name-v /etc/localtime:/etc/localtime:ro # 復制主機的localtime (方法二) [root@centos8]# docker cp /etc/localtime [containerId]:/etc/localtime # 創(chuàng)建自定義的dockerfile (方法三) [root@centos8]# RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ && echo 'Asia/Shanghai' >/etc/timezone \
“怎么解決從Mysql數(shù)據(jù)庫獲取timestamp比正常時間早8小時問題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
文章標題:怎么解決從Mysql數(shù)據(jù)庫獲取timestamp比正常時間早8小時問題
新聞來源:http://fisionsoft.com.cn/article/phogoj.html