Overview
The User Management System supports two database configurations:- H2 for development (in-memory)
- MySQL for production (persistent)
Development Database (H2)
The development profile uses an in-memory H2 database that’s perfect for testing and development.Configuration
application-dev.properties
H2 Console
The H2 console is available at
http://localhost:8080/h2-console when running in development mode.| Field | Value |
|---|---|
| JDBC URL | jdbc:h2:mem:usermanagementdb |
| Username | sa |
| Password | (leave empty) |
| Driver Class | org.h2.Driver |
Schema Creation
Withspring.jpa.hibernate.ddl-auto=create-drop, the database schema is:
- Created automatically on application startup
- Dropped when the application shuts down
Production Database (MySQL)
The production profile uses MySQL for persistent data storage.Configuration
application-prod.properties
Connection Settings
- Local MySQL
- Remote MySQL
- With SSL
Schema Management
In production,spring.jpa.hibernate.ddl-auto=update is used:
- Updates the schema automatically based on entity changes
- Preserves existing data
- Adds new columns and tables as needed
For production deployments, consider using database migration tools like Flyway or Liquibase for better control over schema changes.
Data Initialization
Development Data (data.sql)
The development profile automatically loads sample data fromdata.sql:
data.sql
Initialization Configuration
spring.sql.init.mode=always- Executesdata.sqlon startupspring.jpa.defer-datasource-initialization=true- Ensures schema is created before data insertion
MySQL Setup
Create Database
Connection Pool Configuration
For production, configure connection pooling:Environment Variables
Override database settings using environment variables:Troubleshooting
Common Issues
Cannot connect to MySQL
Cannot connect to MySQL
Check:
- MySQL service is running:
sudo systemctl status mysql - Database exists:
SHOW DATABASES; - User has permissions:
SHOW GRANTS FOR 'username'@'localhost'; - Firewall allows port 3306
- Connection URL is correct
H2 console not accessible
H2 console not accessible
Verify:
- Development profile is active:
spring.profiles.active=dev - H2 console is enabled:
spring.console.enabled=true - Path is configured:
spring.console.path=/h2-console - Accessing correct URL:
http://localhost:8080/h2-console
Schema not updating
Schema not updating
Check
ddl-auto setting:- Development:
create-droprecreates schema on restart - Production:
updateonly adds new changes - Use
validateto only validate schema without changes
Best Practices
- Never hardcode passwords - Use environment variables or secrets management
- Use connection pooling - Configure HikariCP for production
- Enable SSL - Use encrypted connections in production
- Regular backups - Implement automated backup strategy for MySQL
- Monitor connections - Track connection pool metrics
- Schema migrations - Use Flyway/Liquibase for production schema management