1)设置linux系统时间,这以centos为例
- // 查看时间
- [root@localhost ~]# date
- 2022年 08月 03日 星期三 19:34:21 CST
-
- // 同步网络时间
- [root@localhost ~]# yum -y install ntp ntpdate
- 已加载插件:fastestmirror
- Loading mirror speeds from cached hostfile
- epel/x86_64/metalink | 6.6 kB 00:00:00
- * base: mirrors.ustc.edu.cn
- * epel: mirrors.bfsu.edu.cn
- * extras: mirrors.njupt.edu.cn
- * updates: mirrors.njupt.edu.cn
- base | 3.6 kB 00:00:00
- docker-ce-stable | 3.5 kB 00:00:00
- epel | 4.7 kB 00:00:00
- extras | 2.9 kB 00:00:00
- updates | 2.9 kB 00:00:00
- (1/2): epel/x86_64/updateinfo | 1.1 MB 00:00:05
- (2/2): epel/x86_64/primary_db | 7.0 MB 00:00:17
- 软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64 已安装并且是最新版本
- 软件包 ntpdate-4.2.6p5-29.el7.centos.2.x86_64 已安装并且是最新版本
- 无须任何处理
- [root@localhost ~]# ntpdate cn.pool.ntp.org
- 4 Aug 10:43:19 ntpdate[2007]: step time server 139.199.215.251 offset 54409.400114 sec
- [root@localhost ~]# hwclock --systohc
-
- // 查看时间
- [root@localhost ~]# date
- 2022年 08月 04日 星期四 10:43:42 CST
-
- // 查看时差
- [root@localhost ~]# timedatectl
- Local time: 四 2022-08-04 10:45:31 CST
- Universal time: 四 2022-08-04 02:45:31 UTC
- RTC time: 四 2022-08-04 02:45:25
- Time zone: Asia/Shanghai (CST, +0800)
- NTP enabled: yes
- NTP synchronized: no
- RTC in local TZ: no
- DST active: n/a
- // 如果时差有问题 设置时差
- [root@localhost ~]# timedatectl set-timezone Asia/Shanghai
2)如果是容器部署mysql 设置容器时间
- // 进入容器
- [root@localhost ~]# docker exec -it mysql bash
-
- // 移除时间文件
- root@b7238f2a5cef:/# rm /etc/localtime
-
- // 挂载系统时间文件到容器
- root@b7238f2a5cef:/# ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
- root@b7238f2a5cef:/# exit
- exit
-
- // 重启容器
- [root@localhost ~]# docker restart mysql
- mysql
-
- // 查看时间
- [root@localhost ~]# docker exec -it mysql bash
- root@b7238f2a5cef:/# date
- Thu Aug 4 10:50:29 CST 2022