MySQL:在 my.cnf 选项文件中设置 time_zone
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4562456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySQL: setting time_zone in my.cnf options file
提问by emx
In MySQL you can set a session variable called time_zoneto change the timezone. This is useful e.g. when looking at timestamps from another country. Here is an example:
在 MySQL 中,您可以设置一个名为time_zone的会话变量来更改时区。这很有用,例如当查看来自另一个国家的时间戳时。下面是一个例子:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2010-12-30 18:59:18 |
+---------------------+
1 row in set (0.00 sec)
mysql> set time_zone='Brazil/East';
Query OK, 0 rows affected (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2010-12-30 09:59:29 |
+---------------------+
1 row in set (0.00 sec)
Is it possible to put that in an option file e.g. .my.cnf ?
是否可以将其放在选项文件中,例如 .my.cnf ?
When I try, it doesn't work. All I get is:
当我尝试时,它不起作用。我得到的是:
mysql: unknown variable 'time_zone=Brazil/East'
mysql: unknown variable 'time_zone=Brazil/East'
回答by ajreal
it should be
它应该是
default_time_zone=Brazil/East
details : http://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_time_zone
详细信息:http: //dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_time_zone
Option-File Format = default_time_zone
选项文件格式 = default_time_zone
回答by Andrew
I'm not certain what has changed in Xampp, but this solution only works if you place this line in the proper place. Trust me I tried many times and had to do a pretty thorough search to find this solution.
我不确定 Xampp 中发生了什么变化,但只有将此行放在正确的位置,此解决方案才有效。相信我,我尝试了很多次,不得不进行非常彻底的搜索才能找到此解决方案。
default-time-zone = "+00:00"
Example:
例子:
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-time-zone = "+00:00" <--- Place here.
log_error = "mysql_error.log"
https://community.apachefriends.org/f/viewtopic.php?f=16&t=47656
https://community.apachefriends.org/f/viewtopic.php?f=16&t=47656
Also, you'll want to be sure that you have your database populated with the proper time zone names if you are going to use "America/Los_Angeles". I'd recommend using the offset. I'd actually recommend using UTC as your base then converting your time from that point for users based on their timezone which will save you many headaches later and keep your database nice and uniform. Check out the guide I linked below it explained it very clearly for me and I utilized this system. There are many ways to code it but taking this approach will save you a lot of issues.
此外,如果您要使用“America/Los_Angeles”,您还需要确保您的数据库填充了正确的时区名称。我建议使用偏移量。我实际上建议使用 UTC 作为您的基础,然后根据用户的时区将您的时间从该点转换为用户,这将在以后为您省去很多麻烦,并使您的数据库保持良好和统一。查看我在下面链接的指南,它为我解释得很清楚,我使用了这个系统。有很多方法可以对其进行编码,但是采用这种方法可以为您节省很多问题。
回答by AEQ
For MAMP, I added default_time_zone=-03:00
under [mysqld]
in /Applications/MAMP/conf/my.cnf
对于甲基苯丙胺,我说default_time_zone=-03:00
下[mysqld]
在/Applications/MAMP/conf/my.cnf
I would get the following error for Brazil/East, probably because its deprecated(https://en.wikipedia.org/wiki/List_of_tz_database_time_zones):
对于巴西/东部,我会收到以下错误,可能是因为它已被弃用(https://en.wikipedia.org/wiki/List_of_tz_database_time_zones):
[ERROR] Fatal error: Illegal or unknown default time zone 'Brazil/East'
[ERROR] Fatal error: Illegal or unknown default time zone 'Brazil/East'
回答by dolmen
In ~/.my.cnf
:
在~/.my.cnf
:
[mysql]
init_command="SET time_zone='Brazil/East'"
回答by FreeSoftwareServers
Edit the following:
编辑以下内容:
nano /etc/mysql/conf.d/mysql.cnf && systemctl restart mysql ; systemctl status mysql
MySQL.cnf:
MySQL.cnf:
[mysql]
default_time_zone=America/Vancouver
See here for current Time Zone formats --> https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
请参阅此处了解当前时区格式 --> https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
回答by ronan
A bit late, but this might be helpful nonetheless:
有点晚了,但这可能会有所帮助:
When explicitly setting the timezone, confirm that you are using the correct timezone name, keeping in mind the many have been deprecated. You can use https://en.wikipedia.org/wiki/List_of_tz_database_time_zonesto confirm.
明确设置时区时,请确认您使用的是正确的时区名称,请记住许多已被弃用。您可以使用https://en.wikipedia.org/wiki/List_of_tz_database_time_zones进行确认。
In my case, using MySQL 5.7, the deprecated timezone name was not working when adding it below the [mysqld] in my mysqld.cnf file. Using the new timezone name, and restarting the mysql service, worked.
在我的情况下,使用 MySQL 5.7,在我的 mysqld.cnf 文件中将其添加到 [mysqld] 下方时,不推荐使用的时区名称不起作用。使用新的时区名称,并重新启动 mysql 服务,工作。
So for user @kev here, using America/Sao_Pauloshould work, instead of using Brazil/East.
所以对于这里的用户@kev,使用America/Sao_Paulo应该可以工作,而不是使用Brazil/East。