Timezone issue when migrating to new mysql server

I have a Laravel application currently on production. Recently I was under a situation where I had to move the staging database to a new the new server with latest OS. This is one of the exercise that my company does.

Since it is staging, I can take a downtime. Simply took a dump from mysqldump and imported to the new server with mysql command.

My Laravel application connects to the database with default timezone as UTC on staging because the SYSTEM time on Staging is Local time i.e IST.

'timezone' => env('APP_ENV') == 'staging' ? 'UTC' : null,

After I deployed the application on staging I got below error.

SQLSTATE[HY000]: General error: 1298 Unknown or incorrect time zone: 'UTC'

After quick google, I got to know that MySQL needs to have timezone info in the mysql database. Which is usually present in the /usr/share/zoneinfo directory. We can use the mysql_tzinfo_to_sql command provide by mysql server to import the timezone information into the database.

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql -p

Above command will fetch all the timezone info from our Linux machine and dump into the mysql database.

That’s it. My staging setup was up again. Thanks for reading.

Leave a comment