mysql时区查看与设置 mysql时区查看与设置方法
豁然高 人气:0一.查看数据库时区
show variables like'%time_zone'; mysql> show variables like "%time_zone"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CEST | | time_zone | SYSTEM | +------------------+--------+
1.全局参数system_time_zone
系统时区,在MySQL启动时会检查当前系统的时区并根据系统时区设置全局参数system_time_zone的值。
system_time_zone的值根据当前系统的不同会有所不同,此处测试时系统时间为CEST时间,所以值为CEST
查看当前的操作系统的时区
## 使用date命令 date +"%Z %z" //查看当前操作系统的时区 date -R
[vagrant@localhost ~]$ date -R Wed, 17 Jun 2020 10:48:14 +0200 [vagrant@localhost ~]$ date +"%Z %z" CEST +0200
CEST表示在mysql启动时,系统的时间为CEST
CEST为欧洲中部夏令时间,英文全名: Central European Summer Time
欧洲中部夏令时间所属时区: UTC/GMT +2
2.全局参数time_zone
用来设置每个连接会话的时区,默认为system时,使用全局参数system_time_zone的值。我们需要修改的就是time_zone的值
SYSTEM 表示time_zone默认使用system_time_zone的时区,此处即CEST
个人思路
因为my.cnf中默认没有设置default-time_zone,所以time_zone默认为system,即system_time_zone的值,
而system_time_zone的值为mysql启动时的操作系统的时区,所以个人认为可以通过提前设置操作系统的时区来决定mysql的时区
二.设置数据库时区
1.通过mysql命令行模式下动态修改,这种修改只在当前的mysql启动状态生效,如果mysql重启,则恢复到my.ini的设置状态
set global time_zone = '+8:00'; FLUSH PRIVILEGES;
再查看mysql的时区设置如下(需要退出mysql后,再重新登陆mysql,否则time_zone的结果可能不变,仍为SYSTEM)
mysql> show variables like "%time_zone"; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CEST | | time_zone | +08:00 | +------------------+--------+
2.通过修改配置文件来修改时区,这种修改永久生效,即使mysql重启也一样有效
windows系统中配置文件为my.ini。linux系统中配置文件为/etc/my.cnf
在[mysqld]的下面添加或者修改如下内容
default-time_zone = '+8:00'
修改完配置文件后需要重启mysql服务器,
linux系统中服务器重启命令如下
systemctl restart mysqld.service
my.cnf的修改后的内容如下所示
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock default-time_zone = '+9:00' # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
加载全部内容