昨天在另一台电脑安装了mysql8.0,在用navicat打开和jdbc连接时遇到了很多匪夷所思的问题,记录下来
问题为啥产生俺不太清楚,貌似是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';
以后步骤相同。
我用到的环境是jdk1.8,mysql8.0,maven是3.4.5
在这里友情提示大家,选取mysql驱动的jar包时一定要匹配你的数据库版本,不然很容易出问题,相关依赖的信息可以去这个网站上搜索一下:http://mvnrepository.com
先看maven依赖的mysql驱动版本:
mysql
mysql-connector-java
8.0.12
因为我的数据库是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.(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的版本就是这么奇葩。。。。。这只是我上次连接时遇到的问题及解决方式,希望对大家有参考意义