1.离线设置集群时钟同步
服务器环境:ubuntu 18.04
1.1安装ntp
下载ntp包:
链接:https://pan.baidu.com/s/19jQmhvhuDrpuiN0zOlZw-Q?pwd=ojrt
解压位置:/var/cache/apt/archives
sudo apt-get install ntp (可以跳过)
dpkg -i /var/cache/apt/archives/*.deb
修改配置文件:(依据具体情况修改ip)
服务器端(192.168.137.13):
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help driftfile /var/lib/ntp/ntp.drift # Leap seconds definition provided by tzdata leapfile /usr/share/zoneinfo/leap-seconds.list # Enable this if you want statistics to be logged. #statsdir /var/log/ntpstats/ statistics loopstats peerstats clockstats filegen loopstats file loopstats type day enable filegen peerstats file peerstats type day enable filegen clockstats file clockstats type day enable # Specify one or more NTP servers. # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for # more information. #pool 0.ubuntu.pool.ntp.org iburst #pool 1.ubuntu.pool.ntp.org iburst #pool 2.ubuntu.pool.ntp.org iburst #pool 3.ubuntu.pool.ntp.org iburst server 0.cn.pool.ntp.org server 1.cn.pool.ntp.org server 2.cn.pool.ntp.org server 3.cn.pool.ntp.org # 允许上层时间服务器主动修改本机时间 restrict 0.cn.pool.ntp.org nomodify notrap noquery restrict 1.cn.pool.ntp.org nomodify notrap noquery restrict 2.cn.pool.ntp.org nomodify notrap noquery restrict 3.cn.pool.ntp.org nomodify notrap noquery # Use Ubuntu's ntp server as a fallback. #pool ntp.ubuntu.com # 外部时间服务器不可用时,以本地时间作为时间服务 server 127.127.1.0 fudge 127.127.1.0 stratum 10 # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery limited restrict -6 default kod notrap nomodify nopeer noquery limited # Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1 # Needed for adding pool entries restrict source notrap nomodify noquery # Clients from this (example!) subnet have unlimited access, but only if # cryptographically authenticated. #restrict 192.168.10.245 mask 255.255.0.0 notrust restrict 192.168.10.245 mask 255.255.0.0 # If you want to provide time to your local subnet, change the next line. # (Again, the address is an example only.) #broadcast 192.168.123.255 # If you want to listen to time broadcasts on your local subnet, de-comment the # next lines. Please do this only if you trust everybody on the network! #disable auth #broadcastclient #Changes recquired to use pps synchonisation as explained in documentation: #http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918 #server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS #fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware #server 127.127.22.1 # ATOM(PPS) #fudge 127.127.22.1 flag3 1 # enable PPS API
客户端:
# /etc/ntp.conf, configuration for ntpd; see ntp.conf(5) for help driftfile /var/lib/ntp/ntp.drift # Leap seconds definition provided by tzdata leapfile /usr/share/zoneinfo/leap-seconds.list # Enable this if you want statistics to be logged. #statsdir /var/log/ntpstats/ statistics loopstats peerstats clockstats filegen loopstats file loopstats type day enable filegen peerstats file peerstats type day enable filegen clockstats file clockstats type day enable # Specify one or more NTP servers. # Use servers from the NTP Pool Project. Approved by Ubuntu Technical Board # on 2011-02-08 (LP: #104525). See http://www.pool.ntp.org/join.html for # more information. #pool 0.ubuntu.pool.ntp.org iburst #pool 1.ubuntu.pool.ntp.org iburst #pool 2.ubuntu.pool.ntp.org iburst #pool 3.ubuntu.pool.ntp.org iburst server 192.168.137.13 restrict 192.168.137.13 nomodify notrap noquery server 127.127.1.0 fudge 127.127.1.0 stratum 10 # Use Ubuntu's ntp server as a fallback. #pool ntp.ubuntu.com # Access control configuration; see /usr/share/doc/ntp-doc/html/accopt.html for # details. The web page <http://support.ntp.org/bin/view/Support/AccessRestrictions> # might also be helpful. # # Note that "restrict" applies to both servers and clients, so a configuration # that might be intended to block requests from certain clients could also end # up blocking replies from your own upstream servers. # By default, exchange time with everybody, but don't allow configuration. restrict -4 default kod notrap nomodify nopeer noquery limited restrict -6 default kod notrap nomodify nopeer noquery limited # Local users may interrogate the ntp server more closely. restrict 127.0.0.1 restrict ::1 #restrict 192.168.137.13 nomodify notrap noquery # Needed for adding pool entries restrict source notrap nomodify noquery # Clients from this (example!) subnet have unlimited access, but only if # cryptographically authenticated. #restrict 192.168.123.0 mask 255.255.255.0 notrust # If you want to provide time to your local subnet, change the next line. # (Again, the address is an example only.) #broadcast 192.168.123.255 # If you want to listen to time broadcasts on your local subnet, de-comment the # next lines. Please do this only if you trust everybody on the network! #disable auth #broadcastclient #Changes recquired to use pps synchonisation as explained in documentation: #http://www.ntp.org/ntpfaq/NTP-s-config-adv.htm#AEN3918 #server 127.127.8.1 mode 135 prefer # Meinberg GPS167 with PPS #fudge 127.127.8.1 time1 0.0042 # relative to PPS for my hardware #server 127.127.22.1 # ATOM(PPS) #fudge 127.127.22.1 flag3 1 # enable PPS API # restrict 127.0.0.1 server 192.168.137.13 prefer
1.2设置ntp
服务端:
一般安装后,就不需要其他操作
使用ntpq -p可以查看同步服务器关系
启动ntp服务:
systemctl start ntp
systemctl status ntp
客户端:
停止ntp:
systemctl stop ntp
同步时钟:ntpdate 192.168.137.13
启动ntp:
systemctl start ntp
使用ntpq -p查看同步关系:
参数refid表示,祖先时钟服务器
2.设置服务器使用东八区时间
mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
为/usr/share/zoneinfo/Asia/Shanghai建立软连接,名字为/etc/localtime
3.数据库设置东八区时间
检查数据库时区
> show variables like "%time_zone%"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | SYSTEM | +------------------+--------+
time_zone说明MySQL使用system的时区,system_time_zone说明system使用CST时区
- 在线设置东八区
set global time_zone='+8:00'; set time_zone='+8:00'; flush privileges;
设置之后:
- 配置文件设置东八区
# vim /etc/my.cnf ##在[mysqld]区域中加上 default-time_zone = '+8:00'
重启mysql
以上选取一种设置
最后,mysql连接信息增加:serverTimezone=Asia/Shanghai
4.docker容器时区不正确
docker exec -it <container> /bin/bash ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
重启容器
或者运行容器时,加上挂载参数
docker run -d <container> -v /etc/timezone:/etc/timezone -v /etc/localtime:/etc/localtime
5.修改Java虚拟机时间
java -jar -Duser.timezone=GMT+08 xxx.jar