MySQL:在 my.cnf 选项文件中设置 time_zone

发布于 2024-10-09 14:07:43 字数 681 浏览 2 评论 0原文

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

是否可以将其放入选项文件中,例如 .my.cnf ?

当我尝试时,它不起作用。我得到的只是:

mysql:未知变量“time_zone=巴西/东部”

In MySQL you can set a session variable called time_zone to change the timezone. This is useful e.g. when looking at timestamps from another country. Here is an example:

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 ?

When I try, it doesn't work. All I get is:

mysql: unknown variable 'time_zone=Brazil/East'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清醇 2024-10-16 14:07:43

它应该是

default_time_zone=Brazil/East

详细信息:http://dev. mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_time_zone

选项文件格式 = default_time_zone

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

Option-File Format = default_time_zone

晌融 2024-10-16 14:07:43

我不确定 Xampp 中发生了什么变化,但只有当您将此行放在正确的位置时,此解决方案才有效。相信我,我尝试了很多次,并且必须进行相当彻底的搜索才能找到这个解决方案。

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

示例:

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

另外,如果您要使用“America/Los_Angeles”,您需要确保数据库中填充了正确的时区名称。我建议使用偏移量。我实际上建议使用 UTC 作为您的基础,然后根据用户的时区从该时间点转换您的时间,这将为您节省以后的许多麻烦,并保持您的数据库良好和统一。查看我在下面链接的指南,它为我解释得非常清楚,并且我使用了这个系统。有很多方法可以对其进行编码,但采用这种方法可以避免很多问题。

http:// /www.vertabelo.com/blog/technical-articles/the-proper-way-to-handle-multiple-time-zones-in-mysql

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.

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

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.

http://www.vertabelo.com/blog/technical-articles/the-proper-way-to-handle-multiple-time-zones-in-mysql

不忘初心 2024-10-16 14:07:43

~/.my.cnf中:

[mysql]
init_command="SET time_zone='Brazil/East'"

In ~/.my.cnf:

[mysql]
init_command="SET time_zone='Brazil/East'"
破晓 2024-10-16 14:07:43

对于 MAMP,我在 /Applications/MAMP/conf/my.cnf[mysqld] 下添加了 default_time_zone=-03:00

我会得到巴西/东部出现以下错误,可能是因为其已弃用(https://en.wikipedia.org/wiki /List_of_tz_database_time_zones):

[错误] 致命错误:非法或未知的默认时区“巴西/东部”

For MAMP, I added default_time_zone=-03:00 under [mysqld] in /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):

[ERROR] Fatal error: Illegal or unknown default time zone 'Brazil/East'

感性 2024-10-16 14:07:43

虽然有点晚了,但这可能还是有帮助的:

显式设置时区时,请确认您使用的是正确的时区名称,请记住许多时区名称已被弃用。您可以使用 https://en.wikipedia.org/wiki/List_of_tz_database_time_zones 进行确认。

就我而言,使用 MySQL 5.7,将已弃用的时区名称添加到我的 mysqld.cnf 文件中的 [mysqld] 下面时不起作用。使用新的时区名称并重新启动 mysql 服务,工作正常。

因此,对于这里的用户 @kev,使用 America/Sao_Paulo 应该有效,而不是使用 Brazil/East

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_zones to confirm.

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.

So for user @kev here, using America/Sao_Paulo should work, instead of using Brazil/East.

单身情人 2024-10-16 14:07:43

编辑以下内容:

nano /etc/mysql/conf.d/mysql.cnf && systemctl restart mysql ; systemctl status mysql

MySQL.cnf:

[mysql]
default_time_zone=America/Vancouver

请参阅此处了解当前时区格式 --> https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Edit the following:

nano /etc/mysql/conf.d/mysql.cnf && systemctl restart mysql ; systemctl status mysql

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

孤者何惧 2024-10-16 14:07:43

对于 Windows 10:

转到 C:\ProgramData\MySQL\MySQL Server 8.0 文件夹并以管理员身份打开 my.ini 文件。

滚动到 [mysqld]: 行。

[mysqld] 之后添加以下行:

#default time-zone
default-time-zone = '<your_time_zone>'

保存文件并从 Services 窗口重新启动 MySQL80 服务。

您可以控制更改

MySQL Workbench -> Local instance -> Server -> 
Status and System Variables -> System Variables -> Filtered -> time_zone

For Windows 10:

Go to C:\ProgramData\MySQL\MySQL Server 8.0 folder and open my.ini file as Administrator.

Scroll to the [mysqld]: line.

Add this lines after the [mysqld]:

#default time-zone
default-time-zone = '<your_time_zone>'

Save the file and restart the MySQL80 service from Services window.

You can control changes from

MySQL Workbench -> Local instance -> Server -> 
Status and System Variables -> System Variables -> Filtered -> time_zone
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文