Article Directory
Environment: Windows10 system, MySQL5.7 version
Mysql method to modify the time zone: 1. In the mysql command line mode, execute the "set global time_zone = 'time zone information'" command; 2. Temporarily solve the problem in PHP: use mysql_query("SET time_zone = '+8 :00'”); Dynamic modification after the connection between PHP and MySQL is established; 3. In the “my.cnf(my.ini)” configuration file, find and modify the value of the “default-time_zone” item to the required time zone. Yes, if not, you can add it yourself.
What is MySQL time zone?
MySQL time zone is a global system variable used to store dates and times, which defines the time zone used when inserting and retrieving dates and times. If the application is running in multiple time zones, the MySQL time zone will need to be properly configured to ensure data accuracy
Modify by MySQL command mode
First check the current time of MySQL
select curtime();
Execution result:
only print the current time
select now();
Execution result:
the current time and date will be printed
show variables like “%time_zone%”;
Execution result:
time_zone indicates that mysql uses the system time zone, and system_time_zone indicates that system uses the CST time zone
to modify
set global time_zone = '+8:00'; (modify the global time zone of MySQL to Beijing time, which is the East 8 district where we are located)
set time_zone = '+8:00'; (modify the time zone of the current session)
flush privileges; (immediately effective)
Execute show variables like '%time_zone%' after modification; result:
It is inconvenient to restart MySQL to temporarily solve the time zone problem
mysql_query("SET time_zone = '+8:00'");
Take PHP as an example, use it under mysql_connect(), it can be temporarily solved, and it can be dynamically adjusted when needed
Modify by modifying the configuration file mysql.cnf(my.ini)
Open the MySQL configuration file (my.cnf) with a suitable text editor and modify or add the following code:
[mysqld]
default-time-zone=+08:00 // Corresponding time zone setting
If it already exists, you can modify it, if it does not exist, you need to add it
After saving the configuration file and restarting the MySQL service , you can query the time zone again through the select statement to check whether the change takes effect
Summarize
The above is the content of this article. There are three methods to modify the MySQL time zone: 1. In the mysql command line mode, execute the "set global time_zone = 'time zone information'" command; 2. Temporarily solve the problem in PHP : Use mysql_query("SET time_zone = '+8:00'"); dynamically modify after PHP establishes a connection with MySQL; 3. In the "my.cnf(my.ini)" configuration file, find and set "default- Time_zone” item value can be modified to the required time zone, if not, you can add it yourself
If it is helpful to everyone, I hope you will support me three times.