前些日子配置了一个远程服务器系统是Centos7,安装好MySQL后,Navicat可以连上,自己是springboot项目始终连不上,让我非常胸闷,期间找了两个熟人用他们的项目连接,是没问题的。一开始的提示错误是下面这个:
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
那些让我修改mysql密码授权的经验文章,我看了不下20遍(以致于以下命令是完全手动敲出来的),但是我的问题不在于此!
grant all privileges on *.* to username@'host_ip_address' identified by 'password';flush privileges;
TIPS: if host_ip_address equals ‘%’ means all hosts can access in.
当然,在调试过程中还有其他不同错误,提示如下:
A ResourcePool could not acquire a resource from its primary factory or source
Connections could not be acquired from the underlying database!
com.mchange.v2.resourcepool.TimeoutException
针对不同的提示,我搜索了好几天(真的好几天),后来一条条的改,终于定位(后知后觉)到应该是数据源配置有问题,然后逐条对照发现问题所在。
在springboot里配置c3p0连接的正确格式如下:
c3p0.jdbcUrl=jdbc:mysql://ip:port/dnname?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false
c3p0.user=${username}
c3p0.password=${password}
c3p0.driverClass=com.mysql.jdbc.Driver
而我自己的配置是这样的:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/malldb?useUnicode=true&characterEncoding=UTF-8
spring.datasource.data-username=root
spring.datasource.data-password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.generate-unique-name=true
仔细观察,前缀c3p0或者spring.datasource都不是重点,关键在于后面的jdbcUrl和url!!!
spring.datasource.url这种写法是spring-boot集成连接数据库的的写法,而我配置的是c3p0…
至此,真相大白!终于搞清楚问题根源在哪了。。。这个坑真的好大、好深!所以基础配置的时候还是需要认真,仔细(弄懂原理)。
出现这类问题,按照这个思路去逐一排查应该是没问题的,借用前人的经验,附在这里:
1,驱动配置有误:driver=com.mysql.jdbc.Driver
2,数据库连接地址有误:url=jdbc:mysql://localhost:3306/test?3useUnicode=true&;characterEncoding=utf8
3,密码或帐号有误:username=root, password=root
4,数据库未启动或无权访问
5,项目未引入对应的驱动jar包mysql-connector-java-5.1.6-bin.jar
6,mysql root没有远程访问的权限,需要增加权限,增加权限的步骤如下:
进入mysql数据库:
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;
处理这个问题期间,搜索的文章包括(不限于)以下出处,一一谢过
记一次坑爹的编程经历,泪奔…
链接本地数据库正确,别的就报错。A ResourcePool could not acquire a resource from its primary factory or sour 蛋疼的问题
数据库连接错误集锦A ResourcePool could not acquire a resource from its primary factory or source
c3p0配置文件报错
项目经验分享——完美解决Access denied for user ‘root’@‘localhost’ (using password: YES)
MySql ERROR 1044 (42000) 错误解决
使用c3p0数据库连接池时出现com.mchange.v2.resourcepool.TimeoutException
springboot 使用c3p0数据库连接池的方法
解决:Connections could not be acquired from the underlying database!
A ResourcePool could not acquire a resource from its primary factory or source