1、问题描述
出错描述如题,SpringBoot后台应用程序使用了Druid连接池,SpringBoot后台服务程序在本地测试和上线一开始都没有问题(也可能是因为我没有长时间测试),后台程序在阿里云服务器上运行一段时间后,某天打开测试发现没有响应,查看系统错误日志,如下:
2020-04-06 00:01:00.641 [ERROR] c.alibaba.druid.pool.DruidDataSource - create connection error, url: jdbc:mysql://localhost/XXX?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8, errorCode 1698, state 28000
java.sql.SQLException: Access denied for user 'root'@'localhost'
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.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:148)
at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:217)
at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:142)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1466)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1529)
at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2178)
虽然有此错误,然而我用 Navicat Premium却可以连接登录,在Ubuntu的终端命令行也是可以登录的。
2、问题原因:
(1)MySQL的密码登录模式配置不对,在mysql.user表的用户表配置中。
打开我的mysql数据库user表发现是这样的:
我这里有两个root,然而他们的plugin模式是不一样的,数据库在验证的时候plugin方式不合适就会出错;
root的plugin有一个是auth_socket,用密码登陆的plugin应该是mysql_native_password 才对。
3、问题解决
解决方案:修改登录用户的plugin方式为mysql_native_password ,刷新权限,重启mysql。步骤如下:
mysql> select user,plugin from mysql.user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
| root | mysql_native_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)
mysql> update mysql.user set authentication_string=PASSWORD('yjf2019'),plugin='mysql_native_password' where user='root';
Query OK, 1 row affected, 1 warning (1.02 sec)
Rows matched: 2 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,plugin from mysql.user;
+------------------+-----------------------+
| user | plugin |
+------------------+-----------------------+
| root | mysql_native_password |
| mysql.session | mysql_native_password |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
| root | mysql_native_password |
+------------------+-----------------------+
5 rows in set (0.00 sec)
# 刷新后退出
mysql> flush privileges;
mysql> exit;
# 重启mysql
service mysql restart;
重启服务后访问就OK!
4、附:
我这里情况有些特殊,里面有两个root,正常来说只有一个root;把root的plugin方式修改为 mysql_native_password,然后刷新重启即可。步骤同上。
问题解决!