Does `serverTimezone` param change @@session.time_zone in MySQL?

非 Y 不嫁゛ 提交于 2021-01-27 14:16:17

问题


If I'm making a connection to MySQL with serverTimezone=UTC in my connection URL, will that change the @@session.time_zone variable for my connection to MySQL? Or is the only way to alter @@session.time_zone through issuing a SET time_zone... statement? I was lead to believe that the combination of serverTimezone=UTC and useLegacyDateTimeCode=false would set the @@session.time_zone to UTC (or whatever timezone I passed in as an argument to serverTimezone) but testing this behaviour with MySQL Connector/J seems to indicate it does not.


回答1:


As I said in a comment, I was facing the same problem -- needed to find a way of doing @@session.time_zone = '+0:00' without a SQL statement, by configuring the JDBC driver instead.

Here's the solution if you're using properties (Dropwizard and maybe Spring):

sessionTimeZone: UTC
useLegacyDatetimeCode: false

Or just append it to the JDBC connection URL: ?sessionTimeZone=UTC&useLegacyDatetimeCode=false

The connector's documentation doesn't mention it at all (and that's probably why there are so many answers pointing towards 'serverTimezone' and others.




回答2:


If you want your MySQL to be configured UTC :

SET @@global.time_zone = '+00:00';

So every automatic update of datetime and everytime you connect to the database, @@session.time_zone will be set to @@global.time_zone.

Reboot and reconnection of the DB could be needed.

If you're just interested by the modification of the timezone of your session :

SET @@session.time_zone = '+00:00';

You need to do this after every connection.



来源:https://stackoverflow.com/questions/51196041/does-servertimezone-param-change-session-time-zone-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!