mysql時區(qū)查看與設置方法
一.查看數據庫時區(qū)
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
系統(tǒng)時區(qū),在MySQL啟動時會檢查當前系統(tǒng)的時區(qū)并根據系統(tǒng)時區(qū)設置全局參數system_time_zone的值。
system_time_zone的值根據當前系統(tǒng)的不同會有所不同,此處測試時系統(tǒng)時間為CEST時間,所以值為CEST
查看當前的操作系統(tǒng)的時區(qū)
## 使用date命令 date +"%Z %z" //查看當前操作系統(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.全局參數time_zone
用來設置每個連接會話的時區(qū),默認為system時,使用全局參數system_time_zone的值。我們需要修改的就是time_zone的值
SYSTEM 表示time_zone默認使用system_time_zone的時區(qū),此處即CEST
個人思路
因為my.cnf中默認沒有設置default-time_zone,所以time_zone默認為system,即system_time_zone的值,
而system_time_zone的值為mysql啟動時的操作系統(tǒng)的時區(qū),所以個人認為可以通過提前設置操作系統(tǒng)的時區(qū)來決定mysql的時區(qū)
二.設置數據庫時區(qū)
1.通過mysql命令行模式下動態(tài)修改,這種修改只在當前的mysql啟動狀態(tài)生效,如果mysql重啟,則恢復到my.ini的設置狀態(tài)
set global time_zone = '+8:00'; FLUSH PRIVILEGES;
再查看mysql的時區(qū)設置如下(需要退出mysql后,再重新登陸mysql,否則time_zone的結果可能不變,仍為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]的下面添加或者修改如下內容
default-time_zone = '+8:00'
修改完配置文件后需要重啟mysql服務器,
linux系統(tǒng)中服務器重啟命令如下
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
到此這篇關于mysql時區(qū)查看與設置方法的文章就介紹到這了,更多相關mysql時區(qū)查看與設置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳細介紹基于MySQL的搜索引擎MySQL-Fullltext
這篇文章主要詳細介紹基于MySQL的搜索引擎MySQL-Fullltext,需要用到C和C#以及JavaScript的知識,屬于MySQL的高階應用,需要的朋友可以參考下2015-04-04