本文翻译自:How do I set the time zone of MySQL?

On one server, when I run: 在一台服务器上,当我运行时:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2009-05-30 16:54:29 |
+---------------------+
1 row in set (0.00 sec)

On another server: 在另一台服务器上:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2009-05-30 20:01:43 |
+---------------------+
1 row in set (0.00 sec)

#1楼

参考:https://stackoom.com/question/3uAW/如何设置MySQL的时区


#2楼

If you are using the MySql Workbench you can set this by opening up the administrator view and select the Advanced tab. 如果使用的是MySql Workbench ,则可以通过打开管理员视图并选择“高级”选项卡来进行设置。 The top section is "Localization" and the first check box should be "default-time-zone". 顶部是“本地化”,第一个复选框应该是“默认时区”。 Check that box and then enter your desired time zone, restart the server and you should be good to go. 选中该框,然后输入所需的时区,重新启动服务器,您应该一切顺利。


#3楼

Simply run this on your MySQL server: 只需在您的MySQL服务器上运行此命令即可:

SET GLOBAL time_zone = '+8:00';

Where +8:00 will be your time zone. 其中+8:00将是您的时区。


#4楼

I thought this might be useful: 我认为这可能有用:

There are three places where the timezone might be set in MySQL: 在MySQL中可以在三个地方设置时区:

In the file "my.cnf" in the [mysqld] section 在[mysqld]部分的“ my.cnf”文件中

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

@@global.time_zone variable @@ global.time_zone变量

To see what value they are set to: 要查看它们设置为什么值:

SELECT @@global.time_zone;

To set a value for it use either one: 要为其设置值,请使用以下任一方法:

SET GLOBAL time_zone = '+8:00';
SET GLOBAL time_zone = 'Europe/Helsinki';
SET @@global.time_zone = '+00:00';

(Using named timezones like 'Europe/Helsinki' means that you have to have a timezone table properly populated.) (使用诸如“ Europe / Helsinki”之类的时区意味着您必须正确填充时区表。)

Keep in mind that +02:00 is an offset. 请记住,+ +02:00是偏移量。 Europe/Berlin is a timezone (that has two offsets) and CEST is a clock time that corresponds to a specific offset. Europe/Berlin是时区(具有两个偏移量), CEST是与特定偏移量相对应的时钟时间。

@@session.time_zone variable @@ session.time_zone变量

SELECT @@session.time_zone;

To set it use either one: 要设置它,请使用以下任一方法:

SET time_zone = 'Europe/Helsinki';
SET time_zone = "+00:00";
SET @@session.time_zone = "+00:00";

Both might return SYSTEM which means that they use the timezone set in my.cnf. 两者都可能返回SYSTEM,这意味着它们使用了my.cnf中设置的时区。

For timezone names to work, you must setup your timezone information tables need to be populated: http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html . 为了使时区名称起作用,必须设置时区信息表: http : //dev.mysql.com/doc/refman/5.1/en/time-zone-support.html 。 I also mention how to populate those tables in this answer . 我还提到了如何在此答案中填充这些表。

To get the current timezone offset as TIME 要将当前时区偏移量设为TIME

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

It will return 02:00:00 if your timezone is +2:00. 如果您的时区为+2:00,它将返回02:00:00。

To get the current UNIX timestamp: 要获取当前的UNIX时间戳:

SELECT UNIX_TIMESTAMP();
SELECT UNIX_TIMESTAMP(NOW());

To get the timestamp column as a UNIX timestamp 将timestamp列获取为UNIX时间戳

SELECT UNIX_TIMESTAMP(`timestamp`) FROM `table_name`

To get a UTC datetime column as a UNIX timestamp 获取UTC日期时间列作为UNIX时间戳记

SELECT UNIX_TIMESTAMP(CONVERT_TZ(`utc_datetime`, '+00:00', @@session.time_zone)) FROM `table_name`

Note: Changing the timezone will not change the stored datetime or timestamp , but it will show a different datetime for existing timestamp columns as they are internally stored as UTC timestamps and externally displayed in the current MySQL timezone. 注意:更改时区不会更改存储的datetime或timestamp ,但是它将为现有时间戳列显示不同的日期时间,因为它们在内部存储为UTC时间戳,并在外部显示在当前MySQL时区中。

I made a cheatsheet here: Should MySQL have its timezone set to UTC? 我在这里做了一个备忘单: MySQL是否应该将其时区设置为UTC?


#5楼

Keep in mind, that 'Country/Zone' is not working sometimes... This issue is not OS, MySQL version and hardware dependent - I've met it since FreeBSD 4 and Slackware Linux in year 2003 till today. 请记住,“国家/地区”有时无法使用...这个问题不依赖于操作系统,MySQL版本和硬件-从2003年FreeBSD 4和Slackware Linux到今天,我都遇到了这个问题。 MySQL from version 3 till latest source trunk. 从版本3到最新的源主干的MySQL。 It is ODD, but it DOES happens. 它是奇数,但确实会发生。 For example: 例如:

root@Ubuntu# ls -la /usr/share/zoneinfo/US
total 8drwxr-xr-x  2 root root 4096 Apr 10  2013 .
drwxr-xr-x 22 root root 4096 Apr 10  2013 ..
lrwxrwxrwx  1 root root   18 Jul  8 22:33 Alaska -> ../SystemV/YST9YDT
lrwxrwxrwx  1 root root   21 Jul  8 22:33 Aleutian -> ../posix/America/Adak
lrwxrwxrwx  1 root root   15 Jul  8 22:33 Arizona -> ../SystemV/MST7
lrwxrwxrwx  1 root root   18 Jul  8 22:33 Central -> ../SystemV/CST6CDT
lrwxrwxrwx  1 root root   18 Jul  8 22:33 Eastern -> ../SystemV/EST5EDT
lrwxrwxrwx  1 root root   37 Jul  8 22:33 East-Indiana -> ../posix/America/Indiana/Indianapolis
lrwxrwxrwx  1 root root   19 Jul  8 22:33 Hawaii -> ../Pacific/Honolulu
lrwxrwxrwx  1 root root   24 Jul  8 22:33 Indiana-Starke -> ../posix/America/Knox_IN
lrwxrwxrwx  1 root root   24 Jul  8 22:33 Michigan -> ../posix/America/Detroit
lrwxrwxrwx  1 root root   18 Jul  8 22:33 Mountain -> ../SystemV/MST7MDT
lrwxrwxrwx  1 root root   18 Jul  8 22:33 Pacific -> ../SystemV/PST8PDT
lrwxrwxrwx  1 root root   18 Jul  8 22:33 Pacific-New -> ../SystemV/PST8PDT
lrwxrwxrwx  1 root root   20 Jul  8 22:33 Samoa -> ../Pacific/Pago_Pago
root@Ubuntu#

And a statement like that is supposed to work: 这样的语句应该起作用:

SET time_zone='US/Eastern';

But you have this problem: 但是你有这个问题:

Error Code: 1298. Unknown or incorrect time zone: 'EUS/Eastern' 错误代码:1298。未知或不正确的时区:'EUS / Eastern'

Take a look at the subfolder in your zone information directory, and see the ACTUAL filename for symlink, in this case it's EST5EDT. 查看区域信息目录中的子文件夹,并查看符号链接的ACTUAL文件名,在本例中为EST5EDT。 Then try this statement instead: 然后尝试使用以下语句:

SET time_zone='EST5EDT';

And it's actually working as it is supposed to! 它实际上正在按预期的方式工作! :) Keep this trick in mind; :)记住这个技巧; I haven't seen it to be documented in MySQL manuals and official documentation. 我还没有看到它在MySQL手册和官方文档中有记录。 But reading the corresponding documentation is must-do thing: MySQL 5.5 timezone official documentation - and don't forget to load timezone data into your server just like that (run as root user!): 但是必须阅读相应的文档: MySQL 5.5时区官方文档 -并且不要忘记像那样将时区数据加载到服务器中(以root用户身份运行!):

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

Trick number one - it must be done exactly under MySQL root user. 第一招-必须完全在MySQL root用户下完成。 It can fail or produce non-working result even from the user that has full access to a MySQL database - I saw the glitch myself. 即使是对MySQL数据库具有完全访问权限的用户,它也可能失败或产生无法正常工作的结果-我自己也看到了故障。


#6楼

For anyone still having this issue: 对于仍然有此问题的任何人:

value="jdbc:mysql://localhost:3306/dbname?serverTimezone=UTC"

Worked for me. 为我工作。 Just append ?serverTimezone=UTC at the end. 只需在末尾附加?serverTimezone=UTC

如何设置MySQL的时区?相关推荐

  1. mysql 时区设定_设置MySQL默认时区

    MySQL默认的时区是UTC时区,比北京时间晚8个小时. 假设日志里面的时间是中午12:00,那么北京时间应该是晚上的8:00点钟. 为方便查看和设置时间,我们要设置MySQL的默认时区,以符合本地使 ...

  2. mysql中如何设置时区_如何设置MySQL的时区?

    我认为这可能是有用的: 有三个位置可以在MySQL中设置时区: 在[mysqld]部分中的"my.cnf"文件中default-time-zone='+00:00' @global ...

  3. IDEA连接mysql出现时区错误_idea连接数据库时区错误

    错误界面 IDEA连接mysql,地址,用户名,密码,数据库名,全都配置好了,点测试连接,咔!不成功! 界面是这样的, 翻译过来就是:服务器返回无效时区.进入"高级"选项卡,手动设 ...

  4. mysql印度时区_一次 JDBC 与 MySQL 因 “CST” 时区协商误解导致时间差了 14 或 13 小时的排错经历...

    CST 时区 名为 CST 的时区是一个很混乱的时区,有四种含义: 美国中部时间 Central Standard Time (USA) UTC-06:00 澳大利亚中部时间 Central Stan ...

  5. mysql 时区设定_如何设置MySQL 时区

    MySQL 时区默认是服务器的时区.在使用中经常会遇见. 可以通过以下命令查看 查看时区 SQL代码. mysql> show variables like '%time_zone%'; . + ...

  6. mysql的时区设置

    mysql的时区设置 IDEA配置mysql数据库时,地址,用户名,密码,数据库名填写之后,点测试连接,提示 Server returns invalid timezone. Go to 'Advan ...

  7. mysql中国时区_mysql时区设置为东八区

    场景:后台返回给页面的时间统一差8小时. 分析:差八小时,应该是时区问题.具体的是哪一层出的问题呢,mybatis?mysql?系统时间? 解析: 1.查询mysql时区(正常) 输入show var ...

  8. docker设置mysql时区

    永久设置时区 进入docker容器 $ docker exec -it 容器ID 或 name bash 输入 $ date 结果: Sun Jan 23 04:59:46 UTC 2022 设置my ...

  9. 数据库时区那些事儿 - MySQL的时区处理

    原文地址 当JVM时区和数据库时区不一致的时候,会发生什么?这个问题也许你从来没有注意过,但是当把Java程序容器化的时候,问题就浮现出来了,因为目前几乎所有的Docker Image的时区都是UTC ...

最新文章

  1. 【Qt中文手册】QObject
  2. Elasticsearch之分布式介绍
  3. 对页面制定区域进行打印,以及打印不显示页脚URL的方法
  4. display:inline、block、inline-block的区别
  5. hash hashcode变化_没想到 Hash 冲突还能这么玩,你的服务中招了吗?
  6. Android Studio 加载网络图片
  7. Redis 学习---(6)Redis 键(key)
  8. android window设置动画,android - 具有动画的Windowmanager
  9. SQL Server存储过程初学者
  10. Debian/Ubuntu - 解决Root用户不能远程连接服务器的问题
  11. SqlServer中获取字符串中的数字部分
  12. npp 插件html,Notepad++中常用的插件
  13. java验证码 验证
  14. 无纸化会议桌的应用及说明
  15. 怎么在档案写自己的计算机水平,关于个人档案怎么写范文
  16. Windows C盘清理
  17. 小程序12306服务器,微信小程序12306来了!史上最详细体验出炉!
  18. 【机器学习】数据驱动方法在电网稳定分析应用浅谈
  19. Pandas数据结构
  20. 搜狗输入法取消软键盘

热门文章

  1. Android开发出现Warning:Gradle version 2.10 is required. Current version is 2.8
  2. 现在的Android程序员为什么会感到焦虑?焦虑的源头在哪里?该怎么去缓解焦虑呢?——没有无中生有的贩卖焦虑,只有你的挣扎和不甘。
  3. Java IO 体系(一): 装饰者模式
  4. 解决Android Studio报错:DefaultAndroidProject : Unsupported major.minor version 52.0
  5. 第四周项目五-用递归方法求解(输出Fibnacci序列的第20个数)
  6. java多层catch语句_Java异常之catch语句块
  7. 打开Eclipse时出现Failed to create the Java Virtual Machine
  8. Android10.0 四大组件与进程启动间关系
  9. 一个页面区分管理者和普通用户如何设计_产品经理要做的操作权限/数据权限设计...
  10. 几个UI Prototype应用的使用感受