mysql date is 14 hours older than time

When using mybatis to query the data in mysql today, inexplicably all the time went wrong, and all the time was 14 hours longer than the database time. After thinking about it, it was initially determined that it was a problem of the system time zone. Because the mysql time zone setting is the operating system time zone by default, after checking the centos time zone, there is nothing wrong with the east 8 zone, so it can be determined that a wrong time zone is set in the code.


Now start debugging the mybatis source code, when debugging to the createFromTimestamp method of the com.mysql.cj.jdbc.io.JdbcTimestampValueFactory in the mysql-connector-java-6.0.4.jar package, it is found that the underlying driver of mysql has the time to query from the database. A Calender is used for type conversion. The time zone included in the Calender record is CST, which is exactly 14 hours away from China's time zone Asia/Shanghai.


So why does mybatis connect to the database using CST's US time? Continue to view the source code and find that

when mysql connects to the database, the time zone setting of the system will be read from mysql. When debugging the configureTimezone method of com.mysql.cj.mysqla.MysqlaSession.java, it is found that this.getServerVariable("system_time_zone") is read from the system The time zone setting is CST So


far, the problem has been clarified. The time zone set by mysql is incorrect. Log in to Linux, execute mysql -uroot -p, and then run the command show variables like '%time_zone%', and find that the system_time_zone item is indeed CST.


At this point, the troubleshooting is over, and you can modify the time zone settings of mysql.

Author: Xu Wushun
Link : http://www.jianshu.com/p/ea7ef2d29940
Source: Jianshu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326415587&siteId=291194637