Solve the problem of inconsistency between mysql time and system time in docker

Solve the problem of inconsistency between mysql time and system time in docker

Recently, when I installed MySQL in Docker, I found that the database time was 8 hours different from the system time.

Linux server time:

Enter mysql and enter date as follows

It turns out that the two time zones are different. Because the MySQL container is installed in Docker, the incorrect MySQL time should be caused by the host Docker.

After searching the data, I found that the default time zone of Docker is zone 0. In fact, this will cause a lot of trouble to the installed container, such as inaccurate execution log records.

There are two solutions:

Method 1: Copy the localtime of the host

docker cp /etc/localtime [container ID or NAME]:/etc/localtime

Note: Due to time zone issues, the above command may report the following error

Error response from daemon: Error processing tar file(exit status 1):

invalid symlink "/usr/share/zoneinfo/UCT" -> "../usr/share/zoneinfo/Asia/Shanghai"

View this file

It was found that this file referenced an address

Solution: Switch Command

docker cp /usr/share/zoneinfo/Asia/Shanghai[container ID or NAME]:/etc/localtime

Then restart the container

docker restart [container ID or NAME]

Method 2: Shared host localtime

Specify startup parameters when creating a container and mount the localtime file into the container

docker run --name -v /etc/localtime container id:/etc/localtime:ro ....

The problem of inconsistent time zones between the two is solved.

This is the end of this article about the inconsistency between mysql time and system time in docker. For more related docker time inconsistency content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Docker deploys Mysql, .Net6, Sqlserver and other containers
  • Docker case analysis: Building a MySQL database service
  • Docker installs mysql and solves the Chinese garbled problem
  • Docker installation and configuration steps for MySQL

<<:  Detailed explanation of the code for implementing six sieve styles using HTML grid layout

>>:  How can we promote Jiedaibao so that everyone will register? Jiedaibao promotion methods and skills

Recommend

Vue implements automatic jump to login page when token expires

The project was tested these days, and the tester...

JS implements user registration interface function

This article example shares the specific code of ...

CSS Summary Notes: Examples of Transformations, Transitions, and Animations

1. Transition Transition property usage: transiti...

Linux common commands chmod to modify file permissions 777 and 754

The following command is often used: chmod 777 文件...

Example code for converting http to https using nginx

I am writing a small program recently. Because th...

Detailed explanation of .bash_profile file in Linux system

Table of contents 1. Environment variable $PATH: ...

Using JS to implement a small game of aircraft war

This article example shares the specific code of ...

Three ways to delete a table in MySQL (summary)

drop table Drop directly deletes table informatio...

mysql 8.0.19 win10 quick installation tutorial

This tutorial shares the installation tutorial of...

Vue2.x - Example of using anti-shake and throttling

Table of contents utils: Use in vue: explain: Ima...

What does this.parentNode.parentNode (parent node of parent node) mean?

The parent node of the parent node, for example, t...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

Mysql some complex sql statements (query and delete duplicate rows)

1. Find duplicate rows SELECT * FROM blog_user_re...