我是靠谱客的博主 爱笑煎蛋,最近开发中收集的这篇文章主要介绍Docker springboot连接mysql connect refuse项目场景:问题描述原因分析:解决方案:,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
docker-compose配置文章
项目场景:
docker-compose运行jar、mysql容器。在同一网桥下,配置springboot通过域名访问mysql数据库。
问题描述
日志报错:springboot cannot connect to mysql connection refused
原因分析:
springboot无法正常访问数据库,非同一网络、端口未开放、防火墙、Jar重新连接数据库配置未重构等。
解决方案:
1.是否配置useSSL(高版本的mysql需要指明)
2.查看是否是同一网络下,并且存在容器(证明是否在同一网络)
#查看网络
[root@localhost docker-compose-demo]# docker network ls
NETWORK ID
NAME
DRIVER
SCOPE
5bb70c9c8677
bridge
bridge
local
9035eb472472
docker-compose-demo_docker-compose-demo-net
bridge
local
3221931118c7
host
host
local
478c3b2b1271
none
null
local
[root@localhost docker-compose-demo]# docker network inspect 9035eb472472
[
{
"Name": "docker-compose-demo_docker-compose-demo-net",
"Id": "9035eb47247219c3aa76dd476431e1738cdc4f61397745e2a99e1b9848645012",
"Created": "2022-12-08T06:27:40.512043218-08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": null,
"Config": [
{
"Subnet": "192.168.62.56/24",
"Gateway": "192.168.62.1"
}
]
},
"Internal": false,
"Attachable": true,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"7f20a56083fe518e8e6aafb4d5a8385b699151c381705c8e4811ccf53eea67a1": {
"Name": "mysql",
"EndpointID": "055f135452b248ac2eabc955ce407e0ee1b9b8569b09f182a6dcded0298c6f6e",
"MacAddress": "02:42:c0:a8:3e:03",
"IPv4Address": "192.168.62.3/24",
"IPv6Address": ""
},
"b34ea8492a6f65798369f8aa2430cf9c350b337147eeed2ad38164c606e2d3c0": {
"Name": "docker-compose-demo02",
"EndpointID": "cdafff677802ac63a5d9cc9eadbce844c32d501fc2092ec068977c6059a55d0e",
"MacAddress": "02:42:c0:a8:3e:02",
"IPv4Address": "192.168.62.2/24",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {
"com.docker.compose.network": "docker-compose-demo-net",
"com.docker.compose.project": "docker-compose-demo",
"com.docker.compose.version": "1.29.2"
}
}
]
3.查看Jar容器是否可以ping通mysql(证明是否可以访问mysql容器)
[root@localhost docker-compose-demo]# docker ps
CONTAINER ID
IMAGE
COMMAND
CREATED
STATUS
PORTS
NAMES
b34ea8492a6f
docker-compose-demo
"java -jar docker-co…"
4 days ago
Up 11 minutes
8082/tcp, 0.0.0.0:8082->8080/tcp, :::8082->8080/tcp
docker-compose-demo02
7f20a56083fe
mysql:8.0.30
"docker-entrypoint.s…"
4 days ago
Up 11 minutes
0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp
mysql
[root@localhost docker-compose-demo]# docker exec -it b34ea8492a6f bash
root@b34ea8492a6f:/# ping mysql
PING mysql (192.168.62.3): 56 data bytes
64 bytes from 192.168.62.3: icmp_seq=0 ttl=64 time=0.110 ms
64 bytes from 192.168.62.3: icmp_seq=1 ttl=64 time=0.120 ms
64 bytes from 192.168.62.3: icmp_seq=2 ttl=64 time=0.091 ms
^C--- mysql ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/stddev = 0.091/0.107/0.120/0.000 ms
root@b34ea8492a6f:/# ^C
root@b34ea8492a6f:/#
4.通过Navicat(证明数据库可以被docker-compose内设置的root用户访问)
5.DockerFile生成镜像是最新的(证明jar生成的容器是最新的)
本人就是因为这个原因弄了很久。之前的mysql配置不存在useSSL=false,添加后,重新运行docker-compose up。不过DockerFile存在缓存,不会重新构建镜像,导致容器不是最新的。
解决方法:
最后
以上就是爱笑煎蛋为你收集整理的Docker springboot连接mysql connect refuse项目场景:问题描述原因分析:解决方案:的全部内容,希望文章能够帮你解决Docker springboot连接mysql connect refuse项目场景:问题描述原因分析:解决方案:所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复