Initial commit
This commit is contained in:
396
skills/aws-rds-spring-boot-integration/SKILL.md
Normal file
396
skills/aws-rds-spring-boot-integration/SKILL.md
Normal file
@@ -0,0 +1,396 @@
|
||||
---
|
||||
name: aws-rds-spring-boot-integration
|
||||
description: Configure AWS RDS (Aurora, MySQL, PostgreSQL) with Spring Boot applications. Use when setting up datasources, connection pooling, security, and production-ready database configuration.
|
||||
category: aws
|
||||
tags: [aws, rds, aurora, spring-boot, spring-data-jpa, datasource, configuration, hikari, mysql, postgresql]
|
||||
version: 1.1.0
|
||||
allowed-tools: Read, Write, Bash, Glob
|
||||
---
|
||||
|
||||
# AWS RDS Spring Boot Integration
|
||||
|
||||
Configure AWS RDS databases (Aurora, MySQL, PostgreSQL) with Spring Boot applications for production-ready connectivity.
|
||||
|
||||
## When to Use This Skill
|
||||
|
||||
Use this skill when:
|
||||
- Setting up AWS RDS Aurora with Spring Data JPA
|
||||
- Configuring datasource properties for Aurora, MySQL, or PostgreSQL endpoints
|
||||
- Implementing HikariCP connection pooling for RDS
|
||||
- Setting up environment-specific configurations (dev/prod)
|
||||
- Configuring SSL connections to AWS RDS
|
||||
- Troubleshooting RDS connection issues
|
||||
- Setting up database migrations with Flyway
|
||||
- Integrating with AWS Secrets Manager for credential management
|
||||
- Optimizing connection pool settings for RDS workloads
|
||||
- Implementing read/write split with Aurora
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before starting AWS RDS Spring Boot integration:
|
||||
1. AWS account with RDS access
|
||||
2. Spring Boot project (3.x)
|
||||
3. RDS instance created and running (Aurora/MySQL/PostgreSQL)
|
||||
4. Security group configured for database access
|
||||
5. Database endpoint information available
|
||||
6. Database credentials secured (environment variables or Secrets Manager)
|
||||
|
||||
## Quick Start
|
||||
|
||||
### Step 1: Add Dependencies
|
||||
|
||||
**Maven (pom.xml):**
|
||||
```xml
|
||||
<dependencies>
|
||||
<!-- Spring Data JPA -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Aurora MySQL Driver -->
|
||||
<dependency>
|
||||
<groupId>com.mysql</groupId>
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>8.2.0</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Aurora PostgreSQL Driver (alternative) -->
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Flyway for database migrations -->
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Validation -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
```
|
||||
|
||||
**Gradle (build.gradle):**
|
||||
```gradle
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
|
||||
// Aurora MySQL
|
||||
runtimeOnly 'com.mysql:mysql-connector-j:8.2.0'
|
||||
|
||||
// Aurora PostgreSQL (alternative)
|
||||
runtimeOnly 'org.postgresql:postgresql'
|
||||
|
||||
// Flyway
|
||||
implementation 'org.flywaydb:flyway-core'
|
||||
}
|
||||
```
|
||||
|
||||
### Step 2: Basic Datasource Configuration
|
||||
|
||||
**application.properties (Aurora MySQL):**
|
||||
```properties
|
||||
# Aurora MySQL Datasource - Cluster Endpoint
|
||||
spring.datasource.url=jdbc:mysql://myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops
|
||||
spring.datasource.username=admin
|
||||
spring.datasource.password=${DB_PASSWORD}
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
# JPA/Hibernate Configuration
|
||||
spring.jpa.hibernate.ddl-auto=validate
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
|
||||
spring.jpa.properties.hibernate.format_sql=true
|
||||
spring.jpa.open-in-view=false
|
||||
|
||||
# HikariCP Connection Pool
|
||||
spring.datasource.hikari.maximum-pool-size=20
|
||||
spring.datasource.hikari.minimum-idle=5
|
||||
spring.datasource.hikari.connection-timeout=20000
|
||||
spring.datasource.hikari.idle-timeout=300000
|
||||
spring.datasource.hikari.max-lifetime=1200000
|
||||
|
||||
# Flyway Configuration
|
||||
spring.flyway.enabled=true
|
||||
spring.flyway.baseline-on-migrate=true
|
||||
spring.flyway.locations=classpath:db/migration
|
||||
```
|
||||
|
||||
**application.properties (Aurora PostgreSQL):**
|
||||
```properties
|
||||
# Aurora PostgreSQL Datasource
|
||||
spring.datasource.url=jdbc:postgresql://myapp-aurora-pg-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:5432/devops
|
||||
spring.datasource.username=admin
|
||||
spring.datasource.password=${DB_PASSWORD}
|
||||
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||
|
||||
# JPA/Hibernate Configuration
|
||||
spring.jpa.hibernate.ddl-auto=validate
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
|
||||
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
|
||||
spring.jpa.open-in-view=false
|
||||
```
|
||||
|
||||
### Step 3: Set Up Environment Variables
|
||||
|
||||
```bash
|
||||
# Production environment variables
|
||||
export DB_PASSWORD=YourStrongPassword123!
|
||||
export SPRING_PROFILES_ACTIVE=prod
|
||||
|
||||
# For development
|
||||
export SPRING_PROFILES_ACTIVE=dev
|
||||
```
|
||||
|
||||
## Configuration Examples
|
||||
|
||||
### Simple Aurora Cluster (MySQL)
|
||||
|
||||
**application.yml:**
|
||||
```yaml
|
||||
spring:
|
||||
application:
|
||||
name: DevOps
|
||||
|
||||
datasource:
|
||||
url: jdbc:mysql://myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops
|
||||
username: admin
|
||||
password: ${DB_PASSWORD}
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
hikari:
|
||||
pool-name: AuroraHikariPool
|
||||
maximum-pool-size: 20
|
||||
minimum-idle: 5
|
||||
connection-timeout: 20000
|
||||
idle-timeout: 300000
|
||||
max-lifetime: 1200000
|
||||
leak-detection-threshold: 60000
|
||||
connection-test-query: SELECT 1
|
||||
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: validate
|
||||
show-sql: false
|
||||
open-in-view: false
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.MySQL8Dialect
|
||||
format_sql: true
|
||||
jdbc:
|
||||
batch_size: 20
|
||||
order_inserts: true
|
||||
order_updates: true
|
||||
|
||||
flyway:
|
||||
enabled: true
|
||||
baseline-on-migrate: true
|
||||
locations: classpath:db/migration
|
||||
validate-on-migrate: true
|
||||
|
||||
logging:
|
||||
level:
|
||||
org.hibernate.SQL: WARN
|
||||
com.zaxxer.hikari: INFO
|
||||
```
|
||||
|
||||
### Read/Write Split Configuration
|
||||
|
||||
For read-heavy workloads, use separate writer and reader datasources:
|
||||
|
||||
**application.properties:**
|
||||
```properties
|
||||
# Aurora MySQL - Writer Endpoint
|
||||
spring.datasource.writer.jdbc-url=jdbc:mysql://myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops
|
||||
spring.datasource.writer.username=admin
|
||||
spring.datasource.writer.password=${DB_PASSWORD}
|
||||
spring.datasource.writer.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
# Aurora MySQL - Reader Endpoint (Read Replicas)
|
||||
spring.datasource.reader.jdbc-url=jdbc:mysql://myapp-aurora-cluster.cluster-ro-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops
|
||||
spring.datasource.reader.username=admin
|
||||
spring.datasource.reader.password=${DB_PASSWORD}
|
||||
spring.datasource.reader.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
|
||||
# HikariCP for Writer
|
||||
spring.datasource.writer.hikari.maximum-pool-size=15
|
||||
spring.datasource.writer.hikari.minimum-idle=5
|
||||
|
||||
# HikariCP for Reader
|
||||
spring.datasource.reader.hikari.maximum-pool-size=25
|
||||
spring.datasource.reader.hikari.minimum-idle=10
|
||||
```
|
||||
|
||||
### SSL Configuration
|
||||
|
||||
**Aurora MySQL with SSL:**
|
||||
```properties
|
||||
spring.datasource.url=jdbc:mysql://myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops?useSSL=true&requireSSL=true&verifyServerCertificate=true
|
||||
```
|
||||
|
||||
**Aurora PostgreSQL with SSL:**
|
||||
```properties
|
||||
spring.datasource.url=jdbc:postgresql://myapp-aurora-pg-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:5432/devops?ssl=true&sslmode=require
|
||||
```
|
||||
|
||||
## Environment-Specific Configuration
|
||||
|
||||
### Development Profile
|
||||
|
||||
**application-dev.properties:**
|
||||
```properties
|
||||
# Local MySQL for development
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/devops_dev
|
||||
spring.datasource.username=root
|
||||
spring.datasource.password=root
|
||||
|
||||
# Enable DDL auto-update in development
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
# Smaller connection pool for local dev
|
||||
spring.datasource.hikari.maximum-pool-size=5
|
||||
spring.datasource.hikari.minimum-idle=2
|
||||
```
|
||||
|
||||
### Production Profile
|
||||
|
||||
**application-prod.properties:**
|
||||
```properties
|
||||
# Aurora Cluster Endpoint (Production)
|
||||
spring.datasource.url=jdbc:mysql://${AURORA_ENDPOINT}:3306/${DB_NAME}
|
||||
spring.datasource.username=${DB_USERNAME}
|
||||
spring.datasource.password=${DB_PASSWORD}
|
||||
|
||||
# Validate schema only in production
|
||||
spring.jpa.hibernate.ddl-auto=validate
|
||||
spring.jpa.show-sql=false
|
||||
spring.jpa.open-in-view=false
|
||||
|
||||
# Production-optimized connection pool
|
||||
spring.datasource.hikari.maximum-pool-size=30
|
||||
spring.datasource.hikari.minimum-idle=10
|
||||
spring.datasource.hikari.connection-timeout=20000
|
||||
spring.datasource.hikari.idle-timeout=300000
|
||||
spring.datasource.hikari.max-lifetime=1200000
|
||||
|
||||
# Enable Flyway migrations
|
||||
spring.flyway.enabled=true
|
||||
spring.flyway.validate-on-migrate=true
|
||||
```
|
||||
|
||||
## Database Migration Setup
|
||||
|
||||
Create migration files for Flyway:
|
||||
|
||||
```
|
||||
src/main/resources/db/migration/
|
||||
├── V1__create_users_table.sql
|
||||
├── V2__add_phone_column.sql
|
||||
└── V3__create_orders_table.sql
|
||||
```
|
||||
|
||||
**V1__create_users_table.sql:**
|
||||
```sql
|
||||
CREATE TABLE users (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
email VARCHAR(255) NOT NULL UNIQUE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_email (email)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
```
|
||||
|
||||
## Advanced Features
|
||||
|
||||
For advanced configuration, see the reference documents:
|
||||
|
||||
- [Multi-datasource, SSL, Secrets Manager integration](references/advanced-configuration.md)
|
||||
- [Common issues and solutions](references/troubleshooting.md)
|
||||
|
||||
## Best Practices
|
||||
|
||||
### Connection Pool Optimization
|
||||
|
||||
- Use HikariCP with Aurora-optimized settings
|
||||
- Set appropriate pool sizes based on Aurora instance capacity
|
||||
- Configure connection timeouts for failover handling
|
||||
- Enable leak detection
|
||||
|
||||
### Security Best Practices
|
||||
|
||||
- Never hardcode credentials in configuration files
|
||||
- Use environment variables or AWS Secrets Manager
|
||||
- Enable SSL/TLS connections
|
||||
- Configure proper security group rules
|
||||
- Use IAM Database Authentication when possible
|
||||
|
||||
### Performance Optimization
|
||||
|
||||
- Enable batch operations for bulk data operations
|
||||
- Disable open-in-view pattern to prevent lazy loading issues
|
||||
- Use appropriate indexing for Aurora queries
|
||||
- Configure connection pooling for high availability
|
||||
|
||||
### Monitoring
|
||||
|
||||
- Enable Spring Boot Actuator for database metrics
|
||||
- Monitor connection pool metrics
|
||||
- Set up proper logging for debugging
|
||||
- Configure health checks for database connectivity
|
||||
|
||||
## Testing
|
||||
|
||||
Create a health check endpoint to test database connectivity:
|
||||
|
||||
```java
|
||||
@RestController
|
||||
@RequestMapping("/api/health")
|
||||
public class DatabaseHealthController {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@GetMapping("/db-connection")
|
||||
public ResponseEntity<Map<String, Object>> testDatabaseConnection() {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
response.put("status", "success");
|
||||
response.put("database", connection.getCatalog());
|
||||
response.put("url", connection.getMetaData().getURL());
|
||||
response.put("connected", true);
|
||||
return ResponseEntity.ok(response);
|
||||
} catch (Exception e) {
|
||||
response.put("status", "failed");
|
||||
response.put("error", e.getMessage());
|
||||
response.put("connected", false);
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Test with cURL:**
|
||||
```bash
|
||||
curl http://localhost:8080/api/health/db-connection
|
||||
```
|
||||
|
||||
## Support
|
||||
|
||||
For detailed troubleshooting and advanced configuration, refer to:
|
||||
|
||||
- [AWS RDS Aurora Advanced Configuration](references/advanced-configuration.md)
|
||||
- [AWS RDS Aurora Troubleshooting Guide](references/troubleshooting.md)
|
||||
- [AWS RDS Aurora documentation](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/java_aurora_code_examples.html)
|
||||
- [Spring Boot Data RDS Aurora documentation](https://www.baeldung.com/aws-aurora-rds-java)
|
||||
@@ -0,0 +1,279 @@
|
||||
# AWS RDS Aurora Advanced Configuration
|
||||
|
||||
## Read/Write Split Configuration
|
||||
|
||||
For applications with heavy read operations, configure separate datasources:
|
||||
|
||||
**Multi-Datasource Configuration Class:**
|
||||
```java
|
||||
@Configuration
|
||||
public class AuroraDataSourceConfig {
|
||||
|
||||
@Primary
|
||||
@Bean(name = "writerDataSource")
|
||||
@ConfigurationProperties("spring.datasource.writer")
|
||||
public DataSource writerDataSource() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Bean(name = "readerDataSource")
|
||||
@ConfigurationProperties("spring.datasource.reader")
|
||||
public DataSource readerDataSource() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "writerEntityManagerFactory")
|
||||
public LocalContainerEntityManagerFactoryBean writerEntityManagerFactory(
|
||||
EntityManagerFactoryBuilder builder,
|
||||
@Qualifier("writerDataSource") DataSource dataSource) {
|
||||
return builder
|
||||
.dataSource(dataSource)
|
||||
.packages("com.example.domain")
|
||||
.persistenceUnit("writer")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean(name = "readerEntityManagerFactory")
|
||||
public LocalContainerEntityManagerFactoryBean readerEntityManagerFactory(
|
||||
EntityManagerFactoryBuilder builder,
|
||||
@Qualifier("readerDataSource") DataSource dataSource) {
|
||||
return builder
|
||||
.dataSource(dataSource)
|
||||
.packages("com.example.domain")
|
||||
.persistenceUnit("reader")
|
||||
.build();
|
||||
}
|
||||
|
||||
@Primary
|
||||
@Bean(name = "writerTransactionManager")
|
||||
public PlatformTransactionManager writerTransactionManager(
|
||||
@Qualifier("writerEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
|
||||
return new JpaTransactionManager(entityManagerFactory);
|
||||
}
|
||||
|
||||
@Bean(name = "readerTransactionManager")
|
||||
public PlatformTransactionManager readerTransactionManager(
|
||||
@Qualifier("readerEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
|
||||
return new JpaTransactionManager(entityManagerFactory);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Usage in Repository:**
|
||||
```java
|
||||
@Repository
|
||||
public interface UserReadRepository extends JpaRepository<User, Long> {
|
||||
// Read operations automatically use reader endpoint
|
||||
}
|
||||
|
||||
@Repository
|
||||
public interface UserWriteRepository extends JpaRepository<User, Long> {
|
||||
// Write operations use writer endpoint
|
||||
}
|
||||
```
|
||||
|
||||
## SSL/TLS Configuration
|
||||
|
||||
Enable SSL for secure connections to Aurora:
|
||||
|
||||
**Aurora MySQL with SSL:**
|
||||
```properties
|
||||
spring.datasource.url=jdbc:mysql://myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:3306/devops?useSSL=true&requireSSL=true&verifyServerCertificate=true
|
||||
```
|
||||
|
||||
**Aurora PostgreSQL with SSL:**
|
||||
```properties
|
||||
spring.datasource.url=jdbc:postgresql://myapp-aurora-pg-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com:5432/devops?ssl=true&sslmode=require
|
||||
```
|
||||
|
||||
**Download RDS Certificate:**
|
||||
```bash
|
||||
# Download RDS CA certificate
|
||||
wget https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem
|
||||
|
||||
# Configure in application
|
||||
spring.datasource.url=jdbc:mysql://...?useSSL=true&trustCertificateKeyStoreUrl=file:///path/to/global-bundle.pem
|
||||
```
|
||||
|
||||
## AWS Secrets Manager Integration
|
||||
|
||||
**Add AWS SDK Dependency:**
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>secretsmanager</artifactId>
|
||||
<version>2.20.0</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
**Secrets Manager Configuration:**
|
||||
```java
|
||||
@Configuration
|
||||
public class AuroraDataSourceConfig {
|
||||
|
||||
@Value("${aws.secretsmanager.secret-name}")
|
||||
private String secretName;
|
||||
|
||||
@Value("${aws.region}")
|
||||
private String region;
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
Map<String, String> credentials = getAuroraCredentials();
|
||||
|
||||
HikariConfig config = new HikariConfig();
|
||||
config.setJdbcUrl(credentials.get("url"));
|
||||
config.setUsername(credentials.get("username"));
|
||||
config.setPassword(credentials.get("password"));
|
||||
config.setMaximumPoolSize(20);
|
||||
config.setMinimumIdle(5);
|
||||
config.setConnectionTimeout(20000);
|
||||
|
||||
return new HikariDataSource(config);
|
||||
}
|
||||
|
||||
private Map<String, String> getAuroraCredentials() {
|
||||
SecretsManagerClient client = SecretsManagerClient.builder()
|
||||
.region(Region.of(region))
|
||||
.build();
|
||||
|
||||
GetSecretValueRequest request = GetSecretValueRequest.builder()
|
||||
.secretId(secretName)
|
||||
.build();
|
||||
|
||||
GetSecretValueResponse response = client.getSecretValue(request);
|
||||
String secretString = response.secretString();
|
||||
|
||||
// Parse JSON secret
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
try {
|
||||
return mapper.readValue(secretString, Map.class);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to parse secret", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**application.properties (Secrets Manager):**
|
||||
```properties
|
||||
aws.secretsmanager.secret-name=prod/aurora/credentials
|
||||
aws.region=us-east-1
|
||||
```
|
||||
|
||||
## Database Migration with Flyway
|
||||
|
||||
### Setup Flyway
|
||||
|
||||
**Create Migration Directory:**
|
||||
```
|
||||
src/main/resources/db/migration/
|
||||
├── V1__create_users_table.sql
|
||||
├── V2__add_phone_column.sql
|
||||
└── V3__create_orders_table.sql
|
||||
```
|
||||
|
||||
**V1__create_users_table.sql:**
|
||||
```sql
|
||||
CREATE TABLE users (
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
email VARCHAR(255) NOT NULL UNIQUE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_email (email)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
```
|
||||
|
||||
**V2__add_phone_column.sql:**
|
||||
```sql
|
||||
ALTER TABLE users ADD COLUMN phone VARCHAR(20);
|
||||
```
|
||||
|
||||
**Flyway Configuration:**
|
||||
```properties
|
||||
spring.jpa.hibernate.ddl-auto=validate
|
||||
spring.flyway.enabled=true
|
||||
spring.flyway.baseline-on-migrate=true
|
||||
spring.flyway.locations=classpath:db/migration
|
||||
spring.flyway.validate-on-migrate=true
|
||||
```
|
||||
|
||||
## Connection Pool Optimization for Aurora
|
||||
|
||||
**Recommended HikariCP Settings:**
|
||||
```properties
|
||||
# Aurora-optimized connection pool
|
||||
spring.datasource.hikari.maximum-pool-size=20
|
||||
spring.datasource.hikari.minimum-idle=5
|
||||
spring.datasource.hikari.connection-timeout=20000
|
||||
spring.datasource.hikari.idle-timeout=300000
|
||||
spring.datasource.hikari.max-lifetime=1200000
|
||||
spring.datasource.hikari.leak-detection-threshold=60000
|
||||
spring.datasource.hikari.connection-test-query=SELECT 1
|
||||
```
|
||||
|
||||
**Formula for Pool Size:**
|
||||
```
|
||||
connections = ((core_count * 2) + effective_spindle_count)
|
||||
For Aurora: Use 20-30 connections per application instance
|
||||
```
|
||||
|
||||
## Failover Handling
|
||||
|
||||
Aurora automatically handles failover between instances. Configure connection retry:
|
||||
|
||||
```properties
|
||||
# Connection retry configuration
|
||||
spring.datasource.hikari.connection-timeout=30000
|
||||
spring.datasource.url=jdbc:mysql://cluster-endpoint:3306/db?failOverReadOnly=false&maxReconnects=3&connectTimeout=30000
|
||||
```
|
||||
|
||||
## Read Replica Load Balancing
|
||||
|
||||
Use reader endpoint for distributing read traffic across replicas:
|
||||
|
||||
```properties
|
||||
# Reader endpoint for read-heavy workloads
|
||||
spring.datasource.reader.url=jdbc:mysql://cluster-ro-endpoint:3306/db
|
||||
```
|
||||
|
||||
## Performance Optimization
|
||||
|
||||
**Enable batch operations:**
|
||||
```properties
|
||||
spring.jpa.properties.hibernate.jdbc.batch_size=20
|
||||
spring.jpa.properties.hibernate.order_inserts=true
|
||||
spring.jpa.properties.hibernate.order_updates=true
|
||||
spring.jpa.properties.hibernate.batch_versioned_data=true
|
||||
```
|
||||
|
||||
**Disable open-in-view pattern:**
|
||||
```properties
|
||||
spring.jpa.open-in-view=false
|
||||
```
|
||||
|
||||
**Production logging configuration:**
|
||||
```properties
|
||||
# Disable SQL logging in production
|
||||
logging.level.org.hibernate.SQL=WARN
|
||||
logging.level.org.springframework.jdbc=WARN
|
||||
|
||||
# Enable HikariCP metrics
|
||||
logging.level.com.zaxxer.hikari=INFO
|
||||
logging.level.com.zaxxer.hikari.pool=DEBUG
|
||||
```
|
||||
|
||||
**Enable Spring Boot Actuator for metrics:**
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
```properties
|
||||
management.endpoints.web.exposure.include=health,metrics,info
|
||||
management.endpoint.health.show-details=always
|
||||
```
|
||||
@@ -0,0 +1,180 @@
|
||||
# AWS RDS Aurora Troubleshooting Guide
|
||||
|
||||
## Common Issues and Solutions
|
||||
|
||||
### Connection Timeout to Aurora Cluster
|
||||
**Error:** `Communications link failure` or `Connection timed out`
|
||||
|
||||
**Solutions:**
|
||||
- Verify security group inbound rules allow traffic on port 3306 (MySQL) or 5432 (PostgreSQL)
|
||||
- Check Aurora cluster endpoint is correct (cluster vs instance endpoint)
|
||||
- Ensure your IP/CIDR is whitelisted in security group
|
||||
- Verify VPC and subnet configuration
|
||||
- Check if Aurora cluster is in the same VPC or VPC peering is configured
|
||||
|
||||
```bash
|
||||
# Test connection from EC2/local machine
|
||||
telnet myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com 3306
|
||||
```
|
||||
|
||||
### Access Denied for User
|
||||
**Error:** `Access denied for user 'admin'@'...'`
|
||||
|
||||
**Solutions:**
|
||||
- Verify master username and password are correct
|
||||
- Check if IAM authentication is required but not configured
|
||||
- Reset master password in Aurora console if needed
|
||||
- Verify user permissions in database
|
||||
|
||||
```sql
|
||||
-- Check user permissions
|
||||
SHOW GRANTS FOR 'admin'@'%';
|
||||
```
|
||||
|
||||
### Database Not Found
|
||||
**Error:** `Unknown database 'devops'`
|
||||
|
||||
**Solutions:**
|
||||
- Verify initial database name was created with cluster
|
||||
- Create database manually using MySQL/PostgreSQL client
|
||||
- Check database name in JDBC URL matches existing database
|
||||
|
||||
```sql
|
||||
-- Connect to Aurora and create database
|
||||
CREATE DATABASE devops CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
```
|
||||
|
||||
### SSL Connection Issues
|
||||
**Error:** `SSL connection error` or `Certificate validation failed`
|
||||
|
||||
**Solutions:**
|
||||
```properties
|
||||
# Option 1: Disable SSL verification (NOT recommended for production)
|
||||
spring.datasource.url=jdbc:mysql://...?useSSL=false
|
||||
|
||||
# Option 2: Properly configure SSL with RDS certificate
|
||||
spring.datasource.url=jdbc:mysql://...?useSSL=true&requireSSL=true&verifyServerCertificate=true&trustCertificateKeyStoreUrl=file:///path/to/global-bundle.pem
|
||||
|
||||
# Option 3: Trust all certificates (NOT recommended for production)
|
||||
spring.datasource.url=jdbc:mysql://...?useSSL=true&requireSSL=true&verifyServerCertificate=false
|
||||
```
|
||||
|
||||
### Too Many Connections
|
||||
**Error:** `Too many connections` or `Connection pool exhausted`
|
||||
|
||||
**Solutions:**
|
||||
- Review Aurora instance max_connections parameter
|
||||
- Optimize HikariCP pool size
|
||||
- Check for connection leaks in application code
|
||||
|
||||
```properties
|
||||
# Reduce pool size
|
||||
spring.datasource.hikari.maximum-pool-size=15
|
||||
spring.datasource.hikari.minimum-idle=5
|
||||
|
||||
# Enable leak detection
|
||||
spring.datasource.hikari.leak-detection-threshold=60000
|
||||
```
|
||||
|
||||
**Check Aurora max_connections:**
|
||||
```sql
|
||||
SHOW VARIABLES LIKE 'max_connections';
|
||||
-- Default for Aurora: depends on instance class
|
||||
-- db.r6g.large: ~1000 connections
|
||||
```
|
||||
|
||||
### Slow Query Performance
|
||||
**Error:** Queries taking longer than expected
|
||||
|
||||
**Solutions:**
|
||||
- Enable slow query log in Aurora parameter group
|
||||
- Review connection pool settings
|
||||
- Check Aurora instance metrics in CloudWatch
|
||||
- Optimize queries and add indexes
|
||||
|
||||
```properties
|
||||
# Enable query logging (development only)
|
||||
logging.level.org.hibernate.SQL=DEBUG
|
||||
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
|
||||
```
|
||||
|
||||
### Failover Delays
|
||||
**Error:** Application freezes during Aurora failover
|
||||
|
||||
**Solutions:**
|
||||
- Configure connection timeout appropriately
|
||||
- Use cluster endpoint (not instance endpoint)
|
||||
- Implement connection retry logic
|
||||
|
||||
```properties
|
||||
spring.datasource.hikari.connection-timeout=20000
|
||||
spring.datasource.hikari.validation-timeout=5000
|
||||
spring.datasource.url=jdbc:mysql://...?failOverReadOnly=false&maxReconnects=3
|
||||
```
|
||||
|
||||
## Testing Aurora Connection
|
||||
|
||||
### Connection Test with Spring Boot Application
|
||||
|
||||
**Create a Simple Test Endpoint:**
|
||||
```java
|
||||
@RestController
|
||||
@RequestMapping("/api/health")
|
||||
public class DatabaseHealthController {
|
||||
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
|
||||
@GetMapping("/db-connection")
|
||||
public ResponseEntity<Map<String, Object>> testDatabaseConnection() {
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
response.put("status", "success");
|
||||
response.put("database", connection.getCatalog());
|
||||
response.put("url", connection.getMetaData().getURL());
|
||||
response.put("connected", true);
|
||||
return ResponseEntity.ok(response);
|
||||
} catch (Exception e) {
|
||||
response.put("status", "failed");
|
||||
response.put("error", e.getMessage());
|
||||
response.put("connected", false);
|
||||
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Test with cURL:**
|
||||
```bash
|
||||
curl http://localhost:8080/api/health/db-connection
|
||||
```
|
||||
|
||||
### Verify Aurora Connection with MySQL/PostgreSQL Client
|
||||
|
||||
**MySQL Client Connection:**
|
||||
```bash
|
||||
# Connect to Aurora MySQL cluster
|
||||
mysql -h myapp-aurora-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com \
|
||||
-P 3306 \
|
||||
-u admin \
|
||||
-p devops
|
||||
|
||||
# Verify connection
|
||||
SHOW DATABASES;
|
||||
SELECT @@version;
|
||||
SHOW VARIABLES LIKE 'aurora_version';
|
||||
```
|
||||
|
||||
**PostgreSQL Client Connection:**
|
||||
```bash
|
||||
# Connect to Aurora PostgreSQL
|
||||
psql -h myapp-aurora-pg-cluster.cluster-abc123xyz.us-east-1.rds.amazonaws.com \
|
||||
-p 5432 \
|
||||
-U admin \
|
||||
-d devops
|
||||
|
||||
# Verify connection
|
||||
\l
|
||||
SELECT version();
|
||||
```
|
||||
Reference in New Issue
Block a user