mysql NOW() return wrong value whereas global timezone is set +00:00
-
18-06-2021 - |
質問
since 2 weeks I puzzled over timezone issue, everything working fine on my localhost BUT it returns wrong value on dedicated server. Let me tell what i have done so far.
First set global timezone by below query: ( Super privilege both on localhost and server )
SET GLOBAL time_zone = '+00:00';
now run below query to cross check whatever done
SELECT NOW(),@@global.time_zone AS gtz,@@session.time_zone AS stz,
TIMEDIFF(NOW(), CONVERT_TZ( NOW() , @@session.time_zone , '+00:00' ) )
AS OFFSET;
but it display different results on local and dedicated server
on localhost (192.168.x.x) mysql version : 5.5.8
+---------------------+--------+--------+----------+
| NOW() | gtz | stz | OFFSET |
+---------------------+--------+--------+----------+
| 2012-07-02 07:06:55 | +00:00 | +00:00 | 00:00:00 |
+---------------------+--------+--------+----------+
1 row in set (0.00 sec)
on dedicated server (182.168.x.x) mysql version :5.1.53-log
+---------------------+--------+--------+----------+
| NOW() | gtz | stz | OFFSET |
+---------------------+--------+--------+----------+
| 2012-07-02 12:37:59 | +00:00 | +00:00 | 00:00:00 |
+---------------------+--------+--------+----------+
My question is
why NOW() gives wrong time ( above is IST ) whereas timezone is set to +00:00 ?
side note :
I run below query
SHOW VARIABLES LIKE '%time%zone%';
on localhost
+------------------+---------------------+
| Variable_name | Value |
+------------------+---------------------+
| system_time_zone | India Standard Time |
| time_zone | +00:00 |
+------------------+---------------------+
on server
+------------------+---------------------+
| Variable_name | Value |
+------------------+---------------------+
| system_time_zone | GMT+5 |
| time_zone | +00:00 |
+------------------+---------------------+
does this will affect the result? OR
is there any bug in earlier version of mysql ?
please help me.
解決
When calling NOW()
(and related functions), MySQL converts the computer's system clock to the session timezone:
If the system clock is set to
12:30+05:30
and the session timezone is+00:00
, the result will be07:00
.If the system clock is set to
17:30+05:00
and the session timezone is+00:00
, the result will be12:30
.
However, one can 'fool' MySQL into thinking that the system clock is in a different timezone to that which the operating system believes by using the --timezone
command line argument to mysqld_safe
:
- If the system clock is set to
17:30+10:30
and the session timezone is+00:00
, but MySQL was started in such a way specifying that the system clock should be understood to beGMT+5
, the result will be the same as the second bullet above.
You should therefore check:
That the timezone of the system clock reconciles with the value given in the
system_time_zone
system variable (if it doesn't, ensure that you are not specifying a--timezone
argument tomysqld_safe
and that there was noTZ
environment variable set whenmysqld_safe
was invoked);That the system clock is reporting the correct time in its specified timezone.
他のヒント
You just need to restart mysqld after altering timezone of System..
The Global time zone of MySQL takes timezone of System. When you change any such attribute of system, you just need a restart of Mysqld.
That's it.
1) Change your system time and timezone, if necessary. 2) Open mysql console and put in your timezone, smth like this: SET GLOBAL time_zone = "+04:00"; 3) Restart mysql.
i have tested- SET time_zone='+05:30'; it is working,but i dont have global permission so you try by setting server time +05:30 -> SET GLOBAL time_zone = '+05:30';