同網段20幾台服務器:
其中有一組mysql 集群中 互為主從
選一台mysql master 作為時間同步的服務器,這樣做的好處以便於這台down了 另一個與他互為主從的master 繼續提供時間同步服務。
假如 這個mysql 集群 ip是這樣分配
master1 :10.0.0.11
master2 :10.0.0.12
其他 。。。。。省略
vip: 10.0.0.20
- 選擇一台服務器作為時間服務器。(master1做為時間服務器)
- 使用root用戶,查看服務器是否安裝ntp服務
rpm -qa|grep ntp
timedatectl set-timezone Asia/Shanghai沒有安裝的話使用yum install進行安裝。
- 修改文件 /etc/ntp.conf,一共修改三處內容:
- ①將#去掉。並且將網段修改正確。
restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
- ②將以下4個server進行#注釋掉
#server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst
server ntp1.aliyun.com
server ntp2.aliyun.com
server ntp3.aliyun.com
server ntp4.aliyun.com
server ntp5.aliyun.com
server ntp6.aliyun.com
server ntp7.aliyun.com -
③最后添加倆句話:
server 127.127.1.0 #local clock fudge 127.127.1.0 stratum 10
- 編輯etc/sysconfig/ntpd文件
# Drop root to id 'ntp:ntp' by default. SYNC_HWCLOCK=yes OPTIONS="-u ntp:ntp -p /var/run/ntpd.pid -g"
-
啟動ntpd服務,並且設置開機啟動
systemctl start ntpd.service systemctl enable ntpd.service
- 新建同步 master1 服務器的腳本。(每隔5分鍾同步一次maste1服務器)
[root@slave2 ~]# cat /opt/script/ntp.sh #!/bin/bash while true do /usr/sbin/ntpdate 10.0.0.20 &> /dev/null sleep 300 done
centos7 的開機自啟動文件默認沒有執行權限 需要添加以下,並設置腳本開機自啟動
chmod +x /etc/rc.d/rc.local chmod +x /opt/script/ntp.sh
vi /etc/rc.d/rc.local #加入下面的命令
/opt/script/ntp.sh &
10.重啟后查看 腳本進程是否在后台運行着
這里基本就完成了,有個地方要說下,一開始准備用定時任務執行命令的,但是不知道這centos7 有什么毛病,死活不執行,索性用個死循環腳本放后台執行吧 ,並設置開機自啟動腳本。