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

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)

当前回答

如果符合您的场景,您可以通过更改操作系统时区轻松做到这一点。

在Ubuntu系统中,使用此命令可以列出时区

sudo timedatectl list-timezones

如果需要修改操作系统的时区,需要同时执行该命令

timedatectl set-timezone America/New_York

检查操作系统时区,执行

date

然后重新启动MySQL

sudo service mysql restart

要在MySQL中检查时区,请登录并运行

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

其他回答

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

要为当前会话设置它,请执行:

SET time_zone = timezonename;

要在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> 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中有三个地方可以设置时区:

在[mysqld]部分的“my.cnf”文件中

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

@@global。time_zone变量

要查看它们被设置为什么值:

SELECT @@global.time_zone;

要为它设置一个值,可以使用其中任何一个:

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

(使用像“欧洲/赫尔辛基”这样的命名时区意味着你必须有一个正确填充的时区表。)

记住+02:00是一个偏移量。欧洲/柏林是一个时区(有两个偏移量),CEST是一个时钟时间,对应于特定的偏移量。

@@session。time_zone变量

SELECT @@session.time_zone;

要设置它,可以使用任何一个:

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

两者都可能返回SYSTEM,这意味着它们使用my.cnf中设置的时区。

要使时区名称生效,必须设置需要填充的时区信息表:http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html。在这个回答中,我还提到了如何填充这些表。

获取当前时区偏移量为TIME

SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP);

如果您的时区是+2:00,它将返回02:00:00。

获取当前UNIX时间戳:

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

获取时间戳列作为UNIX时间戳

SELECT UNIX_TIMESTAMP(`timestamp`) FROM `table_name`

获取一个UTC datetime列作为UNIX时间戳

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

注意:改变时区不会改变存储的日期时间或时间戳,但它会为现有的时间戳列显示不同的日期时间,因为它们在内部存储为UTC时间戳,而在外部显示为当前MySQL时区。

我在这里做了一个备忘单:MySQL是否应该将它的时区设置为UTC?