Sync MySQL Time
Luke Hebert
- 2 minutes read - 223 wordsIf your application logs show timestamps that do not line up with the server clock, the culprit is often a stale MySQL time reference. The fix is straightforward: confirm the mismatch, restart the service, and verify the correction.
Why This Happens
MySQL reads the system clock at startup and caches the timezone offset. If the host clock drifts, is manually adjusted, or the timezone configuration changes while MySQL is running, the database will keep reporting the old time until it is restarted.
Steps
Check the system date.
dateCheck what MySQL reports.
SELECT NOW();Compare the two values. If they differ by more than a few seconds, proceed.
Restart the MySQL service.
sudo systemctl restart mysqlOn systems that use the older service manager:
sudo service mysql restartVerify the fix.
SELECT NOW();The returned timestamp should now match the output of
dateon the host.
A Few Things to Keep in Mind
Restarting MySQL will briefly interrupt all active connections. Schedule the restart during a maintenance window if the instance is in production.
- If the mismatch persists after a restart, double-check the
default-time-zonesetting in your MySQL configuration file (/etc/mysql/mysql.conf.d/mysqld.cnfor/etc/my.cnf). - For long-running hosts, consider enabling
ntpdorchronydso the system clock stays accurate and the mismatch never builds up in the first place.