Files
oceanbase/docs/docs-cn/4.installation-and-deployment/6.optional-configuring-clock-sources.md
2022-02-10 14:51:49 +08:00

3.9 KiB

(可选)配置时钟源

本文介绍如何为集群内的多台机器配置 NTP 时钟源。如果您在单机安装 OceanBase 数据库,或者做个人测试,则无需设置。 如果您使用集群安装 OceanBase,则需要保证集群内各机器的时间同步。否则集群无法启动,服务在运行时也会出现异常。如果您已配置 NTP 时钟同步,则无需重新配置。 OceanBase 集群中的服务器时间必须保持一致,否则会导致 OceanBase 集群无法启动,运行时也会出现故障。物理机与时钟服务器的误差在 50ms 以下可认为时钟是同步状态,OceanBase 集群最大容忍误差不能超过 200ms。当超过 200ms 时,会出现无主情况。恢复时钟同步后。重启 OceanBase 集群, 可以恢复正常。

前提条件

配置 NTP 时钟源前,确保您拥有所有机器的 root 用户权限。

操作步骤

按以下步骤配置 NTP 时钟同步:

  1. 在每台机器上执行以下命令安装 NTP:

    yum install ntp ntpdate -y
    
  2. 运行以下命令查看服务器的 NTP 连接:

    当物理机与时钟服务器的时间误差在 50ms 以内时,系统即认为时钟是同步的。

    [root@centos-01 /root]#ntpq -4p
    remote refid st t when poll reach delay offset jitter
    ==============================================================================
    *time6.aliyun.co 10.137.38.86 2 u 712 1024 377 21.951 4.253 4.208
    
  3. (可选)开启 NTP 服务端服务

    如果您已经有了 NTP 服务端服务器,则可跳过此步骤。本文档使用目标机器 10.244.0.114 作为 NTP 服务端服务器。

    1. 编辑配置文件。

      以 root 用户登录 NTP 服务端服务器,运行以下命令编辑配置文件:

      vi /etc/ntp.conf
      

      在配置文件中添加以下内容:

      server <yout_ntp_server_ip>
      

      保存并关闭配置文件。

    2. 运行以下命令重启 NTP 服务端服务:

      service ntpd restart
      
    3. 运行以下命令查看 NTP 服务端服务是否启动成功:

      ps -ef | grep -i ntpd
      
    4. 运行以下命令设置 NTP 服务开机自启动:

      chkconfig ntpd on
      
  4. 开启 NTP 客户端服务。

    注意

    NTP 服务端服务启动后,您需要等待 5 分钟再开启 NTP 客户端服务。否则系统会提示 "no server suitable for synchronization found"。

    1. 使用 root 用户登录 OceanBase 数据库。

    2. 运行以下命令停止 NTP 服务:

      service ntpd stop
      
    3. 运行以下命令同步 NTP 服务端时间:

      ntpdate <yout_ntp_server_ip>
      
    4. 编辑配置文件。

      运行以下命令打开配置文件:

      vi /etc/ntp.conf
      

      在配置文件中添加以下内容:

      server <yout_ntp_server_ip>
      
    5. 运行以下命令,启动 NTP 服务:

      service ntpd start
      
  5. 运行以下命令验证配置是否成功:

    ntpstat
    

    返回以下结果:

    synchronised to NTP server (10.143.0.44) at stratum 3
    time correct to within 2 ms
    polling server every 64 s
    
    timedatectl
    

    返回以下结果:

          Local time: Thu 2021-04-22 11:02:32 CST
      Universal time: Thu 2021-04-22 03:02:32 UTC
            RTC time: Thu 2021-04-22 11:02:32
           Time zone: Asia/Shanghai (CST, +0800)
         NTP enabled: yes
    NTP synchronized: yes
     RTC in local TZ: yes
          DST active: n/a
    

    说明 NTP 服务生效。