How to avoid the trap of URL time zone in MySQL

How to avoid the trap of URL time zone in MySQL

Preface

Recently, when using MySQL 6.0.x or higher jar, you need to specify serverTimezone in the code URL link. An exception will occur:

1. serverTimezone not specified

Configure the URL in xml

<property name="url" value="jdbc:mysql://localhost:3306/mybatisstudy"/>

Abnormalities

Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

You must configure the server or JDBC driver (via the serverTimezone configuration property) to use a more detailed time zone value if you want to use time zone support.

2. Online solutions

Add the parameter ?serverTimezone=utc after the url

<property name="url" value="jdbc:mysql://localhost:3306/springdatastudy?serverTimezone=UTC"/>

2.1. Problems encountered

Although there is no error in adding the time zone program above, there is a problem when we insert the time into the database using Java code.

For example, the time inserted in the Java code is: 2017-08-21 17:29:56

However, the time displayed in the database is: 2017-08-21 09:29:56

3. Root Cause

Because of the time zone setting problem.

UTC stands for Universal Coordinated Time, but the time we use is the Beijing time zone, which is East 8, which is eight hours ahead of UTC.

UTC + (+0800) = Local (Beijing) time

4. Solution

The time zone of the URL uses China Standard Time. That is, serverTimezone=Asia/Shanghai

4.1 Use Java code to obtain the local time zone ID

Calendar cal = Calendar.getInstance();
TimeZone timeZone = cal.getTimeZone();
System.out.println(timeZone.getID());
System.out.println(timeZone.getDisplayName());
Asia/Shanghai
China Standard Time

Summarize

The above is the full content of this article. I hope that the content of this article can bring some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Modification of time zone problem of MySQL container in Docker
  • Detailed explanation of mysql time zone problem in Java
  • How to view and modify the time zone in MySQL
  • Detailed explanation of MySQL timestamp types and time zone examples
  • Summary of methods for modifying MySQL time zone
  • How to distinguish uppercase and lowercase letters in strings when querying MySQL
  • mysql time zone problem
  • A brief summary of PHP and MySQL time zones
  • mysql solves time zone related problems

<<:  How to use CocosCreator to create a shooting game

>>:  Detailed Introduction to Nginx Installation and Configuration Rules

Recommend

Solution to docker suddenly not being accessible from the external network

According to the methods of the masters, the caus...

Summary of React's way of creating components

Table of contents 1. Create components using func...

Example code for converting http to https using nginx

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

VMware WorkStation 14 pro installation Ubuntu 17.04 tutorial

This article records the specific method of insta...

Detailed analysis of mysql MDL metadata lock

Preface: When you execute a SQL statement in MySQ...

Example of fork and mutex lock process in Linux multithreading

Table of contents Question: 1. First attempt 2. R...

Summary of the benefits of deploying MySQL delayed slaves

Preface The master-slave replication relationship...

Details of using Vue slot

Table of contents 1. Why use slots? 1.1 slot 1.2 ...

MySQL SHOW PROCESSLIST assists in the entire process of troubleshooting

1. SHOW PROCESSLIST command SHOW PROCESSLIST show...

How to allow remote access to open ports in Linux

1. Modify the firewall configuration file # vi /e...

HTML input box optimization to improve user experience and ease of use

In order to improve user experience and ease of us...

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

MySQL 5.7.21 installation and configuration tutorial under Window10

This article records the installation and configu...