一.查看數(shù)據(jù)庫(kù)時(shí)區(qū)
show variables like'%time_zone';
mysql> show variables like "%time_zone";
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CEST |
| time_zone | SYSTEM |
+------------------+--------+
1.全局參數(shù)system_time_zone
系統(tǒng)時(shí)區(qū),在MySQL啟動(dòng)時(shí)會(huì)檢查當(dāng)前系統(tǒng)的時(shí)區(qū)并根據(jù)系統(tǒng)時(shí)區(qū)設(shè)置全局參數(shù)system_time_zone的值。
system_time_zone的值根據(jù)當(dāng)前系統(tǒng)的不同會(huì)有所不同,此處測(cè)試時(shí)系統(tǒng)時(shí)間為CEST時(shí)間,所以值為CEST
查看當(dāng)前的操作系統(tǒng)的時(shí)區(qū)
## 使用date命令
date +"%Z %z" //查看當(dāng)前操作系統(tǒng)的時(shí)區(qū)
date -R
[vagrant@localhost ~]$ date -R
Wed, 17 Jun 2020 10:48:14 +0200
[vagrant@localhost ~]$ date +"%Z %z"
CEST +0200
CEST表示在mysql啟動(dòng)時(shí),系統(tǒng)的時(shí)間為CEST
CEST為歐洲中部夏令時(shí)間,英文全名: Central European Summer Time
歐洲中部夏令時(shí)間所屬時(shí)區(qū): UTC/GMT +2
2.全局參數(shù)time_zone
用來(lái)設(shè)置每個(gè)連接會(huì)話的時(shí)區(qū),默認(rèn)為system時(shí),使用全局參數(shù)system_time_zone的值。我們需要修改的就是time_zone的值
SYSTEM 表示time_zone默認(rèn)使用system_time_zone的時(shí)區(qū),此處即CEST
個(gè)人思路
因?yàn)閙y.cnf中默認(rèn)沒(méi)有設(shè)置default-time_zone,所以time_zone默認(rèn)為system,即system_time_zone的值,
而system_time_zone的值為mysql啟動(dòng)時(shí)的操作系統(tǒng)的時(shí)區(qū),所以個(gè)人認(rèn)為可以通過(guò)提前設(shè)置操作系統(tǒng)的時(shí)區(qū)來(lái)決定mysql的時(shí)區(qū)
二.設(shè)置數(shù)據(jù)庫(kù)時(shí)區(qū)
1.通過(guò)mysql命令行模式下動(dòng)態(tài)修改,這種修改只在當(dāng)前的mysql啟動(dòng)狀態(tài)生效,如果mysql重啟,則恢復(fù)到my.ini的設(shè)置狀態(tài)
set global time_zone = '+8:00';
FLUSH PRIVILEGES;
再查看mysql的時(shí)區(qū)設(shè)置如下(需要退出mysql后,再重新登陸mysql,否則time_zone的結(jié)果可能不變,仍為SYSTEM)
mysql> show variables like "%time_zone";
+------------------+--------+
| Variable_name | Value |
+------------------+--------+
| system_time_zone | CEST |
| time_zone | +08:00 |
+------------------+--------+
2.通過(guò)修改配置文件來(lái)修改時(shí)區(qū),這種修改永久生效,即使mysql重啟也一樣有效
windows系統(tǒng)中配置文件為my.ini。linux系統(tǒng)中配置文件為/etc/my.cnf
在[mysqld]的下面添加或者修改如下內(nèi)容
default-time_zone = '+8:00'
修改完配置文件后需要重啟mysql服務(wù)器,
linux系統(tǒng)中服務(wù)器重啟命令如下
systemctl restart mysqld.service
my.cnf的修改后的內(nèi)容如下所示
# 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
到此這篇關(guān)于mysql時(shí)區(qū)查看與設(shè)置方法的文章就介紹到這了,更多相關(guān)mysql時(shí)區(qū)查看與設(shè)置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!