Martin Čuka
Martin Čuka

Reputation: 18468

Cannot connect Spring Boot application to Docker Mysql container - unknown database

In docker a created network

docker network create mysql-network

Then I create mysql image

docker container run -d -p 3306:3306 --net=mysql-network --name mysql-hibernate -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -v hibernate:/var/lib/mysql mysql

When I run docker ps everything seems OK enter image description here

This is my application.properties

spring.jpa.hibernate.ddl-auto=create
useSSL=false
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect

I also tried

spring.datasource.url=jdbc:mysql://mysql-hibernate:3306/test

But I will always get an error on startup

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'test'

How's that possible that it doesn't know database 'test' ? I specified name in docker like this
-e MYSQL_DATABASE=test
What am I missing ?

Upvotes: 2

Views: 2059

Answers (1)

Artur
Artur

Reputation: 128

I know it is bit late but I'll answer anyway so people coming here can benefit ;) Your configuration overall seems alright. When you get error like this you can add flag param set to true in your application.properties in line where you set datasource url. So you will come up with something like this:

spring.datasource.url=jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true

Hope this helps!

Upvotes: 6

Related Questions