Search code examples
javamysqlspringspring-boot

Difficulty connecting Mysql to Spring Boot


I am new to Spring Boot and have been attempting to connect it to my MySql workbench. I started a new Spring Boot project with web, jdbc, jpa, and hibernate, but have been stuck.

There are a number of tutorials on this subject, but they all start by indicating that it's necessary to go into the application.properties file and add a configuration similar to the below:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/mysqltutorial?useSSL=false
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

However, none of the tutorials I have found discusses how to find the spring.datasource.url or even the username or password.

How can I find update these application properties so my Mysql workbench will connect to my Spring Boot application?


Solution

  • In your case, spring.datasource.url is equal to jdbc:mysql://localhost:3306/mysqltutorial?useSSL=false

    Breaking it up:

    jdbc:mysql://localhost:3306 is a reference to MySQL that is running locally on your machine. By default, MySQL runs on port 3306, though it can be changed if you so desire. Additionally, the address can be changed from localhost if you want to connect to a database that is running elsewhere.

    mysqltutorial is the name of your database created in MySQL.

    ?useSSL=false is an additional property of connecting to the database.

    Next,
    spring.datasource.username and spring.datasource.password are the password and username you use for the database in MySQL. For a small project, you can use root, though that is not recommended for more serious projects and another user should be created with varying permissions.