Configuring Spring Boot for MySQL
18 CommentsLast Updated on June 2, 2019 by Simanta
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.
POM.xml
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
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 spring.datasource.username=root spring.datasource.password= spring.jpa.hibernate.ddl-auto=create-drop
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
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.
snicoll
For your information, you don’t need to specify the driver or the Hibernate dialect. These are detected automatically.
jt
Thanks for pointing that out! I’ve updated the post.
michael45678
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
jt
Thanks – glad it helped!
thawtarnaing
This saved my day.Thanks
Vish
Great Example. Thanks for posting it. I have a query, why do we need h2database dependency here?
jt
You don’t need h2, I should have deleted that for this example.
premrishi
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
jt
Thanks. Theres a few things to consider about a jndi lookup. See this link – http://stackoverflow.com/questions/24941829/how-to-create-jndi-context-in-spring-boot-with-embedded-tomcat-container
Zoli I
Hello,
Unfortunately I am getting this error, and can’t get rid of it
“Access denied for user ”@’localhost’ (using password: NO)”
jt
Looks like a security issue in MySQL with the account you are using.
Msela
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!
jt
Looks like the Spring context is not finding any JPA entities.
smita
how can we integrate spring boot with H2 and flyway?
smita
Continuation of previous question, how can we connect to H2 with tcp, spring boot and flyway?