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

MySQL uses covering index to avoid table return and optimize query

Preface Before talking about covering index, we m...

Solution to 1449 and 1045 exceptions when connecting to MySQL

Solution to 1449 and 1045 exceptions when connect...

Some common mistakes with MySQL null

According to null-values, the value of null in My...

Web Design: When the Title Cannot Be Displayed Completely

<br />I just saw the newly revamped ChinaUI....

CSS3 realizes various graphic effects of small arrows

It’s great to use CSS to realize various graphics...

Detailed explanation of Javascript Echarts air quality map effect

We need to first combine the air quality data wit...

Pure CSS to achieve hover image pop-out pop-up effect example code

Implementation principle The main graphics are co...

Page Refactoring Skills - Javascript, CSS

About JS, CSS CSS: Stylesheet at the top Avoid CS...

MySQL trigger trigger add, delete, modify and query operation example

This article uses examples to describe the add, d...

HTML commonly used meta encyclopedia (recommended)

The Meta tag is an auxiliary tag in the head area...

Summary of MySQL log related knowledge

Table of contents SQL execution order bin log Wha...

Detailed installation and configuration tutorial of PostgreSQL 11 under CentOS7

1. Official website address The official website ...

Steps to transplant the new kernel to the Linux system

1. Download the ubuntu16.04 image and the corresp...

How to use Zen coding in Dreamweaver

After I published my last article “Zen Coding: A Q...

MySQL data backup and restore sample code

1. Data backup 1. Use mysqldump command to back u...