1,调整数据库服务器时间
因为应用app摇一摇活动发现过时15分钟,还可以继续摇一摇,问题根源在于数据库服务器时间比应用服务器时间蛮了18分钟,而app取得是数据库的now(),所以才导致的。2个解决办法,1是修改程序代码,不通过数据库now()去取而是通过应用服务器取;2是直接修改数据库的时间。经过权衡之后采纳第二种方案。
1.1,通过date -s修改时间
修改linux服务器时间 date -s 2015-08-03 修改时间: date -s 10:18:00
1.2,使用ntpdate调整手动调整时间
安装ntpdate yum install -y ntpdate 手动调整时间(需要ntpdate) ntpdate us.pool.ntp.org
执行过程:
[root@testmysqldb ~]# ntpdate us.pool.ntp.org25 Sep 22:33:44 ntpdate[11419]: the NTP socket is in use, exiting [root@testmysqldb ~]# [root@testmysqldb ~]# service ntpd stop关闭 ntpd: [确定] [root@testmysqldb ~]# /usr/sbin/ntpdate 10.254.1.21;/sbin/clock --systohc25 Sep 22:48:12 ntpdate[11445]: step time server 10.254.1.21 offset 814.746911 sec [root@testmysqldb ~]#
1.3,写一个自动同步时间的脚本,加入定时计划任务,每天同步一次
vim /etc/cron.daily/ntpdate #! /bin/bash /usr/sbin/ntpdate -s us.pool.ntp.org /usr/sbin/hwclock --systohc 4,授予权限 # chmod +x /etc/cron.daily/ntpdate 加入crontab任务,每20分钟同步一次 [root@testmysqldb ~]# crontab -l */20 * * * * sh /etc/cron.daily/ntpdate [root@testmysqldb ~]#
原blog地址为:http:///,未经原作者mchdba允许,谢绝转载
2,Linux时间设置与同步
2.1 关于时间和时区
如果有人问你说现在几点? 你看了看表回答他说晚上8点了. 这样回答看上去没有什么问题,但是如果问你的这个人在欧洲的话那么你的回答就会让他很疑惑,因为他那里还太阳当空呢
这里就有产生了一个如何定义时间的问题. 因为在地球环绕太阳旋转的24个小时中,世界各地日出日落的时间是不一样的.所以我们才有划分时区(timezone) 的必要,也就是把全球划分成24个不同的时区. 所以我们可以把时间的定义理解为一个时间的值加上所在地的时区(注意这个所在地可以精确到城市)
地理课上我们都学过格林威治时间(GMT), 它也就是0时区时间. 但是我们在计算机中经常看到的是UTC. 它是Coordinated Universal Time的简写. 虽然可以认为UTC和GMT的值相等(误差相当之小),但是UTC已经被认定为是国际标准,所以我们都应该遵守标准只使用UTC
那么假如现在中国当地的时间是晚上8点的话,我们可以有下面两种表示方式
20:00 CST
12:00 UTC
这里的CST是Chinese Standard Time,也就是我们通常所说的北京时间了. 因为中国处在UTC+8时区,依次类推那么也就是12:00 UTC了.
第一,不管通过任何渠道我们想要同步系统的时间,通常提供方只会给出UTC+0的时间值而不会提供时区(因为它不知道你在哪里).所以当我们设置系统时间的时候,设置好时区是首先要做的工作
第二,很多国家都有夏令时(中国2010年的时候设置过一次),那就是在一年当中的某一天时钟拨快一小时(比如从UTC+8一下变成UTC+9了),那么同理到时候还要再拨慢回来.如果我们设置了正确的时区,当需要改变时间的时候系统就会自动替我们调整
现在我们就来看一下如何在Linux下设置时区,也就是time zone
2.2 设置Linux Time Zone
在Linux下glibc提供了事先编译好的许多timezone文件, 他们就放在/usr/share/zoneinfo这个目录下,这里基本涵盖了大部分的国家和城市
[root@wgq_idc_mon_1_12 ~]# ls -F /usr/share/zoneinfo/Africa/ Asia/ Canada/ Cuba EST GB GMT-0 HST iso3166.tab Kwajalein Mideast/ NZ Portugal PST8PDT Singapore US/ zone.tabAmerica/ Atlantic/ CET EET EST5EDT GB-Eire GMT+0 Iceland Israel Libya MST NZ-CHAT posix/ right/ Turkey UTC ZuluAntarctica/ Australia/ Chile/ Egypt Etc/ GMT Greenwich Indian/ Jamaica MET MST7MDT Pacific/ posixrules ROC UCT WETArctic/ Brazil/ CST6CDT Eire Europe/ GMT0 Hongkong Iran Japan Mexico/ Navajo Poland PRC ROK Universal W-SU[root@wgq_idc_mon_1_12 ~]# [root@wgq_idc_mon_1_12 ~]# ll /usr/share/zoneinfo/Asia/Shanghai-rw-r--r--. 2 root root 405 1月 23 2013 /usr/share/zoneinfo/Asia/Shanghai[root@wgq_idc_mon_1_12 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimecp:是否覆盖"/etc/localtime"? y [root@wgq_idc_mon_1_12 ~]# date2015年 09月 26日 星期六 12:12:20 CST[root@wgq_idc_mon_1_12 ~]#
去目录:/usr/share/zoneinfo/Asia/,看到有许多城市地区的名字,找到上海的为Shanghai,然后可以用zdump来check下,时间正常,如下所示:
[root@wgq_idc_mon_1_12 ~]# zdump /usr/share/zoneinfo/Asia/Shanghai/usr/share/zoneinfo/Asia/Shanghai Sat Sep 26 13:11:23 2015 CST[root@wgq_idc_mon_1_12 ~]#
那么我们又怎么来告诉系统我们所在time zone是哪个呢? 方法有很多,这里举出两种
第一个就是修改/etc/localtime这个文件,这个文件定义了我么所在的local time zone.
[root@wgq_idc_mon_1_12 ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtimecp:是否覆盖"/etc/localtime"? y [root@wgq_idc_mon_1_12 ~]# date2015年 09月 26日 星期六 13:19:00 CST[root@wgq_idc_mon_1_12 ~]#
第二种方法也就设置TZ环境变量的值. 许多程序和命令都会用到这个变量的值. TZ的值可以有多种格式,最简单的设置方法就是使用tzselect命令,如下所示:
[root@wgq_idc_mon_1_12 ~]# tzselectPlease identify a location so that time zone rules can be set correctly. Please select a continent or ocean. 1) Africa 2) Americas 3) Antarctica 4) Arctic Ocean 5) Asia 6) Atlantic Ocean 7) Australia 8) Europe 9) Indian Ocean10) Pacific Ocean11) none - I want to specify the time zone using the Posix TZ format.#? 5Please select a country. 1) Afghanistan 18) Israel 35) Palestine 2) Armenia 19) Japan 36) Philippines 3) Azerbaijan 20) Jordan 37) Qatar 4) Bahrain 21) Kazakhstan 38) Russia 5) Bangladesh 22) Korea (North) 39) Saudi Arabia 6) Bhutan 23) Korea (South) 40) Singapore 7) Brunei 24) Kuwait 41) Sri Lanka 8) Cambodia 25) Kyrgyzstan 42) Syria 9) China 26) Laos 43) Taiwan10) Cyprus 27) Lebanon 44) Tajikistan11) East Timor 28) Macau 45) Thailand12) Georgia 29) Malaysia 46) Turkmenistan13) Hong Kong 30) Mongolia 47) United Arab Emirates14) India 31) Myanmar (Burma) 48) Uzbekistan15) Indonesia 32) Nepal 49) Vietnam16) Iran 33) Oman 50) Yemen17) Iraq 34) Pakistan#? 9Please select one of the following time zone regions.1) east China - Beijing, Guangdong, Shanghai, etc.2) Heilongjiang (except Mohe), Jilin3) central China - Sichuan, Yunnan, Guangxi, Shaanxi, Guizhou, etc.4) most of Tibet & Xinjiang5) west Tibet & Xinjiang#? 1The following information has been given: China east China - Beijing, Guangdong, Shanghai, etc. Therefore TZ='Asia/Shanghai' will be used. Local time is now: Sat Sep 26 13:21:14 CST 2015.Universal Time is now: Sat Sep 26 05:21:14 UTC 2015.Is the above information OK?1) Yes2) No#? yesPlease enter 1 for Yes, or 2 for No.#? 1You can make this change permanent for yourself by appending the line TZ='Asia/Shanghai'; export TZto the file '.profile' in your home directory; then log out and log in again. Here is that TZ value again, this time on standard output so that you can use the /usr/bin/tzselect command in shell scripts: Asia/Shanghai [root@wgq_idc_mon_1_12 ~]# [root@wgq_idc_mon_1_12 ~]# more /etc/localtime TZif2 CST-8[root@wgq_idc_mon_1_12 ~]#
TZ变量的值会override /etc/localtime. 也就是说当TZ变量没有定义的时候系统才使用/etc/localtime来确定time zone. 所以你想永久修改time zone的话那么可以把TZ变量的设置写入/etc/profile里
2.3 Read Time Clock(RTC) and System Clock
说道设置时间这里还要明确另外一个概念就是在一台计算机上我们有两个时钟:一个称之为硬