MySQL Integration

Integrate MySQL with Spring Boot — DataSource configuration, HikariCP connection pooling, Flyway migrations, and production tuning for enterprise MySQL deployments.

Last reviewed: January 2025

·Spring Boot 2.7Spring Boot 3.xMySQL 8.x
✅
Quick Understanding: Add the MySQL driver and configure spring.datasource.url/username/password in application.properties. Spring Boot auto-configures HikariCP connection pool automatically. Use Flyway for database migrations. Never use spring.jpa.hibernate.ddl-auto=create or update in production — always use migrations.

🔍 What Is It?

Spring Boot integrates with MySQL through:

  • →MySQL Connector/J — the JDBC driver
  • →HikariCP — auto-configured connection pool (fastest Java connection pool)
  • →Spring Data JPA / Hibernate — ORM layer
  • →Flyway / Liquibase — database schema migrations

Auto-configuration:

When mysql-connector-j is on the classpath and spring.datasource.url is set, Spring Boot auto-configures DataSource, JdbcTemplate, EntityManagerFactory, and TransactionManager.

🎯 Why Do We Need It?

MySQL is the most widely used relational database in enterprise Java applications. Proper integration means: connection pooling (not creating a new connection per request), schema migration management (not manual SQL scripts), and production-ready configuration (timeouts, pool sizes, SSL).

🏗️ What Happens in Real Projects?

Standard MySQL Spring Boot setup:

  • →HikariCP pool with 10-20 connections for typical microservices
  • →Flyway managing schema versions from V1 to V47
  • →Read replicas with separate DataSource for read-heavy workloads
  • →SSL connections in production

⚙️ How It Works

Spring Boot's DataSourceAutoConfiguration reads spring.datasource.* properties and creates a HikariDataSource. The pool maintains a set of open connections, lending them to threads on request and returning them to the pool when done. This is far more efficient than creating a new connection per request (which takes 20-50ms each).

💻 Example

MySQL Integration — Configuration, Flyway migrations, and production settings.

xml
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>  <!-- version managed by Spring Boot -->
</dependency>

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
</dependency>

<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-mysql</artifactId>
</dependency>

🤔 Common Confusions

❓ What is ddl-auto and which value should I use?

ddl-auto controls whether Hibernate creates/modifies the schema. Values: create (drops and recreates on startup), create-drop (creates on start, drops on stop), update (adds missing columns, never drops), validate (validates schema matches entities, throws if not), none (does nothing). Use validate in production with Flyway/Liquibase for migrations. Never use create or update in production.

❓ How many connections should the pool have?

For a typical microservice, 10-20 connections is right. The HikariCP formula: connections = (core_count * 2) + effective_spindle_count. For a 4-core machine with SSD: ~10 connections. More connections don't mean more throughput — they add contention. Monitor pool wait time in production to tune.

🚨 Common Production Issues

Connection pool exhaustion — all connections in use, new requests wait or timeout; increase pool size or optimize slow queries

MySQL server gone away — connections idle too long; set max-lifetime below MySQL wait_timeout

UTC timezone issues — always set serverTimezone=UTC in JDBC URL

Character encoding issues with emoji/special chars — use utf8mb4, not utf8

SSL not configured in production — always use useSSL=true for production databases

✅ Best Practices

Always use Flyway or Liquibase for schema migrations

Set ddl-auto=validate in production

Configure HikariCP pool size based on your workload and DB server capacity

Use UTC timezone in both application and database

Enable SSL for production database connections

Monitor pool metrics via Actuator and alert on pool wait time

🏛️ Architect's Note

Senior Architect's Perspective

For read-heavy microservices, configure two DataSources: a primary (read-write) and a replica (read-only). Route read operations to the replica using @Transactional(readOnly=true) with AbstractRoutingDataSource. This doubles your read throughput with minimal code changes. Spring Boot doesn't do this automatically — it requires custom DataSource configuration.

💬 Frequently Asked Questions

Flyway vs Liquibase — which should I use?

Flyway is simpler: SQL migration files named V1__description.sql. Liquibase is more powerful: XML/YAML/JSON changelogs with rollback support. For most teams, Flyway's simplicity wins. Use Liquibase when you need rollback scripts, database-independent migrations, or complex branching strategies.

🎯 Key Takeaways

1Spring Boot auto-configures HikariCP from spring.datasource.* properties
2Use Flyway for schema migrations — never ddl-auto=create/update in production
3ddl-auto=validate checks schema matches entities at startup
4Set max-lifetime below MySQL wait_timeout to prevent stale connections
5Always use utf8mb4 charset and UTC timezone
6Monitor connection pool metrics in production

🎯 Practice this topic in interviews

↗ Where this topic fits

Sources

Sources / References

Use these Spring Boot references to verify the concept in official docs and continue into the most relevant SplashRide pages.

Related SplashRide pages

SplashRide Spring Boot learning path

Browse the full Spring Boot topic collection and learning order on SplashRide.

Spring Boot Interview Prep

Practice Spring Boot interview questions and scenario-based answers.

Backend Engineer Career Path

A structured journey through Core Java, Spring Boot, production backend engineering, and interview preparation.

Full Stack Java Developer Career Path

A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.

PostgreSQL Integration

Integrate PostgreSQL with Spring Boot — the preferred database for enterprise Spring Boot applications with advanced features, JSON support, and superior performance.

Transactions

Master Spring transaction management — @Transactional, propagation, isolation levels, rollback rules, and the common pitfalls that cause silent data corruption in production.