Setting MySQL to UTC ensures consistent timestamps across different regions, preventing time zone issues, and can be done temporarily for sessions or permanently via configuration changes.
March 18, 2025
Photo by Pawel Czerwinski on Unsplash
Setting your MySQL time zone to UTC is a smart choice if you want consistent timestamps across different regions.
Whether you're logging events, running reports, or handling transactions, keeping everything in UTC avoids confusion caused by daylight savings time and local time differences.
Here’s how to do it easily.
Before making changes, check what time zone your MySQL server is using.
Open a MySQL command prompt and run:
SELECT @@global.time_zone, @@session.time_zone;
If you see SYSTEM, it means MySQL is using the server’s operating system time zone.
If it’s something else, MySQL has a manually set time zone.
If you just need to change the time zone for your current session (e.g., for a query or temporary testing), use:
SET time_zone = 'UTC';
This change only lasts until you disconnect from MySQL.
The next time you log in, MySQL will revert to its default time zone.
To make sure MySQL always uses UTC, follow these steps:
Open the MySQL configuration file (my.cnf
or my.ini
, depending on your system).
On Linux/macOS, it’s usually at:
/etc/mysql/my.cnf
On Windows, it’s typically found at:
C:\ProgramData\MySQL\MySQL Server X.X\my.ini
Add or update this line under the [mysqld]
section:
[mysqld]
default-time-zone = 'UTC'
Save the file and restart MySQL for the changes to take effect:
sudo systemctl restart mysql # Linux/macOS
net stop mysql && net start mysql # Windows
If you see an error like Unknown or incorrect time zone: 'UTC', MySQL might not have time zone tables loaded.
To fix this, run this command in a terminal (not MySQL):
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql
Then, try setting the time zone again.
Once you’ve set the time zone, confirm that it’s correctly applied by running:
SELECT @@global.time_zone, @@session.time_zone;
It should now return UTC
.
Setting MySQL to UTC ensures your timestamps stay accurate no matter where your users are.
Whether you're running a global app or just want to avoid timezone headaches, this simple tweak helps keep things consistent.