如何使 MySQL 的 NOW() 和 CURDATE() 函数使用 UTC?
我想让 MySQL 查询中对 NOW() 和 CURDATE() 的调用返回 UTC 日期。 如何在不检查和更改使用这些函数的所有查询的情况下实现这一点?
I want to make it so calls to NOW() and CURDATE() in MySQL queries return the date in UTC. How do I make this happen without going through and changing all queries that use these functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
以“YYYY-MM-DD hh:mm:ss”或 YYYYMMDDhhmmss.uuuuuu 格式的值返回当前 UTC 日期和时间,具体取决于该函数是在字符串还是数字上下文中使用。
以“YYYY-MM-DD”或 YYYYMMDD 格式的值返回当前 UTC 日期,具体取决于该函数是在字符串还是数字上下文中使用。
以“hh:mm:ss”或 hhmmss.uuuuuu 格式的值返回当前 UTC 时间,具体取决于该函数是在字符串还是数字上下文中使用。
MySQL 参考: https:// dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_utc-timestamp
Returns the current UTC date and time as a value in 'YYYY-MM-DD hh:mm:ss' or YYYYMMDDhhmmss.uuuuuu format, depending on whether the function is used in a string or numeric context.
Returns the current UTC date as a value in 'YYYY-MM-DD' or YYYYMMDD format, depending on whether the function is used in a string or numeric context.
Returns the current UTC time as a value in 'hh:mm:ss' or hhmmss.uuuuuu format, depending on whether the function is used in a string or numeric context.
MySQL reference: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_utc-timestamp
终于找到了我要找的东西......
在my.cnf中,
我将此选项放在[mysqld]下,并且mysql无法启动。
调用“SET time_zone='+0:00';” 在每个页面加载上也可以工作,但我不喜欢在每个页面加载上调用该查询的想法。
Finally found what I was looking for...
In my.cnf,
I was putting this option under [mysqld], and mysql was failing to start.
Calling "SET time_zone='+0:00';" on every page load would also work, but I don't like the idea of calling that query on every single page load.
将服务器的时钟设置为 UTC。 不完全是。
如果你能做到,就去做吧。
最令人头疼的一个是“cron”作业等,在本地时区运行,这意味着一些 cron 作业每年会错过一次,而所有其余的作业则在半年的不同时间 GMT 运行(我假设您所在的时区有夏令时)。
MySQL 有时区支持,但它很疯狂而且很混乱。 如果不需要,请不要使用它。 只需将服务器的时钟设置为 UTC,时间就会开始正常工作。
我知道更改服务器时钟对于任何托管系统来说都是一项重大更改,并且您可能有大量服务器和服务可能会受到影响,但无论如何请尝试这样做。 质量保证工作可能很重要,但请尝试一下。
Set your server's clock to UTC. No really.
If you can do it, do do it.
One of the biggest headaches is "cron" jobs etc, running in local time zone, this means that some cron jobs will get missed once a year, and all the rest run at a different time GMT for half the year (I'm assuming you're in a time zone which has daylight saving time here).
MySQL has time zone support, but it's crazy and messed up. Don't use it if you don't have to. Just set your server's clock to UTC and time will start working how it should.
I know that changing the server clock is a major change for any managed system, and you may have a large number of servers and services which could be affected, but please, try to do it anyway. The QA work may be significant, but try.
转到 /etc/mysql/my.cnf 文件并在 [mysqld] 部分下添加以下行
然后重新启动 mysql。 现在选择 curtime(); 显示 GMT 时间。
Goto /etc/mysql/my.cnf file and add this below line under [mysqld] section
Then restart your mysql. Now select curtime(); shows the GMT time.
如果更改正在运行的生产服务器上的时区或更新关键配置设置并重新启动 mysql 似乎不现实和/或过度,请尝试以下操作:
其中
US/Pacific
是您的NOW()< 的时区/code> 调用正在返回时间。
If changing the timezone on your running production servers or updating a key configuration setting and restarting mysql seems unrealistic and/or overkill, try this:
Where
US/Pacific
is the timezone yourNOW()
call is returning the time in.正确的方法是将服务器的时区更改为 UTC,正如 MarkR 所说。
但是,也可以使用 SET time_zone更改当前会话的时区。
从手册:
The proper way to do this is to change your server's time zone to UTC, just as MarkR said.
However, it's also possible to use SET time_zone to change the time zone for your current session.
From the manual:
您将需要使用 SET TIMESTAMP 语句将日期时间结果格式化为所需的格式。 这将意味着更改所有这些查询。 但 sysdate() 不会遵守这一点。
You will need to use the SET TIMESTAMP statement to format the datetime results in the desired format. This will mean changing all those queries. sysdate() will not obey this though.