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:

Use the following command to create a database.

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.

POM.xml

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 wont setup the H2 database anymore.

The following properties are 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.

NOTE: If this was actually a production database, you 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.

Share

You May Also Like

14 comments on “Configuring Spring Boot for MySQL

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

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

  3. 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

    • Thanks – glad it helped!

  4. This saved my day.Thanks

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

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

  6. 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

    Regards
    Prem

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

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

  8. 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!

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

Leave a Reply