Skip to content
Spring Boot sb databases 3 min read

MySQL Integration

MySQL is one of the most widely deployed relational databases, and Spring Boot integrates with it cleanly through standard JDBC plus Hibernate. You add the MySQL driver, point your datasource at a running server, and the rest of your Spring Data JPA code stays unchanged.

Adding the dependency

Modern MySQL drivers ship under the artifact mysql-connector-j (the older mysql-connector-java is deprecated). Add it as a runtime dependency alongside the JPA starter.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
</dependency>

Note: When you use spring-boot-starter-parent, the MySQL driver version is managed for you, so you do not declare a <version>. Spring Boot 3.5 pairs with a current 8.x connector.

Datasource configuration

Spring Boot reads spring.datasource.* to build the connection. At minimum you need the JDBC URL, username, and password.

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/shopdb?useSSL=false&serverTimezone=UTC
    username: shop
    password: secret
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: validate
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQLDialect
        format_sql: true
    show-sql: true
  • The URL format is jdbc:mysql://<host>:<port>/<database>?<params>.
  • serverTimezone=UTC avoids timezone parsing errors common with MySQL.
  • driver-class-name is optional, Spring Boot infers com.mysql.cj.jdbc.Driver from the URL, but being explicit is harmless.

Choosing the Hibernate dialect

Hibernate uses a dialect to generate database-specific SQL. For modern MySQL, use MySQLDialect; Hibernate 6 auto-detects the server version, so the generic dialect is preferred over version-suffixed ones.

DatabaseRecommended dialect
MySQL 8.xorg.hibernate.dialect.MySQLDialect
MariaDBorg.hibernate.dialect.MariaDBDialect

In most cases you can omit the dialect entirely and let Hibernate detect it from the connection.

The ddl-auto options

spring.jpa.hibernate.ddl-auto controls how Hibernate reconciles your entities with the schema on startup.

ValueBehaviour
noneDo nothing (production default for managed schemas)
validateVerify the schema matches entities; fail fast on mismatch
updateAdd missing tables/columns; never drops anything
createDrop and recreate the schema on startup
create-dropCreate on startup, drop on shutdown

Warning: Never use update, create, or create-drop against a production MySQL database. Use validate and manage schema changes with Flyway or Liquibase.

Running MySQL in Docker

The quickest way to get a local MySQL server is Docker. This starts MySQL 8 with a database and user matching the config above.

docker run --name mysql-shop \
  -e MYSQL_ROOT_PASSWORD=rootpass \
  -e MYSQL_DATABASE=shopdb \
  -e MYSQL_USER=shop \
  -e MYSQL_PASSWORD=secret \
  -p 3306:3306 \
  -d mysql:8

Verify it is accepting connections:

docker exec -it mysql-shop mysql -ushop -psecret -e "SHOW DATABASES;"

Output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| shopdb             |
+--------------------+

A minimal entity and repository

Nothing in your persistence code is MySQL-specific. The same entity that runs on H2 runs on MySQL.

@Entity
@Table(name = "products")
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    private BigDecimal price;
    // constructors, getters, setters
}
public interface ProductRepository extends JpaRepository<Product, Long> {
    List<Product> findByNameContainingIgnoreCase(String fragment);
}

GenerationType.IDENTITY maps naturally to a MySQL AUTO_INCREMENT column.

Best Practices

  • Externalize credentials with environment variables or a secrets manager; never commit passwords to application.yml.
  • Use ddl-auto: validate in production and drive schema evolution with migrations.
  • Configure the HikariCP connection pool explicitly for production traffic.
  • Let Hibernate auto-detect the dialect unless you have a specific reason to pin it.
  • Use UTF-8 (utf8mb4) collation on your database to store the full Unicode range, including emoji.
Last updated June 13, 2026
Was this helpful?