在一台服务器上,当我运行:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2009-05-30 16:54:29 |
+---------------------+
1 row in set (0.00 sec)
在另一台服务器上:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2009-05-30 20:01:43 |
+---------------------+
1 row in set (0.00 sec)
要在MariaDB中设置标准时区,必须转到50-server.cnf文件。
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf
然后您可以在mysqld部分中输入以下条目。
default-time-zone='+01:00'
例子:
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
#
# See the examples of server my.cnf files in /usr/share/mysql/
#
# this is read by the standalone daemon and embedded servers
[server]
# this is only for the mysqld standalone daemon
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
### Default timezone ###
default-time-zone='+01:00'
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
必须通过配置文件进行更改,否则MariaDB服务器重启后将重置mysql表!
请记住,“国家/地区”有时不起作用……这个问题与操作系统、MySQL版本和硬件无关——我从2003年FreeBSD 4和Slackware Linux到今天都遇到过这个问题。MySQL从版本3到最新的源中继。这很奇怪,但确实发生了。例如:
root@Ubuntu# ls -la /usr/share/zoneinfo/US
total 8
drwxr-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#
这样的陈述应该是有用的:
SET time_zone='US/Eastern';
但你有一个问题:
错误码:1298。未知或不正确的时区:“EUS/Eastern”
查看区域信息目录中的子文件夹,并查看符号链接的实际文件名,在本例中为EST5EDT。那就试试下面这句话:
SET time_zone='EST5EDT';
而且它确实像它应该的那样工作!:)记住这个技巧;我还没有在MySQL手册和官方文档中看到它的文档。但是阅读相应的文档是必须要做的事情:MySQL 5.5时区官方文档-不要忘记像这样将时区数据加载到服务器中(以root用户运行!):
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
技巧1 -它必须完全在MySQL根用户下完成。即使用户对MySQL数据库有完全的访问权限,它也可能失败或产生无法工作的结果-我自己看到了故障。
如果使用指定时区会导致错误:
mysql> SET GLOBAL time_zone = "Asia/Bangkok";
ERROR 1298 (HY000): Unknown or incorrect time zone: 'Asia/Bangkok'
你可以试试:
mysql_tzinfo_to_sql tz_file tz_name | mysql -u root -p mysql
然后:
SET GLOBAL time_zone = "Asia/Bangkok";
SET time_zone = "+07:00";
SET @@session.time_zone = "+07:00";
检查一下现在几点了:
SELECT NOW();
裁判:
https://dev.mysql.com/doc/refman/5.7/en/time-zone-support.html#time-zone-installation
如果有人正在使用GoDaddy共享主机,你可以尝试以下解决方案,为我工作。
当启动DB连接时,在我的PDO对象中设置time_zone命令,例如:
$pdo = new PDO($dsn, $user, $pass, $opt);
$pdo->exec("SET time_zone='+05:30';");
其中“+05:30”是印度时区。您可以根据需要更改。
在那之后;所有与日期和时间相关的MySQL进程都设置了所需的时区。
来源:https://in.godaddy.com/community/cPanel-Hosting/How-to-change-TimeZone-for-MySqL/td-p/31861