Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.UnknownHostException dockerized mysql from spring boot application

I have a spring boot application and dockerized mysql db. My docker container is up and result of docker ps command below.

cf7936857c6f        mysql:5.6           "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes        3306/tcp            mysql-standalone

application properties file configuration here;

spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test

spring.datasource.username = sa

spring.datasource.password = password

I have run mysql docker container like this

docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:5.6

And when I try to start my spring boot application on IDE I have faced

java.net.UnknownHostException: mysql-standalone

Is there any missing configuration in my property file?

like image 343
Ugur Artun Avatar asked Jul 25 '18 21:07

Ugur Artun


2 Answers

Since this is not container to container communication you have to bind the MySQL port to a port in the host:

docker run -p 3306:3306 --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -d mysql:5.6
           ^^^^^^^^^^^^

And point to localhost:

spring.datasource.url = jdbc:mysql://localhost:3306/test
like image 116
Robert Avatar answered Oct 13 '22 07:10

Robert


Run mysql container exposing the port as below.

docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=sa -e MYSQL_PASSWORD=password -p 3306:3306 -d mysql:5.6

And in you spring boot application.properties file, modify as below.

spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false

Don't forget to add useSSL=false, else you might get below error.

Spring Boot: Jdbc javax.net.ssl.SSLException: closing inbound before receiving peer's close_notify
like image 27
sam100rav Avatar answered Oct 13 '22 07:10

sam100rav