Setting MySQL Time Zones: Complete Guide
In MySQL, you can change the time zone of the database by setting the system variable time_zone.
- Check the current time zone settings.
SELECT @@global.time_zone;
SELECT @@session.time_zone;
- Set the global time zone (with sufficient permissions).
SET GLOBAL time_zone = '+8:00'; # 设置为东八区
- Set the time zone for the session (only applies to the current session).
SET time_zone = '+8:00'; # 设置为东八区
- Modify the configuration file (my.cnf or my.ini): You can set the default time zone in the configuration file to automatically load when starting the MySQL server.
[mysqld]
default_time_zone = '+8:00' # 设置为东八区
Note: Please adjust ‘+8:00’ to the appropriate time zone representation as needed.