在一台服务器上,当我运行:

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)

当前回答

如果使用指定时区会导致错误:

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

其他回答

在MySQL Workbench 8.0的服务器选项卡下,如果你选择状态和系统变量,你可以在这里设置它。

要在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和PHP的时区设置:

记住:

更改时区系统。Ubuntu的例子: $ sudo dpkg-reconfigure tzdata 重启服务器或者重启Apache 2和MySQL: /etc/init.d / mysql重启

您必须设置您的位置时区。接下来就是下面的过程 打开您的MSQLWorkbench 像这样写一个简单的SQL命令;

select now();

url也可以是这样的;

url = "jdbc:mysql://localhost:3306/your_database_name?serverTimezone=UTC";

如果有人正在使用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