Modification of time zone problem of MySQL container in Docker

Modification of time zone problem of MySQL container in Docker

Preface

When Ahhang was developing the Springboot project, the front-end told him that the verification code was always invalid. There was no problem with the local test, but when I looked at the database time of the remote server, wow - it was 8 hours early. Obviously, it was a MySQL time zone problem. This article will record how to change the time zone of the MySQL container in Docker.

Solution

Let's first verify whether the database time zone is really incorrect. Enter the MySQL database and run the statement:

SELECT NOW();

Will return data similar to this:

mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2020-07-04 15:46:46 |
+---------------------+
1 row in set (0.09 sec)

To check the current time zone, enter the following command:

SHOW VARIABLES LIKE '%time_zone%';

Will return data similar to this:

mysql> SHOW VARIABLES LIKE '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | UTC |
| time_zone | +00:00 |
+------------------+--------+
2 rows in set (0.12 sec)

If the returned time is several hours different from yours, and the time zone is incorrect (not +08:00), then it means you need to look down.

Method 1: Temporary modification

When our needs are urgent, we can make this temporary modification. Run the command:

SET GLOBAL time_zone = '+8:00';

Run the following command again to verify that the returned result is the current time:

SELECT NOW();

Returning the current time proves that the modification was successful.

The reason why this method is called "temporary modification" is that the modification will become invalid after restarting MySQL.

Method 2: Add parameters at startup

This method applies to conditions that allow us to recreate a MySQL container.

When creating a container, we need to add a command to specify the time zone (Eastern Time Zone 8 is Shanghai, you can change it to your own time zone as needed):

-e TZ=Asia/Shanghai

So, our complete docker run command should be (for reference only, your run command may be slightly different):

docker run -p 3306:3306 --name mymysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -e TZ=Asia/Shanghai -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8.0

To see the purpose of each parameter, please check out my article: Install MySQL with Docker.

Method 3: Modify the configuration in the container

Enter the mysql container by typing:

docker exec -it container ID bash

Modify the MySQL configuration file (two cases):

vim /etc/mysql/mysql.conf.d

or

vim /etc/mysql/my.cnf

If the above command returns bash: vim: command not found, please read the solution of no vim command in Docker container first.

After entering the configuration file, click i to enter the edit mode and add a line of configuration file:

default-time-zone = '+08:00'

As shown:

Add a profile

When finished, press ESC and enter :wq to save and exit.

Then enter exit to exit the docker container.

Next we need to restart the mysql container, enter the command:

docker restart container id

So far, the time zone configuration has been successfully modified.

After restarting, enter the following command to verify success:

SELECT NOW();

If the returned time is consistent with the current time, the modification is successful.

Conclusion

If you are in a hurry, we recommend method 1. If you are not in a hurry, we recommend method 2. Please choose the one you need.

This is the end of this article about how to fix the time zone issue in Docker's MySQL container. For more information about Docker's MySQL container time zone, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to install MySQL 8.0 in Docker
  • How to run MySQL using docker-compose
  • Upgrade Docker version of MySQL 5.7 to MySQL 8.0.13, data migration
  • Detailed explanation of deploying MySQL using Docker (data persistence)
  • Simple steps to create a MySQL container with Docker
  • How to run MySQL in Docker environment and enable Binlog to configure master-slave synchronization

<<:  js to achieve cool fireworks effect

>>:  How to automatically backup mysql remotely under Linux

Recommend

Records of using ssh commands on Windows 8

1. Open the virtual machine and git bash window a...

Tutorial on using Webpack in JavaScript

Table of contents 0. What is Webpack 1. Use of We...

In-depth reading and practice records of conditional types in TypeScript

Table of contents Using conditional types in gene...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

HTML realizes hotel screening function through form

<!doctype html> <html xmlns="http:/...

You really need to understand the use of CSS variables var()

When a web project gets bigger and bigger, its CS...

How to find and delete duplicate records in MySQL

Hello everyone, I am Tony, a teacher who only tal...

Use of docker system command set

Table of contents docker system df docker system ...

HTML+css to create a simple progress bar

1. HTML code Copy code The code is as follows: Ex...

Several ways to implement inheritance in JavaScript

Table of contents Structural inheritance (impleme...

Why the CSS attribute value clear:right does not work in detail

Using the clear property to clear floats is a comm...

Creation, constraints and deletion of foreign keys in MySQL

Preface After MySQL version 3.23.44, InnoDB engin...

Founder font library Chinese and English file name comparison table

Founder Type Library is a font library developed ...