无法通过Docker连接到数据库服务器。

huangapple go评论88阅读模式
英文:

Could not create connection to database server via Docker

问题

我已经创建了一个简单的Spring Boot应用程序,通过使用相同的网络与通过Docker运行的MYSQL进行通信。一旦我运行docker-compose up命令,就会出现以下错误:

> employee-jdbc-container | java.sql.SQLNonTransientConnectionException:
> 无法创建与数据库服务器的连接。尝试重新连接3次后放弃。
>
> employee-jdbc-container | 原因是:
> com.mysql.cj.exceptions.UnableToConnectException: 不允许检索公钥

docker-compose.yml

version: "3"   
services:
  employee-mysql:
             image: employee-jdbc
             container_name: employee-jdbc-container
             ports:
             - "9090:9090"
             networks:
               - employee-mysql2    
             depends_on:
               - mysqldb
  mysqldb:
         image: mysql:8
         container_name: mysqldb
         ports:
         - "3306:3306"
         networks:
           - employee-mysql2
         environment:
           - MYSQL_USER=root
           - MYSQL_ROOT_PASSWORD=root
           - MYSQL_DATABASE=HR
networks:                     
    employee-mysql2: 

application.yml

server:
  port: 9090

spring:
  datasource:
    url: "jdbc:mysql://mysqldb:3306/HR?createDatabaseIfNotExists=true&autoReconnect=true&useSSL=false"
    username: root
    password: root
    platform: mysql
    initialization-mode: always
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect

我认为问题出在配置文件中,但不知道出了什么问题。

英文:

I have created a simple spring boot app to communicate with MYSQL via docker using the same network. Once I run docker-compose up command then following errors have occurred

> employee-jdbc-container | java.sql.SQLNonTransientConnectionException:
> Could not create connection to database server. Attempted reconnect 3
> times. Giving up.
>
> employee-jdbc-container | Caused by:
> com.mysql.cj.exceptions.UnableToConnectException: Public Key Retrieval
> is not allowed

docker-compose.yml

version: "3"   
services:
  employee-mysql:
             image: employee-jdbc
             container_name: employee-jdbc-container
             ports:
             - "9090:9090"
             networks:
               - employee-mysql2    
             depends_on:
               - mysqldb
  mysqldb:
         image: mysql:8
         container_name: mysqldb
         ports:
         - "3306:3306"
         networks:
           - employee-mysql2
         environment:
           - MYSQL_USER=root
           - MYSQL_ROOT_PASSWORD=root
           - MYSQL_DATABASE=HR
networks:                     
    employee-mysql2: 

application.yml

server:
  port: 9090

spring:
  datasource:
    url: "jdbc:mysql://mysqldb:3306/HR?createDatabaseIfNotExists=true&autoReconnect=true&useSSL=false"
    username: root
    password: root
    platform: mysql
    initialization-mode: always
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect

I think the problem in the configuration file but have no idea what is wrong.

答案1

得分: 2

最终,我找到了另一种方式来运行这两个容器,没有任何问题。

**首先设置MySQL环境**

> docker pull mysql:8

然后将镜像作为容器运行

    docker container run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=<root_password> -e MYSQL_DATABASE=<database_name>
    -e MYSQL_USER=<user> -e MYSQL_PASSWORD=<password> mysql:8(镜像名称及版本)

**其次设置Spring Boot环境**

*application.yml*

    server:
      port: 9090
    
    spring:
      datasource:
        url: "jdbc:mysql://mysql-db/<database_name>?createDatabaseIfNotExists=true&autoReconnect=true&allowPublicKeyRetrieval=true&useSSL=false"
        username: <username>
        password: <password>
        initialization-mode: always
        driver-class-name: com.mysql.cj.jdbc.Driver
        tomcat:
          test-while-idle: true
          validation-query: SELECT 1
      jpa:
        database-platform: org.hibernate.dialect.MySQL8Dialect
        show-sql: true
        hibernate:
          ddl-auto: update

*Docker文件*

    FROM openjdk:11
    COPY ./target/employee-mysql.jar employee-mysql.jar
    EXPOSE <port number>(应用程序运行的端口)
    ENTRYPOINT ["java","-jar","employee-mysql.jar"]

为Spring Boot应用构建一个Docker镜像

> docker image build -t employee-jdbc .

将employee-jdbc镜像作为容器运行

    docker container run -d --name employee-mysql -p 9090:9090 --link mysql-db:mysql(mysql是mysql-db容器的镜像)employee-jdbc(employee-jdbc是employee-mysql容器的镜像)
英文:

finally, I have found another way to run both containers without any issue.

first set MySQL environment

> docker pull mysql:8

then run the image as a container

docker container run -d --name mysql-db -e MYSQL_ROOT_PASSWORD=&lt;root_password&gt; -e MYSQL_DATABASE=&lt;database_name&gt;
-e MYSQL_USER=&lt;user&gt; -e MYSQL_PASSWORD=&lt;password&gt;  mysql:8(image name with version)

second set spring boot environment

application.yml

server:
  port: 9090

spring:
  datasource:
    url: &quot;jdbc:mysql://mysql-db/&lt;database_name&gt;?createDatabaseIfNotExists=true&amp;autoReconnect=true&amp;allowPublicKeyRetrieval=true&amp;useSSL=false&quot;
    username: &lt;username&gt;
    password: &lt;password&gt;
    initialization-mode: always
    driver-class-name: com.mysql.cj.jdbc.Driver
    tomcat:
      test-while-idle: true
      validation-query: SELECT 1
  jpa:
    database-platform: org.hibernate.dialect.MySQL8Dialect
    show-sql: true
    hibernate:
      ddl-auto: update

Docker file

FROM openjdk:11
COPY ./target/employee-mysql.jar employee-mysql.jar
EXPOSE &lt;port number&gt; (where your application is running)
ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;employee-mysql.jar&quot;]

build a docker image for spring boot app

> docker image build -t employee-jdbc .

run the employee-jdbc image as a container

docker container run -d --name employee-mysql -p 9090:9090 --link mysql-db:mysql(mysql is the image of mysql-db container) employee-jdbc(employee-jdbc is the image of employee-mysql container)

huangapple
  • 本文由 发表于 2020年8月31日 16:36:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63667449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定