1、時間服務器
用來給其他主機提供時間同步服務,在搭建服務器集群的時候,需要保證各個節點的時間是一致的,時間服務器不失為一個好的選擇。
2、安裝ntp服務器
[root@centos7 ~]# yum install ntp ntpdate -y
3、ntp服務器配置文件:/etc/ntp.conf
[root@centos7 ~]# cp /etc/ntp.conf{,.bak} [root@centos7 ~]# vim /etc/ntp.conf server 127.127.1.0 #以本機作為時間服務器 #server ip #也可以設置上游服務器ip做時間服務器,本機與其保持一致 fudge 127.127.1.0 startnum 10 #設置服務器層級 restrict 127.0.0.1 #允許本機使用這個時間服務器 restrict 10.220.5.0 netmask 255.255.255.0 #允許10.220.5.0/24網段的所有主機使用該時間服務器進行時間同步 driftfile /var/lib/ntp/ #記錄當前時間服務器,與上游服務器的時間差的文件 logfile /var/log/ntp/ntp.log #指定日志文件位置,需要手動創建
4、啟動服務
[root@centos7 ~]# systemctl start ntpd [root@centos7 ~]# systemctl enable ntpd Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
5、查看狀態
[root@centos7 ~]# ntpstat synchronised to NTP server (119.28.183.184) at stratum 3 time correct to within 73 ms polling server every 64 s #synchronised:表示時間同步完成(ntp可以正常工作了) #unsynchronised:表示時間同步尚未完成 # 或者用 ntpq -p查看狀態 [root@centos7 ~]#ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== +ntp8.flashdance 194.58.202.20 2 u 11 64 353 237.366 3.680 7.018 +ntp5.flashdance 192.36.143.150 2 u 27 64 171 233.809 7.221 8.689 *119.28.183.184 100.122.36.196 2 u 11 64 377 66.233 14.438 11.150 ntp.wdc1.us.lea 130.133.1.10 2 u 35m 64 0 312.951 -24.458 0.000
6、使用ntpdate [ntp-server ip]進行與ntp-server同步時間
常加入定時計划任務,與ntp時間服務器保持一致,或由ntpd服務定義上游時間服務器
crontab -e 10 06 * * * /usr/sbin/ntpdate ntp-server-ip >> /usr/local/crontab/ntpdate.log
工作中我們一般都是使用ntpdate+ntp來完成時間同步,因為單獨使用ntpdate同步時間雖然簡單快捷但是會導致時間不連續,而時間不連續在數據庫業務中影響是很大的,單獨使用ntp做時間同步時,當服務器與時間服務器相差大的時候則無法啟動ntpd來同步時間。由於ntpd做時間同步時是做的順滑同步(可以簡單理解為時間走得快,以便將落后的時間趕過來),所以同步到時間服務器的的時間不是瞬間完成的,開啟ntpd之后稍等三五分鍾就能完成時間同步。