关于数据库连接出错问题的解决方法(mysql、postgresql)

时间:2021-11-29 08:25:50

今天在进行spring与postgresql的连接时发现了一个错误,错误信息如下:

Exception in thread "main" java.sql.SQLException: Cannot create PoolableConnectionFactory (FATAL: password authentication failed for user "Rick")
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2152)
 at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1903)
 at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
 at cn.yourside.community.web.test.BeanTest.main(BeanTest.java:16)
Caused by: org.postgresql.util.PSQLException: FATAL: password authentication failed for user "Rick"
 at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:408)
 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:181)
 at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
 at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:144)
 at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:29)
 at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:21)
 at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:31)
 at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:24)
 at org.postgresql.Driver.makeConnection(Driver.java:410)
 at org.postgresql.Driver.connect(Driver.java:280)
 at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
 at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:205)
 at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2162)
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2148)
 ... 3 more

其中Rick是我的PC的用户名,感觉很奇怪,为什么我配置的用户名明明是postgres,在这里却变成了Rick。我修改配置文件件连接mysql数据库,发现报如下错误信息:

Exception in thread "main" java.sql.SQLException: Cannot create PoolableConnectionFactory (Access denied for user'Rick'@'localhost' (using password: YES))
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2152)
 at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1903)
 at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
 at cn.yourside.community.web.test.BeanTest.main(BeanTest.java:16)
Caused by: java.sql.SQLException: Access denied for user 'Rick'@'localhost' (using password: YES)
 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:926)
 at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1748)
 at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
 at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2506)
 at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
 at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
 at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
 at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
 at java.lang.reflect.Constructor.newInstance(Unknown Source)
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
 at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
 at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
 at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:39)
 at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:205)
 at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:2162)
 at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2148)
 ... 3 more

看完之后突然想起来之前好像犯过这个错误,仔细一想,之前是因为在一个类里面存在两个username,觉得是username重复被覆盖造成的,所以我将username换成databaseUserName,替换之后就成功了,这个问题貌似和spring有关系,在它的默认的applicationContext中有username的默认值,与自定义的值造成混淆。