Configuring Spring Boot for MySQL

Configuring Spring Boot for MySQL


Out of the box, Spring Boot is very easy to use with the H2 Database. If the H2 database is found on your classpath, Spring Boot will automatically set up an in-memory H2 database for your use. But what if you want to use MySQL? Naturally, Spring Boot has support for MySQL and a number of other popular relational databases.

Previously, I wrote about creating a web application using Spring Boot.  Let’s say we want to deploy this application to production and we’ve decided to use MySQL for the database. Changing Spring Boot from H2 to MySQL is easy to do.

MySQL Configuration

For this example, I’m using MySQL installed locally on my MacBook Pro. You’ll need to have a database defined for your use. For this example, I want to create a database for my use. Using the command prompt, you can log into MySQL with this command:

mysql -u root

Use the following command to create a database.

CREATE DATABASE springbootdb;

You only need to use these commands if you want to use a new database. MySQL is a very robust database. The full capabilities of MySQL are beyond the scope of this tutorial.

MySQL Dependencies

First, we need to add the MySQL database drivers to our project. You will need to add the following dependency to your Maven POM file.



Spring Boot Properties

We need to override the H2 database properties being set by default in Spring Boot. The nice part is, Spring Boot sets default database properties only when you don’t. So, when we configure MySQL for use. Spring Boot won’t setup the H2 database anymore.

The following properties need to configure MySQL with Spring Boot. You can see these are pretty standard Java data source properties. Since in my example project, I’m using JPA too, we need to configure Hibernate for MySQL too.

spring.datasource.url= jdbc:mysql://localhost:3306/springbootdb


NOTE: If this was actually a production database, you do not tell Hibernate to use the create-drop option. This tells Hibernate to recreate the database on startup. Definitely not the behavior we want. You can set this property to the following values: none, validate, update, create-drop. If this was actually a production database, you probably would want to use validate.

Running Spring Boot with MySQL

This is all that needs to be changed to use MySQL with Spring Boot. When you start the project now, MySQL will be used by the Spring Boot application for the database.

Free Introduction to Spring Tutorial

Are you new to the Spring Framework? Checkout my Free Introduction to Spring Online Tutorial.

Get The Source

The source code for this post is available on GitHub here. You can download the source and build the project using Maven.

About jt

    You May Also Like

    18 comments on “Configuring Spring Boot for MySQL

    1. August 27, 2015 at 1:37 am

      For your information, you don’t need to specify the driver or the Hibernate dialect. These are detected automatically.

    2. August 27, 2015 at 7:35 am

      Thanks for pointing that out! I’ve updated the post.

    3. February 1, 2016 at 12:30 am

      Great working example. A big help for me as I am developing an app using these capabilities. Works out of the box unlike alot
      of the old spring genuine samples in the new version ide. Thanks Mike [email protected], DevoutProgrammers
      Port St. Lucie Florida

      • February 1, 2016 at 6:12 am

        Thanks – glad it helped!

    4. March 16, 2016 at 6:53 am

      This saved my day.Thanks

    5. April 15, 2016 at 4:50 am

      Great Example. Thanks for posting it. I have a query, why do we need h2database dependency here?

      • April 15, 2016 at 7:56 am

        You don’t need h2, I should have deleted that for this example.

    6. July 24, 2016 at 4:44 am

      Hi jt
      how to make it work for datasource looking up for jndi rather than db url
      do we need to create a datasource bean for db jodi look up

      Great examples


    7. April 11, 2017 at 9:08 am

      Unfortunately I am getting this error, and can’t get rid of it
      “Access denied for user ”@’localhost’ (using password: NO)”

      • April 11, 2017 at 9:33 am

        Looks like a security issue in MySQL with the account you are using.

    8. July 27, 2017 at 6:35 am

      Hi, I am having issues with running the springboot, it previously worked for H2, but now seems to show the following error:

      Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jpaMappingContext’: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: At least one JPA metamodel must be present!

      Any help would be appreciated!

      • July 27, 2017 at 10:31 am

        Looks like the Spring context is not finding any JPA entities.

    9. July 24, 2018 at 6:02 am

      how can we integrate spring boot with H2 and flyway?

      • July 24, 2018 at 6:04 am

        Continuation of previous question, how can we connect to H2 with tcp, spring boot and flyway?


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.