Linux叢集:ntp時間伺服器同步&&資料庫時間同步
阿新 • • 發佈:2018-12-15
-
autogen、ntp、ntpdata 安裝 與配置
- 注:使用rmp -vih 包名 安裝 錯誤時,使用 yum install -y 包名
[[email protected] opt]# rpm -ivh autogen-libopts-5.18-5.el7.x86_64.rpm warning: autogen-libopts-5.18-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:autogen-libopts-5.18-5.el7 ################################# [100%] [
- server端配置:(指定一個作為時間伺服器)
# ntp server 10.121.2.106:時間伺服器 [[email protected] opt]# vi /etc/ntp.conf restrict 10.121.2.106 mask 255.255.255.0 nomodify notrap # 允許內網其他機器同步時間 server 127.127.1.0 # 外部時間伺服器不可用時,以本地時間作為時間服務 fudge 127.127.1.0 stratum 10
- server端啟動+測試:
[[email protected] opt]# service ntpd restart Redirecting to /bin/systemctl restart ntpd.service [[email protected] opt]# systemctl disable chronyd.service [[email protected] opt]# systemctl enable ntpd.service # 設定開機啟動 [[email protected] opt]# ntpstat synchronised to local net at stratum 11 time correct to within 7948 ms polling server every 64 s [[email protected] opt]# systemctl status ntpd.service ● ntpd.service - Network Time Service Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled) Active: active (running) since Fri 2018-07-06 09:18:49 CST; 22s ago Process: 12428 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS) Main PID: 12429 (ntpd) CGroup: /system.slice/ntpd.service └─12429 /usr/sbin/ntpd -u ntp:ntp -g Jul 06 09:18:49 adrmbda106 ntpd[12429]: Listen and drop on 1 v6wildcard :: UDP 123 Jul 06 09:18:49 adrmbda106 ntpd[12429]: Listen normally on 2 lo 127.0.0.1 UDP 123 Jul 06 09:18:49 adrmbda106 ntpd[12429]: Listen normally on 3 bond0 10.121.2.106 UDP 123 Jul 06 09:18:49 adrmbda106 ntpd[12429]: Listen normally on 4 lo ::1 UDP 123 Jul 06 09:18:49 adrmbda106 ntpd[12429]: Listen normally on 5 bond0 fe80::92ec:baff:fe20:e0f2 UDP 123 Jul 06 09:18:49 adrmbda106 ntpd[12429]: Listening on routing socket on fd #22 for interface updates Jul 06 09:18:49 adrmbda106 ntpd[12429]: 0.0.0.0 c016 06 restart Jul 06 09:18:49 adrmbda106 ntpd[12429]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM Jul 06 09:18:49 adrmbda106 ntpd[12429]: 0.0.0.0 c011 01 freq_not_set Jul 06 09:18:50 adrmbda106 ntpd[12429]: 0.0.0.0 c514 04 freq_mode
- 配置client端 (可以有多個clent端、但是隻能指引一個時間伺服器)
# ntp client 96-105 : 105為其中一個需要同步的客戶端:
[[email protected] opt]# vi /etc/ntp.conf
server 10.121.2.106 #<--該IP是NTP主機端的IP
restrict 10.121.2.106 nomodify notrap noquery # 允許上層時間伺服器主動修改本機時間
[[email protected] opt]# ntpdate -u 10.121.2.106 # 手動同步主機端,免得本地時間與主機端相差太大導致無法同步。
6 Jul 03:15:45 ntpdate[3107]: adjust time server 10.121.2.106 offset -0.050032 sec
[[email protected] opt]# service ntpd restart
Redirecting to /bin/systemctl restart ntpd.service
[[email protected] opt]# systemctl disable chronyd.service
[[email protected] opt]# systemctl enable ntpd.service # 設定開機啟動
[[email protected] opt]# ntpstat
synchronised to NTP server (10.121.2.106) at stratum 12
time correct to within 8415 ms
polling server every 64 s
注:
1.此定時任務 將 ntp-server 端ip 時間 同步 到 ntp-client 系統時間中。
2.hwclock --systohc 系統時間 寫入 硬體時鐘 (需要手動執行一次)
命令:crontab -e
加入:*/10 * * * * /usr/sbin/ntpdate -u 10.121.2.106 >/dev/null 2>&1
儲存 重啟生效
service crond restart
-
資料庫時間與ntp時間同步
注:資料庫時間 使用的 作業系統時間
##查詢資料庫時間
select now()
##顯示資料預設時區:#time_zone說明mysql使用system的時區,system_time_zone說明system使用CST時區
show variables like "%time_zone%";
##修改mysql全域性時區為北京時間,即我們所在的東8區
set global time_zone = '+8:00';
##修改當前會話時區
set time_zone = '+8:00';
##立即生效
flush privileges;