DEV Community

Cover image for Run MySQL on Docker and use in your Java App

Run MySQL on Docker and use in your Java App

Sandro Giacomozzi on June 12, 2019

Running MySQL in Docker is very useful to use in development. In this tutorial, we will see how easy it is to start MySQL in Docker and use it in o...
Collapse
 
franzisk profile image
Francisco Vieira Souza

Hey friend,

Very nice your tutorial.

On the first command (to install mysql on the container), what happens if I have already a mysql local running on 3306 port?

I also would like to know how to create a container with mysql + java + MyApplication.jar, how can I do that?

Collapse
 
sandrogiacom profile image
Sandro Giacomozzi

Thanks for your feedback. See my other posts about this subject. About port, you received an error. Just change port of left side.

dev.to/sandrogiacom/kubernetes-for...

Collapse
 
gauravkohirkar profile image
GauravKohirkar • Edited

Great Tutorial !!
I guess I was stuck on the container access issue.
CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on . to 'demo_java'@'%' identified by '1234';
FLUSH PRIVILEGES;
This solved it. Thanks!!
Can you please also do an Enterprize level Spring Boot Running in docker and orchestrated on Kubernetes tutorial? That'd be great.. :)

Collapse
 
sandrogiacom profile image
Sandro Giacomozzi

Thanks for your feedback. I wrote one serie about this.
dev.to/sandrogiacom/kubernetes-for...

Collapse
 
laurebaugnies profile image
laurebaugnies

Hi ! Thanks for your post, it has been really helpfull ! I have yet a question. How can we save the state of the container in order to reuse it later on a different computer ? How to prevent data loss ?

Collapse
 
robson96 profile image
Robson96

Pode explicar pfv esse trecho de codigo:
De 'demo_java' pro final do codigo nao entende!
CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on . to 'demo_java'@'%' identified by '1234';
FLUSH PRIVILEGES;

O que faz essas tres linhas?

Collapse
 
sandrogiacom profile image
Sandro Giacomozzi

Ola. Ele cria um usuário no banco e da permissão necessárias para criar as tabelas

Collapse
 
sinha123 profile image
sinha123

Hi friend,
I tried all the three option of jdbc url, but did not work. Request your help.

spring.datasource.url=jdbc:mysql://localhost:3306/hello_java

spring.datasource.url=jdbc:mysql://:3306/hello_java

spring.datasource.url=jdbc:mysql://:3306/hello_java

Just to tell you My Machine is Windows 10 pro.
Spring boot codebase running on my local eclipse.

And I have Docker Desktop installed in my System where I have created Mysql container.

Following error comes when I Run as Spring Boot App:

2020-05-23 20:28:02.279 INFO 76 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-05-23 20:28:24.354 ERROR 76 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.

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.16.jar:8.0.16]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.16.jar:8.0.16]

Collapse
 
balajich profile image
Balaji Sankar Chopparapu

I ran to the same situation. The root cause is MySQL Server and Clients are running in two different versions. Thats the reason you are seeing handshake connection issue because of mysql protocol version mismatch.

Run you server with most recent version that should solve the problem.

docker run --name mysql57 -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=1234 \
-e MYSQL_USER=demo_java \
-e MYSQL_PASSWORD=1234 \
-e MYSQL_DATABASE=hello_java \
-d mysql/mysql-server

or Specify the compatible JDBC driver version in pom.xml