springboot 配置MySQL数据库产生一系列问题经验汇总(吐血整理)

1 篇文章 0 订阅
1 篇文章 0 订阅

1.发现问题

前些日子配置了一个远程服务器系统是Centos7,安装好MySQL后,Navicat可以连上,自己是springboot项目始终连不上,让我非常胸闷,期间找了两个熟人用他们的项目连接,是没问题的。一开始的提示错误是下面这个:

ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

2.常规方式

那些让我修改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

3.定位问题

针对不同的提示,我搜索了好几天(真的好几天),后来一条条的改,终于定位(后知后觉)到应该是数据源配置有问题,然后逐条对照发现问题所在。

在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都不是重点,关键在于后面的jdbcUrlurl!!!
spring.datasource.url这种写法是spring-boot集成连接数据库的的写法,而我配置的是c3p0…

至此,真相大白!终于搞清楚问题根源在哪了。。。这个坑真的好大、好深!所以基础配置的时候还是需要认真,仔细(弄懂原理)。

4.总结思路

出现这类问题,按照这个思路去逐一排查应该是没问题的,借用前人的经验,附在这里:

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;

5.搜索记录

处理这个问题期间,搜索的文章包括(不限于)以下出处,一一谢过

记一次坑爹的编程经历,泪奔…

链接本地数据库正确,别的就报错。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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值