MySQL如何設(shè)置時(shí)區(qū)和默認(rèn)編碼

情況描述

學(xué)習(xí)spring boot時(shí)要在windows本地安裝mysql5.7,配置好之后項(xiàng)目,啟動(dòng)之后提示需要設(shè)置時(shí)區(qū),并且在使用過(guò)程中發(fā)現(xiàn)出現(xiàn)亂碼,中文無(wú)法顯示,出現(xiàn)上述問(wèn)題的主要是MySQL5.7中沒(méi)有設(shè)置時(shí)區(qū)和編碼。

推薦:《mysql教程

解決方案

1、在spring boot的配置文件application.properties中設(shè)置MySQL的數(shù)據(jù)源url

spring.datasource.url=jdbc:mysql://localhost:3306/yunzhi_spring_boot?characterEncoding=utf-8&serverTimezone=GMT%2B8

解釋:

characterEncoding為設(shè)置數(shù)據(jù)庫(kù)編碼,采用utf-8;

serverTimezone為設(shè)置時(shí)區(qū),“GMT%2B8”即GMT+8,東八區(qū)北京時(shí)間;

有興趣可以了解一下GMT和UTC的區(qū)別。

2、如果每次都這樣設(shè)置,感覺(jué)好麻煩,直接修改MySQL的配置文件my.ini,設(shè)置兩個(gè)參數(shù):

default-time-zone=+08:00 character-set-server=utf8

然后保存,重啟MySQL服務(wù)。

項(xiàng)目中的MySQL url就可以簡(jiǎn)化為:

spring.datasource.url=jdbc:mysql://localhost:3306/yunzhi_spring_boot

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊6 分享