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 our Java application with Spring Boot.
Prepare
First, you need a docker runtime on you machine. If you don’t have it, please install following this docker documentation: https://docs.docker.com/install/
Start Mysql container
To start the container, type the command below in your terminal:
docker run --name mysql57 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=1234 -d mysql/mysql-server:5.7
Enter fullscreen mode Exit fullscreen mode
The command above, pulled image if not found in local repository and run mysql container in detached mode. Configure local port equals to container port and set root password to 1234
.
Check your container running:
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
380eca8a553c mysql/mysql-server:5.7 "/entrypoint.sh mysq…" 8 minutes ago Up 8 minutes (healthy) 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
Enter fullscreen mode Exit fullscreen mode
As we can see, mysql5.7 is running on localhost:3306
At this point, you have a local mysql server without install it.
Note: You will not have external access to the container with root user.
Create a database
Type:
docker exec -it mysql57 bash
Enter fullscreen mode Exit fullscreen mode
This command access the mysql container and allow to execute commands on database. Inside a container, type:
mysql -h localhost -u root -p
Enter fullscreen mode Exit fullscreen mode
Remember, the password is 1234
. First, we need to create a user for out-of-container access because root access is not allowed:
CREATE USER 'demo_java' IDENTIFIED BY 'java';
grant all on *.* to 'demo_java'@'%' identified by '1234';
FLUSH PRIVILEGES;
Enter fullscreen mode Exit fullscreen mode
To create a database, paste DDL bellow:
CREATE DATABASE hello_java CHARACTER SET utf8 COLLATE utf8_general_ci;
Enter fullscreen mode Exit fullscreen mode
Next, type:
show databases;
+--------------------+
| Database |
+--------------------+
| hello_java |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
Enter fullscreen mode Exit fullscreen mode
Create a database automatically
Another way to start a database and automatic create a hello_java
database and demo_java
user:
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:5.7
Enter fullscreen mode Exit fullscreen mode
With the above command, you do not have to enter the database and create a database and a user. Cool, is not it?
Create Demo App
Go to https://start.spring.io/ and create a demo app with the Web and Jpa dependencies.
Import application in your IDE and follow steps below:
1 – Edit application.properties
file:
spring.datasource.url=jdbc:mysql://localhost:3306/hello_java
spring.datasource.username=demo_java
spring.datasource.password=1234
spring.jpa.hibernate.ddl-auto=update
Enter fullscreen mode Exit fullscreen mode
spring.jpa.hibernate.ddl-auto=update creates the person
table if it doesn’t exist.
Note: It’s not recommended to use in production.
2 – Edit pom.xml
file and add mysql connector java
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode
3 – Add domain, service and persistence classes:
Domain
4 – Change main class to create two new Person:
Run Demo App
Start demo app just running a main class:
Application is running and data is persistent in database
Bonus
To expose your data as a Rest endpoint, just add this dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
Enter fullscreen mode Exit fullscreen mode
Access http://localhost:8080/persons
And Wow! Your data exposed as json.
Source code
sandrogiacom / database-demo
database-demo
Source code for the post: Run MySQL on Docker and use in your Java App
https://dev.to/sandrogiacom/run-mysql-on-docker-and-use-in-your-java-app-jpn
暂无评论内容