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

JavaScript source code for Elimination

JavaScript to achieve the source code download ad...

SQL Optimization Tutorial: IN and RANGE Queries

Preface "High Performance MySQL" mentio...

How to use shell to perform batch operations on multiple servers

Table of contents SSH protocol SSH Connection pro...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...

How to manage users and groups when running Docker

Docker is a management tool that uses processes a...

MySQL detailed explanation of isolation level operation process (cmd)

Read uncommitted example operation process - Read...

HTML+CSS to achieve simple navigation bar function

Without further ado, I'll go straight to the ...

Will the deprecated Docker be replaced by Podman?

The Kubernetes team recently announced that it wi...

5 common scenarios and examples of JavaScript destructuring assignment

Table of contents Preface 1. Extract data 2. Alia...

Detailed explanation of Mencached cache configuration based on Nginx

Introduction Memcached is a distributed caching s...

Implementation of Element-ui Layout (Row and Col components)

Table of contents Basic instructions and usage An...

Troubleshooting ideas and solutions for high CPU usage in Linux systems

Preface As Linux operation and maintenance engine...

MySQL 5.7.27 winx64 installation and configuration method graphic tutorial

This article shares the installation and configur...

Steps for encapsulating element-ui pop-up components

Encapsulate el-dialog as a component When we use ...

Deeply understand how nginx achieves high performance and scalability

The overall architecture of NGINX is characterize...