Solution to the problem of eight hours difference in MySQL insertion time

Solution to the problem of eight hours difference in MySQL insertion time

Solve the problem of eight hours time difference in MySQL insertion

Generally, several parameters need to be added to the jdbc url. Most blogs give tutorials with these parameters: useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

The configuration result is: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

To solve the time zone problem, I used serverTimezone=UTC in the past, but I ran into a small pit today and realized that this is not a panacea.

First, insert a record normally

It is found that the database record time is different from the local time

The first method: Check the time in MySQL

First use mysql -u username -p password in cmd to log in to mysql

Enter select now();

Check MySQL time


If the time is consistent with your local current time

This means that there is no problem with MySQL time, but the problem lies with Java time.

Try changing the jdbc url parameter to serverTimezone=Asia/Shanghai .

Second: If the query MySQL time is inconsistent with the local time, use the following statement to set the MySQL time zone

 show variables like '%time_zone%'; //Query the current time zone set global time_zone='+8:00'; //Add +8 hours to the standard time zone, i.e. Eastern Time Zone 8 flush privileges; # Take effect immediately

Final test results


success

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Mysql classic "8-hour problem"

<<:  Introduction and use of js observer mode

>>:  Detailed explanation of the problem of failure to synchronize warehouse cache after changing yum source in CentOS8

Recommend

TypeScript Enumeration Type

Table of contents 1. Overview 2. Digital Enumerat...

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

Thoroughly understand JavaScript prototype and prototype chain

Table of contents Preface Laying the foundation p...

js to achieve image fade-in and fade-out effect

This article shares the specific code of js to ac...

Class in front-end JavaScript

Table of contents 1. Class 1.1 constructor() 1.2 ...

Detailed explanation of how MySQL solves phantom reads

1. What is phantom reading? In a transaction, aft...

CSS3 new layout: flex detailed explanation

Flex Basic Concepts Flex layout (flex is the abbr...

Usage of if judgment in HTML

In the process of Django web development, when wr...

SQL implementation of LeetCode (178. Score ranking)

[LeetCode] 178.Rank Scores Write a SQL query to r...

When should a website place ads?

I recently discussed "advertising" with...

How to remotely connect to the cloud server database using Navicat

It is very convenient to connect to a remote serv...

Example of exporting and importing Docker containers

Table of contents Exporting Docker containers Imp...

Introduction to MySQL database performance optimization

Table of contents Why optimize? ? Where to start?...