求解mybatis访问mysql数据库报错

错误日志:

2020-05-26 22:01:27.366 ERROR 13440 --- [reate-544405118] com.alibaba.druid.pool.DruidDataSource   : create connection SQLException, url: jdbc:mysql://192.168.124.10:3306/mybatis?autoReconnect=true&serverTimezone=UTC&useSSL=false, errorCode 0, state 08001

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
	at sun.reflect.GeneratedConstructorAccessor31.newInstance(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_60]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_60]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2094) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2019) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:776) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at sun.reflect.GeneratedConstructorAccessor29.newInstance(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_60]
	at java.lang.reflect.Constructor.newInstance(Constructor.java:422) ~[na:1.8.0_60]
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:149) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.FilterAdapter.connection_connect(FilterAdapter.java:786) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.FilterEventAdapter.connection_connect(FilterEventAdapter.java:38) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.FilterAdapter.connection_connect(FilterAdapter.java:786) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.stat.StatFilter.connection_connect(StatFilter.java:218) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.filter.FilterChainImpl.connection_connect(FilterChainImpl.java:143) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1515) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1578) ~[druid-1.1.8.jar:1.1.8]
	at com.alibaba.druid.pool.DruidDataSource$CreateConnectionThread.run(DruidDataSource.java:2451) ~[druid-1.1.8.jar:1.1.8]
Caused by: java.sql.SQLException: Unable to load authentication plugin 'caching_sha2_password'.
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:869) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:865) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1746) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1226) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2188) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2035) ~[mysql-connector-java-5.1.45.jar:5.1.45]
	... 20 common frames omitted


Process finished with exit code 1

Mysql本地配置my.ini

[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64/data
#错误消息文件存在的目录
lc-messages-dir=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64/share 
#错误消息的语言环境。默认是 en_US。可将参数转换为其他语言  
lc-messages="en_US"
#错误日志显示的时间
log_timestamps=SYSTEM
#关闭普通日志
general-log=0
#二进制文件      
general_log_file=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64/data/iris_bin.log 
#慢日志文件
slow_query_log_file=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64/data/iris-slow.log
#慢查询时间阈值
slow-query-log=1
long_query_time=1
# Binary Logging.
log-bin=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64/data/iris-bin
log-error=D:/mysql-8.0.19-winx64/mysql-8.0.19-winx64/data/iris-err.err 
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
#默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
#跳过Mysql密码认证
#skip-grant-tables 
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8

问题原因

pom文件中引入的mysql-connector-java依赖包与docker容器中安装的mysql版本不一致,而且一直没注意到自己只修改了本地的mysql配置文件,而我访问的是docker服务器上的mysql,需要修改的是docker中mysql容器的配置文件

  1. 删除本地maven仓库中旧的mysql-connector-java版本的jar包
    2.修改docker服务器中mysql容器的配置文件/etc/mysql/my.cnf,(修改方法参考docker修改mysql配置):

主要步骤:
1.将配置从容器中copy到宿主机
docker cp  mysql03:/etc/mysql/my.cnf /root/my.cnf

2.修改/root/my.cnf文件
[root@localhost ~]# vi my.cnf

3.将修改后的配置copy到容器
docker cp /root/my.cnf mysql03:/etc/mysql/

4.进入mysql容器
[root@localhost ~]# docker exec -it f01aff89e3b6 /bin/bash

5.查看修改后的配置文件
root@f01aff89e3b6:/etc/mysql# cat my.cnf
# Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql

secure-file-priv= NULL

#默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
character_set_server=utf8
init_connect='SET NAMES utf8'
max_allowed_packet = 20M

[mysql]
default-character-set = utf8

[mysql.server]
default-character-set = utf8

[mysqld_safe]
default-character-set = utf8

[client]
default-character-set = utf8

# Custom config should go here
!includedir /etc/mysql/conf.d/

6.重启mysql容器
[root@localhost ~]# docker restart f01aff89e3b6

另外,补充一下自己springboot项目中application.yml文件

server:
  port: 8081
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.124.10:3306/jdbc?useSSL=false&allowPublicKeyRetrieval=true
#    url: jdbc:mysql://localhost:3306/jdbc?useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
#   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

你可能感兴趣的:(mysql)