Fix MySQL Wrong Timezone on Linux: Why Your Database Insert Time is Off
Imagine debugging a critical error. Your application logs say the crash happened at 10:00 AM, but when you check your database for the transaction, the latest record is stamped 03:00 AM. Confusion sets in.
This 7-hour time difference (usually caused by the server defaulting to UTC while you live in WIB/GMT+7) creates absolute chaos for data consistency, cron job scheduling, and daily reporting. If you don't fix this, your e-commerce orders might appear as if they were purchased "yesterday".
(Speaking of schedules, if your Cron Jobs are running at the wrong time too, check my guide on Mastering Linux Cron Jobs to ensure your automation triggers correctly).
In this guide, I will show you how to force MySQL to respect your Linux server's local timezone permanently.
Step 1: Check and Fix Linux Timezone
First, we must ensure the Operating System itself knows where it is located. Run this command in your terminal to check the status:
timedatectl
If the output shows Time zone: UTC, you need to change it to your local region (e.g., Jakarta, Bangkok, or New York). This ensures system logs match your wall clock:
timedatectl set-timezone Asia/Jakarta
Step 2: The MySQL Config Fix (Permanent Method)
Many tutorials suggest running SET GLOBAL time_zone via SQL query. However, this is temporary. If your server restarts, MySQL reverts to UTC. We need to edit the configuration file for a permanent fix.
- Open the config file:
nano /etc/my.cnf(or via aaPanel > App Store > MySQL > Config). - Scroll down to the
[mysqld]section. - Add this specific line to force the offset:
default-time-zone = '+07:00'
(Replace +07:00 with your specific timezone offset).
Finally, restart the service using service mysqld restart. You can verify it by running SELECT NOW(); in your database manager. It should now show the correct local time.
Conclusion
Time synchronization is often overlooked by new SysAdmins, but it is critical for data integrity. By aligning your OS and Database timezones, you ensure that your logs, reports, and automation tasks all speak the same temporal language.
Author: Marg | Daily Innovate Tech

Post a Comment for "Fix MySQL Wrong Timezone on Linux: Why Your Database Insert Time is Off"