Linux explores the influence of system time on database

00. Background

I encountered some problems related to ntp time synchronization when working recently:

  • The impact of ntp service on database high availability architecture (high availability architecture failure)
  • The relationship between ntp and linux system time zone, cpu tick (affects the execution of time-related processes)
  • After ntp or the system time zone is adjusted, what impact will it have on the existing data in the database?

01. What is ntp service?

NTP is a network time synchronization protocol, which is a protocol used to synchronize the time of each computer in the network.

Communication principle : First, the host starts NTP. The client will send a time adjustment message to the NTP server. Then the NTP server will send the current standard time to the client. After the client receives the time from the server, it will adjust its time based on this information. In this way, the network time synchronization is realized.

There are two specific synchronization modes

  • The number of servers is relatively small, you can directly synchronize with the time server
  • There are many local servers, self-built time synchronization server locally for time synchronization between clusters

Normal operation

  • Use ntpd to calibrate the clock ( change the time speed ) instead of adjusting the time on the computer clock ( change the specific time ).
  • You can use ntpdate to synchronize time when the computer has just started but not many services have been started yet
  • NTPD will record the oscillation frequency deviation of the BIOS timer during synchronization with the time server. In this way, even if there is a problem with the network, the machine can still maintain a fairly accurate travel time.

Two commands for time synchronization

  • ntpd : Calibrate the time, calibrate the time a little bit, and finally correct the time slowly. The ntpd service can correct the cpu tick while correcting the time . The tick belongs to the hardware clock and can relatively change the speed of the time. ntpd has a self-protection setting: if the time difference between this machine and the source is too large, ntpd will not run.

  • ntpdate : Will not consider whether other programs will have problems, and directly adjust the system time synchronously, which may affect the program.

  • cpu tick : a refresh rate similar to cpu. For example: In a game, the changes in the game world are discrete, which means that the computer refreshes the changes in things at a fixed frequency. If the rate is too slow, the player can feel that everything is jumping; if the rate is too fast, the smoother it will naturally be, but it will eat the CPU. The system time is related to the refresh rate of the CPU.

02. ntp time synchronization

Use the ntpdate command directly to synchronize with the time server (the client ntpd service must be closed first)

ntpdate -u x.x.x.x

The xxxx here can be the IP address of the public network time server, or the address of a self-built ntp server.

Several public network time servers: Alibaba Cloud time server. The timing signals come from GPS and Beidou satellite signals and are equipped with atomic clocks to keep time.

http://time1.aliyun.com
http://time2.aliyun.com
http://time3.aliyun.com
http://time4.aliyun.com
http://time5.aliyun.com
http://time6.aliyun.com
http://time7.aliyun.com

You can also build your own ntp server (specific configuration is omitted, direct introduction to regular use)

# 安装  
yum install ntp

# 开机自启动
chkconfig ntpd on

# 查看NTP是否正常运行
netstat -tlunp | grep ntp

# 配置防火墙过滤规则
/sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT

Configure the /etc/sysconfig/ntpd file

  • The ntp service will only synchronize the system time by default. If you want ntp to synchronize the hardware time at the same time, you can set the /etc/sysconfig/ntpd file. In the /etc/sysconfig/ntpd file, add SYNC_HWCLOCK=yes so that the hardware time can be synchronized with the system time.

Start the ntpd service

service ntpd start

Configure the NTP client (other details are omitted, firewall policies, etc.) :

In vim /etc/ntp.conf on all clients, add:

server x.x.x.x 

xxxx is the ntp server address configured above

Note : When the time error between the server and the client is too large (may be 1000 seconds), the modification of the time may bring unpredictable problems to the system and applications, and NTP will stop time synchronization! Therefore, if you find that the time is not synchronized after the NTP is started, you should consider that it may be caused by the time difference. In this case, you need to manually synchronize the time ntpdate.

View the status of the ntp server and the upper ntp server

ntpq -p
  • remote: The ip or host name of the local machine and the upper ntp, "+" has a connection to be a candidate, "*" is in use
  • refid: ntp address of a higher level
  • st: stratum level
  • when: how many seconds ago the time was synchronized
  • poll: how many seconds after the next update
  • reach: The number of times that the upper ntp server has requested an update
  • delay: Network delay
  • offset: Time compensation
  • jitter: Difference between system time and bios time
# ntpq -p
remote  refid                  			st 	t 	when 	poll 	reach   delay   offset  jitter
==============================================================================================
*		time4.aliyun.co 10.137.38.86    2 	u  	111  	128  	377   	27.480	-5.995	1.852

Manually synchronize the time once: /usr/sbin/ntpdate 10.137.38.86 (Server host IP, here the client needs to close the NTP service first )

Start the NTP service:

service ntpd start

Observe the time synchronization status:

ntpq -p

Observe the synchronization result ntpstat command

# ntpstat

unsynchronised
polling server every 8 s

If the synchronization fails, it takes time to synchronize. Wait for 5-10 minutes to query again:

Every 2.0s: ntpstat Tue Jul 11 16:55:57 2017synchronised to NTP server (10.10.11.247) at stratum 12 time correct to within 605 ms polling server every 128 s

The time synchronization is complete. Is the date consistent with the server host time? ! !

03. linux system time

Linux time is divided into System Clock (system time) and Real Time Clock (hardware time, RTC for short).

  • System time: refers to the time in the current Linux Kernel.
  • Hardware time: The time when there is battery power on the motherboard.

View system time

date

View hardware time

hwclock --show
或
clock --show

Synchronization of hardware time and system time. Restart the system, the hardware time will read the system time synchronization. If the system is not restarted, use hwclock or clock commands to achieve synchronization.

硬件时钟与系统时钟同步:(hc代表硬件时间,sys代表系统时间)
# hwclock --hctosys
或者 
# clock --hctosys

系统时钟和硬件时钟同步:
# hwclock --systohc
或者
# clock --systohc

Use the tzselect command to set the time zone
Insert picture description here
Insert picture description here
Insert picture description here

04. How does the system time affect the database?

Database time field storage

  • The bottom layer of the "time field" of the database is actually translated into a timestamp and stored on the disk
  • For example, insert a time data field, which will be translated into a timestamp based on the time zone of the current database and then stored on the disk (if it is a timestamp directly, the translation is ignored).
  • The database automatically generates time functions (now, system) will also obtain the current linux timestamp, and then translate the time zone of the current database into time display, and directly store the corresponding linux timestamp. The display results of these functions may be inconsistent with the Linux time (Linux and the database time zone are different)

Reading the database time field

  • Translate the timestamp on the disk into the time zone time of the current database, and then send it to the client for display.
  • Database related time functions directly read Linux timestamp

Modify system time or time zone

  • The database can also set the parameters related to the time zone. Generally, they must be determined during deployment and will not be changed later (it will cause confusion in the time display).

  • The time zone of the database is generally consistent with the time zone of Linux. If they are inconsistent, the same time stamp will be different from the one displayed in the database.

  • The database system time depends on the Linux system time. Some time functions of the database now and system read the timestamp of Linux. If the time zone is inconsistent, the results of the same timestamp between Linux and the database are different.

  • Modifying the Linux system time will have an impact on the database system time, and it will definitely have an impact on now and system. If your business has unique requirements for time, it will definitely report an error (if the time is modified in the future).

  • ntp can synchronize the time on another machine to the current server (if it is to adjust the time to the future, it is no problem). If some processes or businesses depend on the time sequence, then adjust the time to the past, there will be an error! ! !

  • ntp Synchronize time or modify the Linux system time zone, it will not affect the existing data, only the data that will be stored.

Summary: Time zone modification is risky, and time modification is in the future, depending on whether there is a business that relies on time sequence (for example, modifying time in the past).

Guess you like

Origin blog.csdn.net/qq_42226855/article/details/112757684