本文介绍了配置Spring Boot Docker和Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Boot和Docker的新手.

I am new to Spring Boot and Docker.

我正在尝试创建一个连接到mysql并使用Docker来运行两者的Spring Boot应用程序.

I am trying to create a Spring Boot application connecting to mysql and using Docker to run both.

我遵循的步骤 Step1 -创建了mysql映像并开始运行它.

Steps I followedStep1 - Created mysql image and started running it.

docker run --name=docker-mysql --env="MYSQL_ROOT_PASSWORD=root" --env="MYSQL_PASSWORD=root" --env="MYSQL_DATABASE=test" mysql

Step2 创建了SpringBoot应用程序

Step2 Created a SpringBoot application

docker build -f Dockerfile -t gradle-springboot-docker .

Step3 运行Spring Boot应用程序并与Mysql链接

Step3 Ran the Spring Boot app and linked with Mysql

docker run -t --name gradle-springboot-docker --link docker-mysql:mysql -p 8080:8080 gradle-springboot-docker

它给mysql提供了基本的连接错误.我已经列出了以下application.properties.自从我使用Docker以来,我的连接信息正确吗? mysql的主机是什么?

It gives basic connection error to mysql. I have listed the below application.properties. Is my connection information correct since I am using Docker. What would be the host for mysql?

SSL properties
server.port=8080

#DataSource
datasource.driver = com.mysql.jdbc.Driver
datasource.url= jdbc:mysql://localhost:3306/test?
autoReconnect=true&useSSL=false
datasource.username=root
datasource.password=root

# Hibernate
hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql = true
hibernate.lazy = true
hibernate.max_fetch_depth = 3
hibernate.packagesToScan = com.springboot.poc
# Once DB is created change below property to 'update'
hibernate.hbm2ddl.auto = update

推荐答案

您应该使用mysql的容器名称作为主机名,因为这些链接在一起,因此从春天开始就可以通过其名称发现mysql容器.因此,您需要将datasource.url= jdbc:mysql://localhost:3306/test?更改为datasource.url= jdbc:mysql://docker-mysql:3306/test?

You should use the container name of mysql as the hostname, since these are linked the mysql container is discoverable by its name from spring. So you need to change datasource.url= jdbc:mysql://localhost:3306/test? to datasource.url= jdbc:mysql://docker-mysql:3306/test?

或者您可以使用别名,如@ g00glen00b建议的那样,例如:datasource.url= jdbc:mysql://mysql:3306/test?

Or you can use the alias, as @g00glen00b suggested, like: datasource.url= jdbc:mysql://mysql:3306/test?

这篇关于配置Spring Boot Docker和Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:38