docker mysql Ubuntu时区问题

本文介绍了如何在Docker容器中的MySQL数据库解决时区问题,包括确认系统时区、修改my.cnf配置文件,以及在Ubuntu上通过tzdata设置时区以确保时间和日期的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、mysql时区问题

2、Ubuntu时区问题


1、mysql时区问题

解决docker中mysql时区问题_杨园亮的博客-优快云博客

问题
数据库插入的时间统统相差8个小时。

//初步怀疑服务器时间不对。date看下时间是对的
//然后怀疑代码问题,检查代码后没有问题
//最后怀疑mysql的问题。
--docker exec -it mysql /bin/bash
--mysql -uroot -p 
--show variables like '%time_zone%'
    +------------------+--------+
    | Variable_name    | Value  |
    +------------------+--------+
    | system_time_zone | UTC    |
    | time_zone        | SYSTEM |
    +------------------+--------+
//可见使用的是系统时区

//改文件
-- docker cp mysql:/etc/mysql/my.cnf ./my.cnf
-- vim my.cnf 最后一行加上
default-time-zone = '+8:00'
-- docker cp ./my.cnf mysql:/etc/mysql/my.cnf
-- docker container mysql restart

vi 命令不可用

Docker教程:解决Docker容器内不能使用vim命令的问题 - .NET开发菜鸟 - 博客园

$ apt-get install vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package vim


$ apt-get update
Get:1 http://repo.mysql.com/apt/debian buster InRelease [22.1 kB]
Get:2 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB]                                                      
Get:3 http://deb.debian.org/debian buster InRelease [122 kB]                  
Get:4 http://repo.mysql.com/apt/debian buster/mysql-8.0 amd64 Packages [8447 B]        
Get:5 http://security.debian.org/debian-security buster/updates/main amd64 Packages [318 kB]      
Get:6 http://deb.debian.org/debian buster-updates InRelease [51.9 kB]
Get:7 http://deb.debian.org/debian buster/main amd64 Packages [7911 kB]
Get:8 http://deb.debian.org/debian buster-updates/main amd64 Packages [8796 B]                                                                                   
Fetched 8507 kB in 9min 30s (14.9 kB/s)                                                                                                                          
Reading package lists... Done


$ apt-get install vim
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  vim-common vim-runtime xxd
Suggested packages:
  ctags vim-doc vim-scripts
The following NEW packages will be installed:
  vim vim-common vim-runtime xxd
0 upgraded, 4 newly installed, 0 to remove and 9 not upgraded.
Need to get 7392 kB of archives.
After this operation, 33.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] y


//完成安装
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | UTC    |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2022-05-01 00:20:01 |
+---------------------+
1 row in set (0.00 sec)

mysql> 

2、Ubuntu时区问题

  1. apt-get update
  2. apt-get install -y tzdata
  3. Asia
  4. Shanghai
root@1cdf7c371321:/usr/bin# date -R
Wed, 11 May 2022 12:20:10 +0000



root@1cdf7c371321:/etc# apt-get update

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [109 kB]
Get:4 http://security.ubuntu.com/ubuntu jammy-security/universe amd64 Packages [61.0 kB]
Get:5 http://security.ubuntu.com/ubuntu jammy-security/main amd64 Packages [84.2 kB]
Hit:6 http://archive.ubuntu.com/ubuntu jammy-backports InRelease  
Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 Packages [79.7 kB]
Get:8 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 Packages [98.8 kB]
Fetched 544 kB in 6s (84.7 kB/s)                                                                                           
Reading package lists... Done

//安装 tzdata命令
root@1cdf7c371321:/etc# apt-get install -y tzdata

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  tzdata
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 342 kB of archives.
After this operation, 3925 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 tzdata all 2022a-0ubuntu1 [342 kB]
Fetched 342 kB in 4s (86.3 kB/s) 
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package tzdata.
(Reading database ... 23880 files and directories currently installed.)
Preparing to unpack .../tzdata_2022a-0ubuntu1_all.deb ...
Unpacking tzdata (2022a-0ubuntu1) ...
Setting up tzdata (2022a-0ubuntu1) ...
debconf: unable to initialize frontend: Dialog
debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 78.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.34.0 /usr/local/share/perl/5.34.0 /usr/lib/x86_64-linux-gnu/perl5/5.34 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl-base /usr/lib/x86_64-linux-gnu/perl/5.34 /usr/share/perl/5.34 /usr/local/lib/site_perl) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)
debconf: falling back to frontend: Teletype
Configuring tzdata
------------------

Please select the geographic area in which you live. Subsequent configuration questions will narrow this down by presenting
a list of cities, representing the time zones in which they are located.

  1. Africa   3. Antarctica  5. Arctic  7. Atlantic  9. Indian    11. US
  2. America  4. Australia   6. Asia    8. Europe    10. Pacific  12. Etc
Geographic area: 6    //选序号6

Please select the city or region corresponding to your time zone.

  1. Aden      14. Beirut      27. Gaza         40. Karachi       53. Muscat        66. Riyadh         79. Tokyo
  2. Almaty    15. Bishkek     28. Harbin       41. Kashgar       54. Nicosia       67. Sakhalin       80. Tomsk
  3. Amman     16. Brunei      29. Hebron       42. Kathmandu     55. Novokuznetsk  68. Samarkand      81. Ujung_Pandang
  4. Anadyr    17. Chita       30. Ho_Chi_Minh  43. Khandyga      56. Novosibirsk   69. Seoul          82. Ulaanbaatar
  5. Aqtau     18. Choibalsan  31. Hong_Kong    44. Kolkata       57. Omsk          70. Shanghai       83. Urumqi
  6. Aqtobe    19. Chongqing   32. Hovd         45. Krasnoyarsk   58. Oral          71. Singapore      84. Ust-Nera
  7. Ashgabat  20. Colombo     33. Irkutsk      46. Kuala_Lumpur  59. Phnom_Penh    72. Srednekolymsk  85. Vientiane
  8. Atyrau    21. Damascus    34. Istanbul     47. Kuching       60. Pontianak     73. Taipei         86. Vladivostok
  9. Baghdad   22. Dhaka       35. Jakarta      48. Kuwait        61. Pyongyang     74. Tashkent       87. Yakutsk
  10. Bahrain  23. Dili        36. Jayapura     49. Macau         62. Qatar         75. Tbilisi        88. Yangon
  11. Baku     24. Dubai       37. Jerusalem    50. Magadan       63. Qostanay      76. Tehran         89. Yekaterinburg
  12. Bangkok  25. Dushanbe    38. Kabul        51. Makassar      64. Qyzylorda     77. Tel_Aviv       90. Yerevan
  13. Barnaul  26. Famagusta   39. Kamchatka    52. Manila        65. Rangoon       78. Thimphu
Time zone: 70    //选序号70


Current default time zone: 'Asia/Shanghai'
Local time is now:      Wed May 11 21:29:55 CST 2022.
Universal Time is now:  Wed May 11 13:29:55 UTC 2022.
Run 'dpkg-reconfigure tzdata' if you wish to change it.

root@1cdf7c371321:/etc# date    //再看时间,正常
Wed May 11 21:30:35 CST 2022
root@1cdf7c371321:/etc# 

### 如何在 Ubuntu 上通过 Docker 安装 MySQL #### 准备工作 为了确保顺利安装,在开始之前需进入 root 用户并执行一系列准备工作,这包括但不限于更新系统软件包列表和安装必要依赖项。对于不同版本的 Ubuntu (如 22.04, 20.04 或者更早),这些步骤可能略有差异[^2]。 #### 安装 Docker 使用如下命令来完成 Docker 的安装: ```bash apt-get update && apt-get install -y \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common ``` 接着添加 Docker 的 GPG 密钥与官方仓库地址,并再次刷新本地APT缓存以获取最新可用版本的信息。最后一步则是实际安装 Docker 并启动服务[^4]。 #### 获取 MySQL 镜像 一旦 Docker 成功安装完毕,则可以通过 `docker pull` 命令从 Docker Hub 中下载最新的 MySQL 官方镜像文件到本地机器上备用[^3]。 #### 创建持久化存储位置(可选) 如果希望保存数据库中的数据不随容器销毁而丢失,可以考虑预先建立一个用于挂载卷映射的目标路径 `/docker/mymysql` 来作为外部存储空间[^5]。 #### 启动 MySQL 容器实例 利用先前所提到的方法准备好一切之后,现在就可以正式创建一个新的名为 `mymysql` 的 MySQL 数据库服务器端口监听于主机上的 3306 端口,并设置时区为中国标准时间(`Asia/Shanghai`)以及指定管理员账户密码等参数配置信息: ```bash docker run -d --name mymysql \ -e MYSQL_ROOT_PASSWORD=123456xyz \ -e TZ=Asia/Shanghai \ --restart unless-stopped \ -p 3306:3306 \ -v /docker/mymysql:/var/lib/mysql \ mysql ``` 上述指令会以后台守护进程的方式运行新构建出来的 MySQL 实例。 #### 进行基本验证测试 此时应该能够借助客户端工具比如 Navicat 或者直接经由终端窗口内的 Bash Shell 登录至刚刚搭建成功的 MySQL Server 内部进行初步的功能性检测了[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值