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