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

Analysis of the principle of Vue nextTick

Table of contents Event Loop miscroTask (microtas...

Detailed introduction of Chrome developer tools-timeline

1. Overview Users expect the web applications the...

CSS Transition expands and collapses elements by changing the Height

A common development need is that we want to coll...

How to set the position of the block element in the middle of the window

How to set the position of the block element in t...

It's the end of the year, is your MySQL password safe?

Preface: It’s the end of the year, isn’t it time ...

Let's talk in detail about the props attributes of components in Vue

Table of contents Question 1: How are props used ...

Detailed explanation of Vue life cycle

Table of contents Why understand the life cycle W...

How to compile and install PHP and Nginx in Ubuntu environment

This article describes how to compile and install...

Detailed analysis of several situations in which MySQL indexes fail

1. Leading fuzzy query cannot use index (like ...

HTML table markup tutorial (14): table header

<br />In HTML language, you can automaticall...

Ubuntu 20.04 Chinese input method installation steps

This article installs Google Input Method. In fac...