SoFunction
Updated on 2025-03-09

MySQL implementation method to view and modify time zones

1. Check the current time zone

Available@@global.time_zoneand@@session.time_zoneto get the global and current session time zone settings respectively.

-- Get the global time zone  
SELECT @@global.time_zone;  
  
-- Get the current session time zone  
SELECT @@session.time_zone;

You can also view the current time zone settings through the following command.

SHOW VARIABLES LIKE '%time_zone%';

2. Set the time zone

2.1. Global time zone

Note: Changes to the global time zone are requiredSUPERPermissions, and changes will only affect subsequent connections. Existing connections will not be affected.

SET GLOBAL time_zone = '+00:00';  -- UTC

2.2. Current session time zone

You can set a time zone for the current session, which only affects the current connection.

SET time_zone = '+00:00';  -- UTC

Alternatively, you can use a region identifier, for example:

SET time_zone = 'Asia/Shanghai';  -- Shanghai

3. Set the time zone in or

Can be configured in MySQL (usuallyorSet the default time zone in ) This affects all new connections.

exist[mysqld]Partially add the following line:

[mysqld]  
default-time-zone='+00:00'

Alternatively, use the region identifier:

[mysqld]  
default-time-zone='Asia/Shanghai'

After changing the configuration file, you need to restart the MySQL service for the changes to take effect.

This is the end of this article about MySQL viewing and modifying time zones. For more related content about MySQL viewing and modifying time zones, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!