Setting up MySQL database on AWS Lightsail for Spring Boot production deployment
Database installation and security
User and database creation
Performance optimization
Backup configuration
Spring Boot integration
Database Options on Lightsail
MySQL on Lightsail Instance - Full control over configuration - No additional charges - Simplified networking - Cost-effective for smaller apps
When to Use RDS - Thousands of concurrent users - High availability requirements - Automated scaling needs - Enterprise-level backup requirements
Installing MySQL
Connect to your Lightsail instance and install MySQL:
# Update system packagessudo apt update &&sudo apt upgrade -y# Install MySQL server and clientsudo apt install mysql-server mysql-client -y
Installation takes 2-3 minutes and automatically starts MySQL service
Securing MySQL Installation
Run the security script to remove insecure defaults:
sudo mysql_secure_installation
Configure these settings: {.incremental}
VALIDATE PASSWORD COMPONENT: Y
Password validation policy: 2 (STRONG)
Set strong root password
Remove anonymous users: Y
Disallow root remote login: Y
Remove test database: Y
Creating Application Database
Access MySQL and create dedicated resources:
-- Create database with proper charsetCREATEDATABASE myapp_prod CHARACTERSET utf8mb4 COLLATE utf8mb4_unicode_ci;-- Create application userCREATEUSER'springapp'@'localhost'IDENTIFIEDBY'your-secure-app-password';-- Grant privilegesGRANTALLPRIVILEGESON myapp_prod.*TO'springapp'@'localhost';FLUSHPRIVILEGES;
Testing Database Setup
Verify your configuration works:
-- Switch to new databaseUSE myapp_prod;-- Check current user and databaseSELECTUSER(), DATABASE();-- Test table operationsCREATETABLE test_connection (idINT AUTO_INCREMENT PRIMARYKEY, message VARCHAR(255), created_at TIMESTAMPDEFAULTCURRENT_TIMESTAMP);
# Production MySQL configurationspring.datasource.url=jdbc:mysql://localhost:3306/myapp_prodspring.datasource.username=springappspring.datasource.password=your-secure-app-passwordspring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver# Production JPA settingsspring.jpa.hibernate.ddl-auto=validatespring.jpa.show-sql=false
Security Best Practices
{.incremental}
Use strong, unique passwords
Create dedicated application users
Limit user privileges to required databases
Keep database local to instance
Regular security updates
Implement backup verification
Summary
✅ Completed Tasks:
Installed and secured MySQL server
Created production database and user
Configured performance optimizations
Set up automated backup system
Prepared Spring Boot configuration
Next: Deploy Spring Boot application and connect to MySQL database