使用 JDBC / c3p0 连接到 MariaDB

Posted

技术标签:

【中文标题】使用 JDBC / c3p0 连接到 MariaDB【英文标题】:Connecting to MariaDB using JDBC / c3p0 【发布时间】:2016-08-02 20:05:43 【问题描述】:

我们正处于从 mysql 切换到 MariaDb 的过程中。我希望它会像更新 JDBC URL 和 JDBC 驱动程序一样简单,但不幸的是这还不够。

MariaDb 版本:5.5.47-MariaDB

配置:

db.driver=org.mariadb.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true
db.user=user
db.password=secret

司机:

<dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>1.4.2</version>
</dependency>

c3po 定义,这里没有任何变化,和 MySQL 一样:

<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <!-- access -->
    <property name="driverClass" value="$db.driver" />
    <property name="jdbcUrl" value="$db.url" />
    <property name="user" value="$db.user" />
    <property name="password" value="$db.password" />
    ....

错误:

    Last acquisition attempt exception: 
java.sql.SQLNonTransientConnectionException: Client does not support authentication protocol requested by server. Consider upgrading MariaDB client. plugin was = 
        at org.mariadb.jdbc.internal.util.ExceptionMapper.get(ExceptionMapper.java:123)
        at org.mariadb.jdbc.internal.util.ExceptionMapper.throwException(ExceptionMapper.java:69)
        at org.mariadb.jdbc.Driver.connect(Driver.java:110)
        at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
        at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
        at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
        at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
        at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
        at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
        at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Caused by: org.mariadb.jdbc.internal.util.dao.QueryException: Client does not support authentication protocol requested by server. Consider upgrading MariaDB client. plugin was = 
        at org.mariadb.jdbc.internal.protocol.authentication.DefaultAuthenticationProvider.processAuthPlugin(DefaultAuthenticationProvider.java:94)
        at org.mariadb.jdbc.internal.protocol.authentication.AuthenticationProviderHolder$1.processAuthPlugin(AuthenticationProviderHolder.java:69)
        at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.authentication(AbstractConnectProtocol.java:490)
        at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.handleConnectionPhases(AbstractConnectProtocol.java:448)
        at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connect(AbstractConnectProtocol.java:359)
        at org.mariadb.jdbc.internal.protocol.AbstractConnectProtocol.connectWithoutProxy(AbstractConnectProtocol.java:703)
        at org.mariadb.jdbc.internal.util.Utils.retrieveProxy(Utils.java:471)
        at org.mariadb.jdbc.Driver.connect(Driver.java:105)
        ... 8 more

我已经尝试了一些东西,但到目前为止没有任何帮助:

试过db.url=jdbc:mariadb://localhost...

当我尝试使用新密码格式时:update mysql.user set Password = PASSWORD('secret') where user = 'user';

我收到以下异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
        at sun.reflect.GeneratedConstructorAccessor47.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
        at com.mysql.jdbc.Util.getInstance(Util.java:383)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1023)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
        at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2407)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2328)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
        at sun.reflect.GeneratedConstructorAccessor17.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
        at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
        at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
        at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
        at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
        at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
        at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
        at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
        at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Caused by: java.sql.SQLException: Unable to load authentication plugin ''.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)
        at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1769)
        at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2508)
        at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2346)
        ... 17 more

任何帮助将不胜感激。

【问题讨论】:

您是否尝试在“正常”或“安全”模式下重置密码?例如喜欢这里:tecadmin.net/steps-to-reset-mariadb-root-password-in-linux 【参考方案1】:

经过一些额外的测试,我发现更新到新密码不起作用:

MariaDB [test]> SELECT PASSWORD('123456'), OLD_PASSWORD('123456');
+--------------------+------------------------+
| PASSWORD('123456') | OLD_PASSWORD('123456') |
+--------------------+------------------------+
| 565491d704013245   | 565491d704013245       |
+--------------------+------------------------+

原来我们在/etc/my.cnf 中设置了old_passwords 选项。关闭它并重新启动 MariaDb 后,新的 PASSWORD() 开始正常工作,我们现在可以连接了。希望它对调试困难的人有用。

> SHOW VARIABLES LIKE 'old_passwords';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| old_passwords | 0     |
+---------------+-------+

> SELECT PASSWORD('123456'), OLD_PASSWORD('123456');
+-------------------------------------------+------------------------+
| PASSWORD('123456')                        | OLD_PASSWORD('123456') |
+-------------------------------------------+------------------------+
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 565491d704013245       |
+-------------------------------------------+------------------------+

【讨论】:

以上是关于使用 JDBC / c3p0 连接到 MariaDB的主要内容,如果未能解决你的问题,请参考以下文章

如何在 org.springframework.jdbc.CannotGetJdbcConnectionException 上记录 JDBC 连接信息

无法使用 Postgresql 将 JDBC 连接到 sonarqube

无法使用 JDBC 连接到 Phoenix

如何使用 jdbc 连接到 oracle

msSQL jdbc .. 我连接到服务器但如何连接到使用特定的 databaseName

使用 JDBC 连接到 MySQL 数据库