修改MySQL数据库的时区
banner 2020-09-15 MySQL
# 查看数据库时区
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CST |
| time_zone | CST -07:00|
+------------------+--------+
2 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
这里CST -07:00
代表北美时间
MySQL默认使用的SYSTEM时区,即EST时区,EST时区要比北京时间(东八区)慢13个小时,在数据库中的表现即为:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2017-03-09 21:24:39 |
+---------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 修改时区
# 临时修改
# 设置全局时区
mysql> set global time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)
# 设置时区为东八区
mysql> set time_zone = '+8:00';
Query OK, 0 rows affected (0.00 sec)
# 刷新权限使设置立即生效
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | EST |
| time_zone | +08:00 |
+------------------+--------+
2 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
set global time_zone = '+8:00';
set time_zone = '+8:00';
flush privileges;
1
2
3
2
3
# 修改 my.cnf 实现永久修改
vi /etc/mysql/my.cnf
1
然后在mysqld下边的配置中添加一行:
default-time_zone = '+8:00'
1
然后重启MySQL
service mysql restart
1