Spring Boot + AWS RDS MySQL: Connect Your App to Cloud DB
Connecting a Spring Boot application to AWS RDS MySQL is almost identical to using a local MySQL instance. Only the datasource URL, username, and password change—your Spring Boot, JPA, Hibernate, and repository code remain the same.
Step 1: Local MySQL Setup with Spring Boot
Create a Spring Boot project from Spring Initializr with:
- Spring Web
- Spring Data JPA
- MySQL Driver
Below is an example Product entity:
package com.example.demo.entity;
import jakarta.persistence.*;
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private double price;
// getters and setters
}
Product JPA Repository:
package com.example.demo.repository;
import com.example.demo.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ProductRepository extends JpaRepository {
}
Product REST Controller:
package com.example.demo.controller;
import com.example.demo.entity.Product;
import com.example.demo.repository.ProductRepository;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/products")
public class ProductController {
private final ProductRepository repo;
public ProductController(ProductRepository repo) {
this.repo = repo;
}
@PostMapping
public Product addProduct(@RequestBody Product product) {
return repo.save(product);
}
@GetMapping
public List<Product> getProducts() {
return repo.findAll();
}
}
Your local application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Once MySQL is running locally, Spring Boot creates the tables and your APIs begin storing and retrieving data.
Step 2: Create MySQL Database on AWS RDS
In the AWS Console → RDS → Create Database:
- Engine: MySQL
- Template: Free Tier
- Username: admin
- Password: admin1234 (example)
- Public access: Enabled
- Security group: Allow port 3306
After RDS becomes Available, copy the RDS endpoint.
Connect via terminal:
mysql -h mydb.xxxxx.ap-south-1.rds.amazonaws.com -u admin -p
Create the database:
CREATE DATABASE inventory;
Step 3: Connect Spring Boot to AWS RDS MySQL
Update your application.properties to use the RDS endpoint and new database:
spring.datasource.url=jdbc:mysql://mydb.xxxxx.ap-south-1.rds.amazonaws.com:3306/inventory
spring.datasource.username=admin
spring.datasource.password=admin1234
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
After restarting the application, Hibernate creates the product table in AWS RDS automatically. Your APIs now store and fetch data from the cloud database instead of local MySQL.
Final Thoughts
Spring Boot treats AWS RDS MySQL exactly like a regular MySQL server. As long as your network security group allows access and your JDBC URL is correct, the application works without any other changes.
This makes RDS a reliable choice when moving from local development to cloud deployment on EC2, Elastic Beanstalk, Docker, or Kubernetes.
Comments
Post a Comment