【Mysql】修改mysql時區

小亮520cl發表於2018-11-30

首先需要檢視mysql的當前時區,用time_zone引數


mysql> show global variables like '%zone%';

+------------------+--------+

| Variable_name    | Value  |

+------------------+--------+

| system_time_zone | CST    |      ---系統時間,北京時間

| time_zone        | SYSTEM |       ----mysql所用的時區,這裡是呼叫系統時區,也就是北京時間

+------------------+--------+

2 rows in set (0.17 sec)



驗證下:

mysql> select now();

+---------------------+

| now()               |

+---------------------+

| 2018-11-30 17:11:55 |

+---------------------+

1 row in set (0.18 sec)



修改為utc時區

mysql> set time_zone='+0:00';

mysql> select now();

+---------------------+

| now()               |

+---------------------+

| 2018-11-30 09:12:06 |

+---------------------+

1 row in set (0.18 sec)



修改為東八區

mysql> set time_zone='+8:00';

Query OK, 0 rows affected (0.24 sec)

 

mysql> select now();

+---------------------+

| now()               |

+---------------------+

| 2018-11-30 17:21:40 |

+---------------------+

1 row in set (0.72 sec)


永久修改可以透過修改my.cnf

在 [mysqld] 之下加

default-time-zone=timezone

來修改時區。如:

default-time-zone = '+8:00'

修改完了記得記得重啟msyql

注意一定要在 [mysqld] 之下加 ,否則會出現 unknown variable 'default-time-zone=+8:00'



注意:時區這個對timestamp型別的才有影響,datetime 是沒影響的,如:

mysql> select * from uc_smallfeature_sharerecords(時間戳是timestamp型別);

+------+---------------------+-------------+----------+--------+----------+

| id   | create_time         | destination | platform | userid | username |

+------+---------------------+-------------+----------+--------+----------+

| 3425 | 2018-12-25 19:42:51 | 123213      | 123      | 123    | 123      |

+------+---------------------+-------------+----------+--------+----------+


mysql> select * from uc_push_token(時間戳是datetime型別);

+---------+-----------+-------+---------------------+---------------------+

| user_id | push_type | token | create_time         | update_time         |

+---------+-----------+-------+---------------------+---------------------+

|       1 |         0 |       | 2018-12-25 19:40:49 | 2018-12-25 19:40:49 |

+---------+-----------+-------+---------------------+---------------------+


mysql> show global variables like '%zone%';

+------------------+--------+

| Variable_name    | Value  |

+------------------+--------+

| system_time_zone | UTC    |

| time_zone        | +08:00 |

+------------------+--------+

2 rows in set (0.00 sec)


mysql> set global time_zone='+0:00';

Query OK, 0 rows affected (0.00 sec)



重連查詢:

mysql> select * from uc_smallfeature_sharerecords;(timestamp型別的變了)

+------+---------------------+-------------+----------+--------+----------+

| id   | create_time         | destination | platform | userid | username |

+------+---------------------+-------------+----------+--------+----------+

| 3425 | 2018-12-25 11:42:51 | 123213      | 123      | 123    | 123      |

+------+---------------------+-------------+----------+--------+----------+

1 row in set (0.00 sec)


mysql> select * from uc_push_token;(datetime型別的不會變)

+---------+-----------+-------+---------------------+---------------------+

| user_id | push_type | token | create_time         | update_time         |

+---------+-----------+-------+---------------------+---------------------+

|       1 |         0 |       | 2018-12-25 19:40:49 | 2018-12-25 19:40:49 |

+---------+-----------+-------+---------------------+---------------------+


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-2222318/,如需轉載,請註明出處,否則將追究法律責任。

相關文章