简体   繁体   中英

Unable to change the timezone of Mysql server

We have timezone set to IST (Indian Standard Time) to begin with -

mysql> show global variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone | IST    |
| time_zone        | SYSTEM |
+------------------+--------+


SELECT @@global.time_zone, @@session.time_zone;
+--------------------+---------------------+
| @@global.time_zone | @@session.time_zone |
+--------------------+---------------------+
| SYSTEM             | SYSTEM              |
+--------------------+---------------------+
1 row in set (0.00 sec)

mysql> select NOW();
+---------------------+
| NOW()               |
+---------------------+
| 2017-10-31 17:05:26 |
+---------------------+

Above shows same time as server time, which is also in IST.

We want to change it to UTC. Comparing with a server where the timezone is UTC, the only different value on this server seems to be the system_time_zone variable which is set to IST as shown above.

Not sure which variable/config to change.

We tried changing the default-time-zone in my.ini but it changed the time_zone variable and not the system_time_zone .

Following will also attempt to update the time_zone as I understand - SET GLOBAL time_zone="+00:00";

So, not sure what will update the system_time_zone .

Already checked https://stackoverflow.com/a/5359622/351903 Not sure what exactly is the difference between system_time_zone and time_zone.

Also, The Mysql documentation says -

The permissible values for --timezone or TZ are system dependent.

Unable to find the appropriate value for IST on windows and Cent OS servers. From this article it seems it needs some investigation. Can someone help here? Any pointers.

Background

What I want is to see a time difference of 5:30 hours when I do select NOW(); on the server. Currently it is displaying the same time as application server time (which is also IST). We are trying to add this discrepancy to reproduce a test scenario.

Update

Getting the following on my windows setup -

mysql> SET time_zone = 'Asia/Kolkata';
ERROR 1298 (HY000): Unknown or incorrect time zone: 'Asia/Kolkata'
mysql> SET time_zone = "Asia/Kolkata";
ERROR 1298 (HY000): Unknown or incorrect time zone: 'Asia/Kolkata'
mysql> SET time_zone = "UTC";
ERROR 1298 (HY000): Unknown or incorrect time zone: 'UTC'

Your MySQL software's system time zone default is set to your server machine's default time zone. That's what SYSTEM means in that setting. Your server machine in turn is set to India time.

Notice that NOW() and TIMESTAMP data are always translated to local time upon display or retrieval. Local time is defined via the setting of the time_zone variable, either the one set for your MySQL connection or the global one.

Try this sequence and see if you get NOW() in UTC, and then in India time. This sequence sets your connection's time_zone variable. In so doing it overrides the system setting.

 SET time_zone = 'UTC';
 SELECT NOW();
 SET time_zone = 'Asia/Kolkata';  /* zoneinfo name for India time */
 SELECT NOW();

If you get what you expect, you can change the default timezone setting on your server machine from India time to UTC. That's generally good practice. How you do that is dependent on your server machine's OS. This says how to do it for Ubuntu.

https://askubuntu.com/questions/323131/setting-timezone-from-terminal

Or, you can edit your MySQL configuration file to include a line like this:

 default-time-zone='UTC'

and restart your MySQL server software.

In USA, where we have a whole mess of time zones, it's usually helpful to set the default to UTC. Then, we allow users to choose a time zone preference when they register to use the web app. Maybe, if your web app is all-India, you don't need this added complexity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM