How do I set the time zone of MySQL?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
MySQL, as a robust relational database management system, is often used in environments where timestamps are pivotal for database operations. Setting the correct time zone in MySQL is crucial for logging events, scheduling tasks, and providing accurate data handling across diverse geographic regions. This article covers the steps to set the time zone in MySQL, providing both a conceptual understanding and practical instructions.
Understanding MySQL Time Zones
MySQL operates through several time zone settings that can affect how data is stored and queried:
- System Time Zone: The time zone setting on the host operating system where your MySQL server is running.
- Server Time Zone: The time zone configuration for the MySQL server itself. This setting dictates how MySQL interprets time-related data when it deals with time functions.
- Session Time Zone: Each MySQL client session can operate in a different time zone from the server if required, enabling customized time handling for applications.
Key Commands for Time Zone Configuration
To work with time zones in MySQL, several important SQL commands exist:
- Set the Global Time Zone:
- Set the Session Time Zone:
- Verify Time Zone Settings:
Prerequisites
Before setting the time zone, ensure that the time zone tables in the MySQL database are populated. These tables are typically located in the mysql system database and can be populated if they aren't already:
Step-by-Step Guide
- Set the System Time Zone:The system time zone is configured on the operating system level. On a Linux system, this involves setting the correct time zone files in the
/etc/localtimeand/etc/timezoneconfigurations. - Configure MySQL Time Zone on Startup:You can set MySQL's server time zone by modifying the MySQL configuration file. Typically, this is located at
/etc/my.cnfor/etc/mysql/my.cnf. Insert or modify the[mysqld]section like so:
- Dynamic Time Zone Changes:For temporary changes without restarting the server, use SQL command
SET GLOBALfor server-wide changes orSETfor session-specific changes. - Persisting Changes:When you change the time zone using
SET GLOBAL, remember that it does not persist through server restarts unless specified in the configuration file.
Table: MySQL Time Zone Commands
| Description | Command | Note |
| Set global server time zone | SET GLOBAL time_zone = 'UTC'; | Requires SUPER privilege |
| Set session-specific time zone | SET time_zone = 'America/Los_Angeles'; | Can be used per connection |
| View current time zone settings | SHOW VARIABLES LIKE '%time_zone%'; | Displays both global and session time zones |
| Initialize time zone tables | mysql\_tzinfo\_to\_sql /usr/share/zoneinfo | mysql -u root -p mysql | Required if time zones aren't loaded |
Additional Considerations
- Time Zone Files: Ensure that the
zoneinfodatabase is properly installed on your system. This is often a package in OS repositories, such astzdataon Linux. - Daylight Saving Time: Handling daylight saving time transitions is automatic if the time zone data is properly installed and up-to-date.
- Performance Concerns: Consider the potential performance implications. Constantly using
CONVERT_TZin queries with diverse time zones could have overhead.
Troubleshooting
- Time Zone Not Found: If MySQL reports an unknown time zone, it indicates that the time zone tables may not have been initialized. Run the
mysql_tzinfo_to_sqlscript again. - Privilege Issues: Setting the global time zone might require
SUPERprivileges, ensure proper user privileges when executing related commands.
Setting the time zone in MySQL is crucial for accurate time-sensitive data management and ensuring consistency across databases and applications. Adhering to these guidelines facilitates efficient time zone handling, harnessing the full power of MySQL in diverse operational environments.
Related reading
- How do I show the schema of a table in a MySQL database?
- How do I specify unique constraint for multiple columns in MySQL?
- How do I specify unique constraint for multiple columns in MySQL?
- How do I start Mongo DB from Windows?
- How do I turn off the mysql password validation?
- How do I UPDATE a row in a table or INSERT it if it doesn't exist?
- How do I update an entity using spring-data-jpa?
- How do I update elements of a tensor using indices?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.