盤點mysql8.0遇到的那些坑(navicat和JDBC連結)
昨天在另一臺電腦安裝了mysql8.0,在用navicat開啟和jdbc連線時遇到了很多匪夷所思的問題,記錄下來
一 navicat
一 開啟連線,顯示1251....,如圖
問題為啥產生俺不太清楚,貌似是mysql的密碼的加密方式變了,需要重新改變一下,問題解決步驟如下:
第一步:命令列進入mysql的bin目錄(配置過mysql環境變數的跳過這一步)
cd C:\Program Files\MySQL\MySQL Server 8.0\bin
第二步:登入mysql的root賬戶(別忘了回車-輸密碼-回車)
mysql -uroot -p
第 三步 :更改mysql的加密方式
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
這句話直接複製,回車執行就可以
然後更改密碼
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';
再改回來
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
重新整理
FLUSH PRIVILEGES;
此時,再去連結navicat就基本可以了,截圖為證
但是,此時有些小夥伴可能會報錯:ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%':
或者你用到的使用者不是root,那麼此時你應該登入root賬戶,執行以下語句
select user,host from user;
將對應的localhost換為你查到的host值,例如
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'xiaolizi';
以後步驟相同。
二 : JDBC連結mysql8.0
我用到的環境是jdk1.8,mysql8.0,maven是3.4.5
在這裡友情提示大家,選取mysql驅動的jar包時一定要匹配你的資料庫版本,不然很容易出問題,相關依賴的資訊可以去這個網站上搜索一下:http://mvnrepository.com
先看maven依賴的mysql驅動版本:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
因為我的資料庫是8.0.12,所以我就選了這個版本的依賴,如果你用低於這個版本的依賴,可能會出現各種錯誤,這裡不一一列舉,下面所有出現的錯誤型別都有可能是這裡引起的
第一種型別:
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
這個錯誤不影響執行,但是對於有強迫症的人來說簡直凌然發至,解決也比較簡答,程式碼如下
String driver="com.mysql.cj.jdbc.Driver";
//把com.mysql.jdbc.Driver改成這個
有時間繼續寫,陪物件去逛街
-------------------------------------------------------------我是一條可愛的分割線------------------------------------------------------------------------------
今天公司環境崩了,繼續記錄上週遺留下來的問題。。(為啥沒人給我評論!你們就是這麼欺負一個勤勞的小菜鳥的嗎)
第一種型別:
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at Test.App.main(App.java:23)
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:128)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2236)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2260)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1314)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:963)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822)
... 6 more
那麼,請將連線程式碼改為:
String url ="jdbc:mysql://localhost:3306/bos32?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true";
具體為啥,我還真不知道。。。知道mysql8.0的版本就是這麼奇葩。。。。。這只是我上次連線時遇到的問題及解決方式,希望對大家有參考意義