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 inserting a form with a blank line above and below

I don't know if you have noticed when making a...

mysql 8.0.19 winx64.zip installation tutorial

This article records the installation tutorial of...

XHTML Web Page Tutorial

This article is mainly to let beginners understan...

Let’s take a look at JavaScript precompilation (summary)

JS running trilogy js running code is divided int...

JavaScript implements circular carousel

This article shares the specific code of JavaScri...

Talk about nextTick in Vue

When the data changes, the DOM view is not update...

The presentation and opening method of hyperlink a

<br />Related articles: How to prompt and op...

Multiple methods to modify MySQL root password (recommended)

Method 1: Use the SET PASSWORD command MySQL -u r...

Bootstrap 3.0 study notes grid system case

Preface In the previous article, we mainly learne...

Vue 2.0 Basics in Detail

Table of contents 1. Features 2. Examples 3. Opti...

How to deploy Rancher with Docker (no pitfalls)

Must read before operation: Note: If you want to ...

How to create Apache image using Dockerfile

Table of contents 1. Docker Image 2. Create an in...

HTML solves the problem of invalid table width setting

If you set the table-layer:fixed style for a tabl...

How to solve the problem of forgetting the root password of Mysql on Mac

I haven't used mysql on my computer for a lon...