mybatis connection error Cannot create PoolableConnectionFactory (Access denied for user 'root '@'local

时间:2023-03-08 16:59:45
mybatis connection error  Cannot create PoolableConnectionFactory (Access denied for user 'root  '@'local

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:
Cannot create PoolableConnectionFactory (Access denied for user 'root '@'localhost' (using password: YES))
### The error may exist in com/sample/mappers/EmployeeMapper.xml
### The error may involve com.sample.dao.EmployeeDao.listall
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException:
Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:
Cannot create PoolableConnectionFactory (Access denied for user 'root '@'localhost' (using password: YES))

notice: the user is 'root ' not 'root'

这个问题大致就是需要检查你的数据源配置即Resource中的:url,username,password

url这个一般是"jdbc:mysql://localhost:3306/test",一定要注意3306,这个是你数据库连接用的端口,可以把它改成别的,但是如果你用的tomcat容器,别把它写成8080,这样就会产生Cannot create PoolableConnectionFactory (Communications link failure问题,还有就是url=jdbc:mysql://localhost:3306/test中的localhost,也不是随便配置的,要看你的数据库中的from host

我当时实在MySql Workbench5.2 CE中的 Users and Privileges中,有个Server Access Management

User  From Host 你看From Host中有没有localhost,如果有你就能写成url=jdbc:mysql://localhost:3306/test,要是from host有127.0.0.1的话,也可以写成url=jdbc:mysql://127.0.0.1:3306/test

还有就是 username=  这个一定要写正确,我当时写的是name= ,结果mysql找不到username,然后他自动把username设为"",以至于错误提示: java.sql.SQLException: Access denied for user ‘’ @'localhost' (using password: YES) 你看到@前面是两个单引号了吗,那就是我没输入username而导致默认的默认username,其实这个错误也可以用设置权限改,但是那是mysql内容,就不说了。

总结起来就是:url端口要写对,不能写其他程序被占用的,127.0.01还是localhost要看数据库里面有哪个,还要就是把对的驱动放入项目中的WEB-INF/lib里,这里提一下不用再放到tomcat的lib中。