Integrating MySQL into a Spring Boot application can be streamlined by using Docker for local development and Kubernetes for production deployments. By leveraging Spring Boot's profile-specific configuration files (
application-dev.yml
and
application-prod.yml
), you can maintain clean and environment-specific settings. This guide walks you through setting up MySQL in both environments using
application.yml
files.
1. Setting Up Dependencies
Begin by adding the necessary dependencies to your
pom.xml
:
<dependencies>
<!-- Spring Boot Starter Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2. Running MySQL with Docker for Development
For local development, you can run a MySQL container using Docker:
docker run --name mysql-dev -e MYSQL_ROOT_PASSWORD=devpass -e MYSQL_DATABASE=devdb -e MYSQL_USER=devuser -e MYSQL_PASSWORD=devpass -p 3306:3306 -d mysql:8.0
This command sets up a MySQL instance with the specified credentials and exposes it on port 3306.
3. Configuring application.yml
for Profiles
Spring Boot allows you to define profile-specific configuration files. Create the following files in your
src/main/resources
directory:
application.yml
spring:
profiles:
active: dev
This file sets the default active profile to
dev
.
application-dev.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/devdb
username: devuser
password: devpass
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
This configuration connects to the local MySQL instance for development.
application-prod.yml
spring:
datasource:
url: jdbc:mysql://${DB_HOST:prod-mysql-service}:${DB_PORT:3306}/proddb
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: validate
show-sql: false
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL8Dialect
In production, environment variables are used to avoid hardcoding sensitive information.
4. Deploying MySQL on Kubernetes
For production, deploy MySQL using Kubernetes manifests.
mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 1
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: root-password
- name: MYSQL_DATABASE
value: proddb
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: username
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 3306
mysql-service.yaml
apiVersion: v1
kind: Service
metadata:
name: prod-mysql-service
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306
Ensure you create the corresponding
Secret
named
mysql-secret
with the necessary credentials.
5. Deploying Spring Boot Application on Kubernetes
Dockerize your Spring Boot application:
FROM openjdk:17-jdk-slim
VOLUME /tmp
COPY target/myapp.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
Build and push the Docker image:
docker build -t your-dockerhub-username/myapp:latest .
docker push your-dockerhub-username/myapp:latest
Create a Kubernetes deployment for your application:
app-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: your-dockerhub-username/myapp:latest
env:
- name: SPRING_PROFILES_ACTIVE
value: prod
- name: DB_HOST
value: prod-mysql-service
- name: DB_PORT
value: "3306"
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: mysql-secret
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: password
ports:
- containerPort: 8080
Apply the deployments:
kubectl apply -f mysql-deployment.yaml
kubectl apply -f mysql-service.yaml
kubectl apply -f app-deployment.yaml
6. Verifying the Setup
Once deployed, verify that your application is running and connected to the MySQL database. You can check the logs of your application pods:
kubectl logs -f deployment/myapp
Ensure there are no errors related to database connectivity.
By following this guide, you've set up a Spring Boot application with MySQL, using Docker for development and Kubernetes for production, all managed through profile-specific
application.yml
files. This approach promotes clean separation of environments and secure handling of sensitive information.
image quote pre code