首先需要查看MySQL的當前時區(qū),用time_zone參數(shù)
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領(lǐng)域包括:成都網(wǎng)站建設、網(wǎng)站設計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的樅陽網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!
mysql> show global variables like '%zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST | ---系統(tǒng)時間,北京時間
| time_zone | SYSTEM | ----mysql所用的時區(qū),這里是調(diào)用系統(tǒng)時區(qū),也就是北京時間
+------------------+--------+
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時區(qū)
mysql> set time_zone='+0:00';
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-11-30 09:12:06 |
+---------------------+
1 row in set (0.18 sec)
修改為東八區(qū)
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
來修改時區(qū)。如:
default-time-zone = '+8:00'
修改完了記得記得重啟msyql
注意一定要在 [mysqld] 之下加 ,否則會出現(xiàn) unknown variable 'default-time-zone=+8:00'
注意:時區(qū)這個對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 |
+---------+-----------+-------+---------------------+---------------------+