修改MySQL的时区,涉及参数time_zone

原地址:http://blog.csdn.net/mchdba/article/details/9763521

首先需要查看mysql的当前时区,用time_zone参数

[html] view plain copy
  1. mysql> show variables like '%time_zone%';     
  2. +------------------+--------+  
  3. | Variable_name    | Value  |  
  4. +------------------+--------+  
  5. | system_time_zone | CST    |  
  6. | time_zone        | SYSTEM |  
  7. +------------------+--------+  
  8. 2 rows in set (0.00 sec)  



1 可以通过修改my.cnf
在 [mysqld] 之下加
default-time-zone=timezone
来修改时区。如:
default-time-zone = '+8:00'
修改完了记得记得重启msyql
注意一定要在 [mysqld] 之下加 ,否则会出现 unknown variable 'default-time-zone=+8:00'

2 另外也可以通过命令行在线修改

[html] view plain copy
  1. set time_zone = timezone  
  2. 比如北京时间(GMT+0800)  
  3. set time_zone = '+8:00'; 如下:  
  4. mysql> set time_zone='+8:00';  
  5. Query OK, 0 rows affected (0.00 sec)  
  6.   
  7. mysql> show variables like '%time_zone%';     
  8. +------------------+--------+  
  9. | Variable_name    | Value  |  
  10. +------------------+--------+  
  11. | system_time_zone | CST    |  
  12. | time_zone        | +08:00 |  
  13. +------------------+--------+  
  14. 2 rows in set (0.00 sec)  


 

3 再通过select now()来验证时区

[html] view plain copy
  1. mysql> show variables like '%time_zone%';     
  2. +------------------+--------+  
  3. | Variable_name    | Value  |  
  4. +------------------+--------+  
  5. | system_time_zone | CST    |  
  6. <span style="color:#3366ff;">time_zone        | +08:00 </span>|  
  7. +------------------+--------+  
  8. 2 rows in set (0.00 sec)  
  9.   
  10. mysql> select now();  
  11. +---------------------+  
  12. | now()               |  
  13. +---------------------+  
  14. <span style="color:#3333ff;">2013-08-05 10:35:31 </span>|  
  15. +---------------------+  
  16. 1 row in set (0.00 sec)  
  17.   
  18. mysql>  <span style="color:#ff0000;">set </span><span style="color:#ff0000;">time_zone='+0:00';  
  19. </span>Query OK, 0 rows affected (0.00 sec)  
[html] view plain copy
  1. mysql> show variables like '%time_zone%';  
  2. +------------------+--------+  
  3. | Variable_name    | Value  |  
  4. +------------------+--------+  
  5. | system_time_zone | CST    |  
  6. |<span style="color:#ff0000;"> time_zone        | +00:00 </span>|  
  7. +------------------+--------+  
  8. 2 rows in set (0.00 sec)  
[html] view plain copy
  1. mysql> select now();  
  2. +---------------------+  
  3. | now()               |  
  4. +---------------------+  
  5. <span style="color:#ff0000;">2013-08-05 02:35:43 </span>|  
  6. +---------------------+  
  7. 1 row in set (0.00 sec)  


 

参考文献: http://dev.mysql.com/doc/refman/5.7/en/time-zone-leap-seconds.html
 



======补充==

GMT(Greenwich Mean Time)代表格林尼治标准时间,这个大家都知道。 
而CST却同时可以代表如下 4 个不同的时区: 
  • Central Standard Time (USA) UT-6:00
  • Central Standard Time (Australia) UT+9:30
  • China Standard Time UT+8:00
  • Cuba Standard Time UT-4:00

可见,CST可以同时表示美国,澳大利亚,中国,古巴四个国家的标准时间。 

前面提到的通过 Java 获取的CST时间用的是China Standard Time,而客户端JavaScript则默认采用的是美国的中部时间。 

所以将 Fri Aug 28 09:37:46 CST 2009 加上 6 个小时,再加上 8 个小时,就等于 Fri Aug 28 2009 23:37:46 GMT+0800 

可见,在以后的编程中为了避免错误,还是不要使用CST时间,而尽量采用GMT时间。 


 ====
 
原文地址:https://www.cnblogs.com/xingzc/p/7896460.html