Docker-compose抛出java.sql.SQLSyntaxErrorException:Sping Boot 中的未知数据库

wwodge7n  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(219)

我在Sping Boot 中运行有关CQRS的查询模块时遇到问题。
我无法将数据库与应用程序连接。
下面是account.query的docker-compose.yml

version: '3.9'

services:
  app:
    image: 'account-query:latest'
    build:
      context: .
    ports:
      - "5001:5001"
    depends_on:
      - database
    environment:
      - spring.datasource.url=jdbc:mysql://database:3306/bankAccount?useSSL=false&allowPublicKeyRetrieval=true
      - server.port=5001
      - spring.datasource.username=root
      - spring.datasource.password=root
      - spring.jpa.hibernate.ddl-auto=create
    networks:
      - bankQuery

  database:
    container_name: mysql-database
    image: 'mysql:latest'
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db-data:/var/lib/mysql
    networks:
      - bankQuery
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
      timeout: 20s
      retries: 10

networks:
  bankQuery:

volumes:
  db-data:

当我通过docker-compose --build运行这个文件时,我遇到了这个问题。

java.sql.SQLSyntaxErrorException: Unknown database 'bankAccount'

我该怎么修呢?
下面是链接:Link

dbf7pr2w

dbf7pr2w1#

你需要创建数据库:MYSQL_DATABASE: bankAccount

database:
  container_name: mysql-database
  image: 'mysql:latest'
  ports:
    - "3306:3306"
  restart: always
  environment:
    MYSQL_PASSWORD: root
    MYSQL_ROOT_PASSWORD: root
    MYSQL_DATABASE: bankAccount
  volumes:
    - db-data:/var/lib/mysql
  networks:
    - bankQuery
  healthcheck:
    test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
    timeout: 20s
    retries: 10
daupos2t

daupos2t2#

在将createDatabaseIfNotExist=true添加到数据源URL后,问题消失了。
下面是这部分的环境变量

spring.datasource.url=jdbc:mysql://database:3306/bankAccount?createDatabaseIfNotExist=true&useSSL=false&allowPublicKeyRetrieval=true

相关问题