koding mysql,Docker MySQL-无法从Spring Boot应用连接到MySQL数据库

What I'm trying to do is, connect from my spring-boot app to mysql database in Docker. Each in their own container.

But I must be having something wrong because I can't do it.

To keep it simple :

application-properties :

# URL for the mysql db

spring.datasource.url=jdbc:mysql://workaround-mysql:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640

# User name in mysql

spring.datasource.username=springuser

# Password for mysql

spring.datasource.password=admin

#Port at which application runs

server.port=8080

docker-compose for MySQL:

version: '3'

services:

workaround-mysql:

container_name: workaround-mysql

image: mysql

environment:

MYSQL_DATABASE: workaround

MYSQL_USER: springuser

MYSQL_PASSWORD: admin

MYSQL_ROOT_PASSWORD: admin

MYSQL_ROOT_HOST: '%'

ports:

- "3308:3306"

restart: always

So pretty simple right ? Database I start with docker-compose up:

NKfvA.png

All seems to be working fine so far.

Now that I have db started, to the application, this is its docker-compose.yml:

version: '3'

services:

workaround:

restart: always

# will build ./docker/workaround/Dockerfile

build: ./docker/workaround

working_dir: /workaround

volumes:

- ./:/workaround

- ~/.m2:/root/.m2

expose:

- "8080"

command: "mvn clean spring-boot:run"

For its Dockerfile I use Linux Alpine and Java.

FROM alpine:3.9

....add java...

RUN apk update

RUN apk add dos2unix --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/community/ --allow-untrusted

RUN apk add bash

RUN apk add maven

Super simple. Now let's start the application :

VFGe0.png

WEpR6.png

w6o93.png

Unknown host, so let's try the IP then :

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' workaround-mysql

# URL for the mysql db

spring.datasource.url=jdbc:mysql://172.20.0.2:3308/workaround?serverTimezone=UTC&max_allowed_packet=15728640

Now I get timeout:

AOtVs.png

As you can see I get error. What is wrong with my setup and how to fix

this? Either I have unknown host exception or Refused to connect or connection timeout.

I have tried:

Using ip of a container in my application.properties, didn't work

Different ports for MySQL and application

Different images and versions of MySQL

Having everything in one docker compose with wait

timer for database.

Minimal setup with

https://github.com/hellokoding/hellokoding-courses/tree/master/docker-examples/dockercompose-springboot-mysql-nginx

Also resulted in communication link failure, Site was accessible but I

doubt that db was connected properly.

Notes:

I run this all on one computer I use port 3308 because I have local

MySQL db at 3306.

Here is docker ps -a

z7YJK.png

@Vusal ANSWER output :

EIUFN.png

Only thing different from code in answer I did wait for database to be ready 30 seconds

command: /bin/bash -c "sleep 30;mvn clean spring-boot:run;"

解决方案

Try this docker-compose.yml:

version: '3'

services:

workaround-mysql:

container_name: workaround-mysql

image: mysql

environment:

MYSQL_DATABASE: workaround

MYSQL_USER: springuser

MYSQL_PASSWORD: admin

MYSQL_ROOT_PASSWORD: admin

MYSQL_ROOT_HOST: '%'

ports:

- "3308:3306"

restart: always

workaround:

depends_on:

- workaround-mysql

restart: always

# will build ./docker/workaround/Dockerfile

build: ./docker/workaround

working_dir: /workaround

volumes:

- ./:/workaround

- ~/.m2:/root/.m2

expose:

- "8080"

command: "mvn clean spring-boot:run"

And update your application.properties to use the next JDBC connection url:

spring.datasource.url=jdbc:mysql://workaround-mysql:3306/workaround?serverTimezone=UTC&max_allowed_packet=15728640

It should work when both containers in the same docker-compose file, because docker-compose creates default network for containers, so they can resolve each other by name.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值