Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set MySQL database timezone to GMT

I need to change the timezone of a single database is this possible?

I know we can change the timezone within our WHM (we are using a dedicated server from hostgator), however a large number of legacy software running on the server has a lot of +6 hours coding in it (i.e. server timezone is CST, we wanted the time in GMT so previous developers altered date/times manually within the code - bad!).

I am now working on a new software and would like to have it all in GMT, I know I can use date_default_timezone_set('GMT') however that will not solve MySQL inserts where the datetime column is set to CURRENT_TIMESTAMP as it will insert @ CST timezone.

like image 687
buzzmonkey Avatar asked Dec 17 '12 15:12

buzzmonkey


People also ask

How do I set the time zone on my database?

Use the ALTER DATABASE SET TIME_ZONE command to change the time zone of a database. This command takes either a named region such as America/Los_Angeles or an absolute offset from UTC. This example sets the time zone to UTC: ALTER DATABASE SET TIME_ZONE = '+00:00';

How do I set timezone in MySQL 8?

You can set the time zone for the server with the --timezone= timezone_name option to mysqld_safe. You can also set it by setting the TZ environment variable before you start mysqld. The permissible values for --timezone or TZ are system dependent.

How do I change my timezone in my CNF?

You can also set MySQL server time zone in your server configuration file my. cnf. Open the file in a terminal. Change the '+00:00' to your time zone's GMT offset, such as '-6:00'.

How do I populate MySQL database time zone?

The utility being used in the question is a script bundled with MySQL Server called mysql_tzinfo_to_sql . It reads your Linux (or FreeBSD, Solaris, or macOS) system's time zone database and creates SQL statements from the information it discovers, that will load the time zone tables in MySQL.


1 Answers

No, it's not possible to change the timezone for a single database within a MySQL instance.

We can retrieve the server and client time_zone settings with a query, like this:

SELECT @@global.time_zone, @@session.time_zone;

We can also change the client timezone for a session, or change the timezone for the entire MySQL instance.

But we need to be keenly aware of the implication that this change will have on existing client connections, and the how DATETIME and TIMESTAMP values already stored in the instance will be interpreted.

To have the server time_zone set at MySQL instance startup, we can modify the /etc/my.cnf file (or wherever the mysql instance initialization parameters are read from), under the [mysqld] section:

[mysqld]
default-time-zone='+00:00' 

-- or --

It is also possible (less desirable) to add the --default_time_zone='+00:00' option to mysqld_safe

NOTE: Changing the timezone setting on the MySQL server does NOT change the values stored in existing DATETIME or TIMESTAMP columns, BUT since it does effectively change the context in which those stored values are interpreted, it will look like all of the values ARE shifted. (Where 08:00 was taken to mean 8AM CST, with the time_zone of the server changed from CST to GMT, that same '08:00' will now be taken to be 8AM GMT, which would effectively be 2AM CST.

Also keep in mind that TIMESTAMP columns are always stored in UTC, while DATETIME columns do not have a timezone. http://dev.mysql.com/doc/refman/5.5/en/datetime.html

Each client session can change the timezone setting for their own session:

SET time_zone='-06:00';

But none of this really "solves" the timezone conversion problem, it just moves the conversion problem around.

There's nothing inherently "bad" with the application layer handling timezone conversions; sometimes, that's the best place to handle. It just has to be done correctly and consistently.

(What's odd about the setup you describe is that the app is storing DATETIME values as if the MySQL server time_zone is set to GMT, but the MySQL server time_zone is set to something else.)

like image 83
spencer7593 Avatar answered Sep 22 '22 15:09

spencer7593