1
votes

I have MYSQL container Running i am able to execute it and able to connect from host machine using "mysql -h 172.17.0.2 -u root -p". I have executed "docker run --name=mysql-host -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32" to run the docker image.

but when i trying to access through Spring boot app it`s giving connection refused below are the properties in the application.properties

spring.datasource.url=jdbc:mysql://172.17.0.2:3306/auditLog?createDatabaseIfNotExist=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

i am runnung my jar as given below

java -jar service-1.0.0.jar spring.config.location=file:///home/10662598/Downloads/entity-search-service-deploymentFolder/entity-search-service/configs/application.properties

Error getting Caused by: java.net.ConnectException: Connection refused (Connection refused)

2
Where is your Spring boot app running? On host or inside mysql docker container or inside a separate docker container.Kapil Khandelwal
on my local machine, it is not dockerized. executing the above command to run that.Dewa

2 Answers

0
votes

Your issue is probably because the Spring boot app can't reach the database (mysql), the reason for this is probably because they're not in same network.

If your Spring boot app is running inside container

From your question, I see that you're running containers using docker run..., therefore when you start MySQL and Spring boot app using that command, they're not in same network.

Solution for this is, first create a network, then start MySQL and Spring app to run within that network:

docker network create my-net

Then start your containers as usual but remember to add your containers to the network you just created:

docker run ... --network my-net

Now your containers are in same network and should reach each other, you can verify this by exec to Spring boot app and try curl to Mysql container using its container name (and port):

curl <mysql_container_name>:3306

If Spring boot app is running on host machine

Then simply make sure you map Mysql port to host machine and connect using localhost:<mapped_port>

Good luck :)

-1
votes

a bit late but i think you need to forward port 3306 on local machine to the contain port 3306 try this

docker run --name=mysql-host -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.32