首先需要查看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 |
+---------+-----------+-------+---------------------+---------------------+