Baskar
Baskar

Reputation: 349

How to connect Remote MySQL DB from SpringBoot Docker service?

Im running a SpringBoot application which requires remote MySQL DB. When run the jar file it is working. But when I tried to containerize my springboot application it is giving SQLNonTransientConnectionException. My Docker setup is in Windows OS machine.

java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-java-8.0.18.jar!/:8.0.18]

My application.properties file

spring.datasource.url=jdbc:mysql://xxx.xx.xxx.xx:3306/bootdb?createDatabaseIfNotExist=true
autoReconnect=true&useSSL=false
spring.datasource.username=remote
spring.datasource.password=remotepsw
spring.datasource.platform=mysql
spring.user.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8090

Most of the online material explains only about connecting Docker Mysql and Docker SpringBoot Application.

How should I proceed?

Upvotes: 1

Views: 2673

Answers (2)

Dumb Fella
Dumb Fella

Reputation: 85

Following command gets the job done.

docker run --net=host <spring-app-imageId>

Be careful with this command because --net=host makes the container share network with the host. The network of the container will no longer be isolated.

Upvotes: 2

sai
sai

Reputation: 139

We can do this below ways

1 Pull your mysql image  
   docker pull mysql
2.Run Your image docker run -d -p 3306:3306 --name remote-e MYSQL_ROOT_PASSWORD=remotepsw mysql.
3.get your docker mysql ip
 docker inspect containerid
4. spring.datasource.url=jdbc:mysql://{useid}:3306
spring.datasource.username=remote
spring.datasource.password=remotepsw
5.Or u can check docker link or docker compose concept

Upvotes: -1

Related Questions