简体   繁体   English

使用docker-compose启动dockerized Spring Boot和MariaDb时出现连接拒绝错误

[英]Connection refused error when starting dockerized Spring Boot and MariaDb with docker-compose

I am trying to dockerize a Spring Boot application alongside its MariaDB database using docker-compose , but I get the following error where I run containers using the command 'docker-compose up' 我正在尝试使用docker docker-composeSpring Boot应用程序与其MariaDB数据库并docker-compose ,但我在使用命令'docker-compose up'运行容器时遇到以下错误

java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=localhost)(port=3306)(type=master) : Connection refused (Connection refused)

The service api uses the following Dockerfile : 服务api使用以下Dockerfile

FROM java:8
EXPOSE 8080
ADD target/cm-website-0.0.1-SNAPSHOT.jar cm-website.jar
RUN bash -c 'touch /cm-website.jar'
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "-Dspring.profiles.active=development", "/cm-website.jar"]

My docker-compose.yml file is: docker-compose.yml文件是:

version: '3'

services:
  db:
    container_name: backend-mariadb
    image: mariadb:latest
    environment:
      - MYSQL_ROOT_PASSWORD=pass
      - MYSQL_DATABASE=cmdb
      - MYSQL_USER=cmdb_user
      - MYSQL_PASSWORD=pass
    ports:
      - 3306:3306
  api:
    container_name: backend-api
    image: shaunyl/backend-api
    depends_on:
      - db
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - 8080:8080

My application-development.yml file is: 我的application-development.yml文件是:

spring:
  profiles: development
  main:
    banner-mode: console
  jpa:
    show-sql: true
    generate-ddl: false
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
  datasource:
    name: ds-dev-mariadb
    url: jdbc:mariadb://db:3306/cmdb&useSSL=false
    username: cmdb_user
    password: pass
    driver-class-name: org.mariadb.jdbc.Driver

Do you know what the problem could be? 你知道问题是什么吗? Thank you 谢谢

EDIT 1: Output of docker logs backend-mariadb 编辑1: docker logs backend-mariadb输出

2018-08-23 16:15:37 0 [Note] mysqld (mysqld 10.3.9-MariaDB-1:10.3.9+maria~bionic) starting as process 1 ...
2018-08-23 16:15:37 0 [Note] InnoDB: Using Linux native AIO
2018-08-23 16:15:37 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2018-08-23 16:15:37 0 [Note] InnoDB: Uses event mutexes
2018-08-23 16:15:37 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2018-08-23 16:15:37 0 [Note] InnoDB: Number of pools: 1
2018-08-23 16:15:37 0 [Note] InnoDB: Using SSE2 crc32 instructions
2018-08-23 16:15:37 0 [Note] InnoDB: Initializing buffer pool, total size = 256M, instances = 1, chunk size = 128M
2018-08-23 16:15:37 0 [Note] InnoDB: Completed initialization of buffer pool
2018-08-23 16:15:37 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2018-08-23 16:15:37 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2018-08-23 16:15:37 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2018-08-23 16:15:37 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2018-08-23 16:15:37 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2018-08-23 16:15:37 0 [Note] InnoDB: 10.3.9 started; log sequence number 1630824; transaction id 21
2018-08-23 16:15:37 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
2018-08-23 16:15:37 0 [Note] Plugin 'FEEDBACK' is disabled.
2018-08-23 16:15:37 0 [Note] Server socket created on IP: '::'.
2018-08-23 16:15:37 0 [Warning] 'proxies_priv' entry '@% root@1b6a13ea879f' ignored in --skip-name-resolve mode.
2018-08-23 16:15:37 0 [Note] InnoDB: Buffer pool(s) load completed at 180823 16:15:37
2018-08-23 16:15:37 0 [Note] Reading of all Master_info entries succeded
2018-08-23 16:15:37 0 [Note] Added new Master_info '' to hash table
2018-08-23 16:15:37 0 [Note] mysqld: ready for connections.
Version: '10.3.9-MariaDB-1:10.3.9+maria~bionic'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  mariadb.org binary distribution

From the log your service is still trying to contact localhost but not db . 从日志中,您的服务仍在尝试联系localhost而不是db Are you sure you've rebuilt your JAR file and / or docker image? 您确定要重建JAR文件和/或泊坞窗图像吗? Probably your docker-compose is still running an outdated image with old configurations. 可能你的docker-compose仍在使用旧配置运行过时的图像。

Keep in mind that docker-compose up detects only your docker-compose file changes - it knows nothing about your JAR file or Dockerfile changes. 请记住, docker-compose up仅检测到docker-compose文件更改 - 它对您的JAR文件或Dockerfile更改一无所知。

Sounds like you need to configure your DB for accepting non-localhost connections. 听起来您需要配置数据库以接受非本地主机连接。

Inside of your container, locate this file: /etc/my.cnf and comment out skip-networking and bind-address properties. 在容器内部,找到此文件: /etc/my.cnf并注释掉skip-networkingbind-address属性。

[mysqld]
...
#skip-networking
...
#bind-address = <some ip-address>
...

After that, you need to GRANT access to your user (or any other user that you want to use for remote connections). 之后,您需要授予对您的用户(或您要用于远程连接的任何其他用户)的访问权限。 The statement is something like this: 声明是这样的:

PRIVILEGES ON *.* TO 'root'@'192.168.100.%' IDENTIFIED BY 'my-new-password' WITH GRANT OPTION;

You can find more detailed information here: https://mariadb.com/kb/en/library/configuring-mariadb-for-remote-client-access/ 您可以在此处找到更多详细信息: https//mariadb.com/kb/en/library/configuring-mariadb-for-remote-client-access/

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM