Mysql entry to the proficient-8 hours difference in MySQL time zone configuration

This article mainly introduces the method of MySQL to modify the time zone, summarizes and analyzes three common MySQL time zone modification techniques, including command line mode, configuration file method and code method. Friends who need it can refer to

 

Method 1: Modify dynamically through mysql command line mode

1.1 View mysql current time and current time zone

Copy code

> select curtime(); # or select now() can also 
+-----------+ 
| curtime() | 
+-----------+ 
| 15:18: 10 | 
+-----------+ 
> show variables like "%time_zone%"; 
+------------------+----- ---+ 
| Variable_name | Value | 
+------------------+--------+ 
| system_time_zone | CST | 
| time_zone | SYSTEM | 
+- -----------------+--------+ 
2 rows in set (0.00 sec) 
#time_zone indicates that mysql uses the time zone of system, and system_time_zone indicates that system uses CST time zone

Copy code

1.2 Modify time zone

1

2

3

> set global time_zone = '+8:00'; ##修改mysql全局时区为北京时间,即我们所在的东8区

> set time_zone = '+8:00'; ##修改当前会话时区

> flush privileges; #立即生效

  Method 2: Modify the time zone by modifying the my.cnf configuration file

1

2

3

# vim /etc/my.cnf ##在[mysqld]区域中加上

default-time_zone = '+8:00'

# /etc/init.d/mysqld restart ##重启mysql使新时区生效

  Method 3: If it is inconvenient to restart mysql and you want to temporarily solve the time zone problem, you can initialize the mysql time zone when mysql is initialized through php or other languages

Here, take php as an example, and use it under mysql_connect():

1

mysql_query("SET time_zone = '+8:00'")

  This allows you to change the time zone without restarting. But some system functions of mysql still cannot be used, such as now(). This sentence still cannot be understood.

Guess you like

Origin blog.csdn.net/wxb880114/article/details/113102710