mysql 时区设定_将MySQL数据库时区设置为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.

解决方案

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

You can retrieve the server and client time_zone settings:

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

You can also change the client timezone, or the timezone for the entire MySQL instance.

But be keenly AWARE of the implication that has 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, modify the /etc/my.cnf file (or wherever your mysql instance initialization parameters are read from), under the [mysqld] section:

[mysqld]

default-time-zone='+00:00'

-- or --

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.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值