How can i configure the Spring Boot Starter Application to use mysql instead of h2?

In order to have MySQL instead of H2, you need to do the following:
In your pom.xml, you need to add the following dependencies:

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

What are these? The first one is Spring Data JPA, which makes handling data access easier. For more details, you may look here: Spring Data JPA
The second one is the actual connector used to communicate with the database.

Now, having these is only half of what you have to do. If you want to actually use them, as was said before, you need to configure them, which from the code you provided seems to be fine as you have it (in the application.yaml file). As a side note to this, make sure the database actually exists on your MySQL server, because otherwise your application may fail on startup.

Another note that may be of use to you: I would recommend not deleting the H2 dependency from the pom.xml file, because that my still be used during testing and your application may fail without it.

2 Likes