0

I have a Spring/Hibernate app that was running with h2-database. Now, I need to make it connect to a Mysql database running on a Docker container.

The Mysql container is derived directly from Dockerhub page.

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=test -d mysql:latest

In my application.properties I have:

spring.jpa.hibernate.ddl-auto=create
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=my-secret-pw

Right after it starts I get this exception

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.13.jar:8.0.13] at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.13.jar:8.0.13] at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835) ~[mysql-connector-java-8.0.13.jar:8.0.13] at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455) ~[mysql-connector-java-8.0.13.jar:8.0.13] at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240) ~[mysql-connector-java-8.0.13.jar:8.0.13]

The Mysql container seems to be working fine. I can get in and see it running.

So, where the problem might be?

2 Answers 2

2

You have to publish the port 3306 from your docker instance in order to be able to connect to it from the outside.

docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=test -d mysql:latest

Sign up to request clarification or add additional context in comments.

5 Comments

I wonder why the DockerHub page of Mysql doesn't tell you that you need to expose the ports.
Security reasons, you don't want to publish your ports to the public when you are in production. For local development it's fine.
I suppose in production I need to use config files, isn't it?
I assume that by config files you mean dockerfiles. There is nothing wrong using the original docker file from the provider. Assuming that you application is dockerized as well, in production you may use docker networks where the communication is handle between running containers isolated from the outside.
Also, it is nothing wrong with exposing ports to the outside world, just be aware what are you exposing.
0

JDBC Url needs a change, you need to replace the localhost with mysql container name some-mysql

spring.datasource.url=jdbc:mysql://some-mysql:3306/test?autoReconnect=true&useSSL=false

Expose your MYSQL port in the command, and run with below.

docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=test -e MYSQL_USER=user -e MYSQL_PASSWORD=password -d mysql:latest

3 Comments

It doesn't work. I get this error ": HikariPool-1 - Exception during pool initialization."
I need to change the "some-mysql" to localhost, then it works. I wonder what will I need to do when porting my app to cloud or kubernetes.
Could you please edit your question and post few more details about how you are using the properties in your code ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.