简体   繁体   English

我无法使用 docker-compose 从服务器容器连接 mysql 容器

[英]I cannot connect the mysql container from server container using the docker-compose

I have been trying to connect golang container with the mysql container for nearly 5 days and every time I face the Issue about the "connection refused" problem, So I have added the Wait so that golang container wait for connection when the Mysql container is preparing itself...but adding that Wait command in docker file and docker compose file doesn't resolve anything.我一直在尝试将 golang 容器与 mysql 容器连接近 5 天,每次我遇到关于“连接被拒绝”问题的问题,所以我添加了 Wait 以便 golang 容器在 Mysql 容器正在准备时等待连接本身...但是在 docker 文件和 docker 撰写文件中添加等待命令并不能解决任何问题。

This is the Error I have been facing这是我一直面临的错误

 docker-compose up
Creating network "bucket-api_fullstack" with driver "br
Creating bucket-database ... done
Creating bucket-api      ... done
Attaching to bucket-database, bucket-api
bucket-api      | -------------------------------------
bucket-api      |  docker-compose-wait 2.7.2
bucket-api      | ---------------------------
bucket-api      | Starting with configuration:
bucket-api      |  - Hosts to be waiting for: [bucket-m
bucket-api      |  - Timeout before failure: 300 second
bucket-api      |  - TCP connection timeout before retr
bucket-api      |  - Sleeping time before checking for 
bucket-api      |  - Sleeping time once all hosts are a
bucket-api      |  - Sleeping time between retries: 30 
bucket-api      | -------------------------------------
bucket-api      | Checking availability of bucket-mysql
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Ent MySQL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Entd user 'mysql'
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Ent MySQL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-13T06:01:45.306754Z 0 [Systemn/mysqld (mysqld 8.0.23) starting as process 1
bucket-database | 2021-02-13T06:01:45.415685Z 1 [Systemnitialization has started.
bucket-database | 2021-02-13T06:01:47.738724Z 1 [Systemnitialization has ended.
bucket-database | 2021-02-13T06:01:48.172811Z 0 [System ready for connections. Bind-address: '::' port: 33060,.sock
bucket-database | 2021-02-13T06:01:48.350857Z 0 [System XA crash recovery...
bucket-database | 2021-02-13T06:01:48.357430Z 0 [System recovery finished.
bucket-database | 2021-02-13T06:01:48.743684Z 0 [Warninificate ca.pem is self signed.
bucket-database | 2021-02-13T06:01:48.744238Z 0 [System
mysql_main configured to support TLS. Encrypted connect channel.
bucket-database | 2021-02-13T06:01:48.918000Z 0 [Warnine configuration for --pid-file: Location '/var/run/mysqo all OS users. Consider choosing a different directory
bucket-database | 2021-02-13T06:01:49.233036Z 0 [Systemn/mysqld: ready for connections. Version: '8.0.23'  socck'  port: 3306  MySQL Community Server - GPL.
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Timeout! After 300 seconds some hosts
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Timeout! After 300 seconds some hosts
bucket-api exited with code 1
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Host bucket-mysql:3305 not yet availa
bucket-api      | Timeout! After 300 seconds some hosts
bucket-api exited with code 1
bucket-api      | Host bucket-mysql:3305 not yet availa

This is my Dockerfile这是我的 Dockerfile

  #Builder image build the go binary : Setting the alias as builder
FROM golang:1.15-alpine as builder
RUN mkdir /app

# add this file directory to the image
ADD . /app
# directory where the source file execution takes place
WORKDIR /app

# RUN some go commands
RUN go clean --modcache
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./src/

# Our production image used to run our app
FROM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk add --no-cache git make musl-dev go
COPY --from=builder /app/main .

# Add docker-compose-wait tool -------------------
ENV WAIT_VERSION 2.7.2
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
RUN chmod +x /wait


# Configure GO
ENV GOROOT /usr/lib/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH

RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
EXPOSE 8084
CMD ["./main"]

This is my dockcer-compose file这是我的 docker-compose 文件

version: '3.8'

services: 
  # MYSQL DATABASE DOCKER
  bucket-mysql:
    image: mysql:latest
    container_name: bucket-database
    command: --default-authentication-plugin=mysql_native_password
    ports: 
      - "3305:3306"
    expose: 
      - "3305"
    environment: 
      MYSQL_ROOT_PASSWORD: "123"
      MYSQL_USER: "bu"
      MYSQL_PASSWORD: "root"
      MYSQL_DATABASE: "rb_db"
    networks: 
      - fullstack
    volumes: 
      - database_mysql:/var/lib/mysql
    restart: always
    cap_add: 
      - SYS_NICE
    healthcheck: 
      test: ["CMD-SHELL", "echo 'select 1' |mysql -u root -p'pagal!123' --silent"]
      interval: 30s
      timeout: 20s
      retries: 6
  
  #API DOCKER 
  bucket:
    image: bucketapi
    build: . 
    container_name: bucket-api
    command: sh -c "/wait"
    environment:
      WAIT_HOSTS: bucket-mysql:3305
      WAIT_HOSTS_TIMEOUT: 300
      WAIT_SLEEP_INTERVAL: 30
      WAIT_HOST_CONNECT_TIMEOUT: 30
      MYSQL_ROOT_PASSWORD: "pagal!123"
      MYSQL_USER: "bu"
      MYSQL_PASSWORD: "123"
      MYSQL_DATABASE: "rb_db"
      # MYSQL_HOST: "bucket-database"
      # MYSQL_PORT: "3305"
    ports: 
      - "8084:8084"
    restart: on-failure
    # volumes: 
    #   - ./src:/usr/src/app/
    depends_on: 
      - bucket-mysql
    networks: 
      - fullstack


volumes: 
    database_mysql:
  
networks: 
  fullstack:
    driver: bridge

This is my Go MySQL connection code这是我的 Go MySQL 连接代码

func init() {
    // loaDerr := godotenv.Load()
    // if loaDerr != nil {
    //  log.Fatal("Error loading .env file")
    // }

    username := os.Getenv("MYSQL_USER")
    password := os.Getenv("MYSQL_ROOT_PASSWORD")
    host := os.Getenv("WAIT_HOSTS")
    port := os.Getenv("MYSQL_PORT")
    schema := os.Getenv("MYSQL_DATABASE")

    fmt.Println(username, password, host, port, schema)

    dataSourceName := fmt.Sprintf(
        "%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
        username,
        password,
        host,
       // port,
        schema,
    )

    var err error
    //connect to the database server
    client, err = sql.Open("mysql", dataSourceName)
    if err != nil {
        panic(err)
    }

    //checking the connection
    if connectionErr := client.Ping(); connectionErr != nil {
        panic(connectionErr.Error())
    }

    fmt.Println("Database connection is been established succesfully")
}

please help me in this i have tried all the combinations but its not working, If I connect the mysql workbench with the running mysql container it connect normally and don't give any error but doing connection with docker and golang it giving me an error thankuu请帮助我,我已经尝试了所有组合但它不起作用,如果我将 mysql 工作台与正在运行的 mysql 容器连接起来,它会正常连接并且不会给出任何错误,但与 Z05B6053C41A21430AFD6FC3BEZ18

In your DSN, you didn't set the port, which was changed from default 3306 -> 3305 in your docker-compose在您的 DSN 中,您没有设置端口,该端口已从docker-compose中的默认 3306 -> 3305 更改

 fmt.Println(username, password, host, port, schema)

    dataSourceName := fmt.Sprintf(
        "%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",
        username,
        password,
        host,
        schema,
    )

Add port to DSN将端口添加到 DSN

 fmt.Println(username, password, host, port, schema)

    dataSourceName := fmt.Sprintf(
        "%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
        username,
        password,
        host,
        port
        schema,
    )

Whenever you make a connection between containers, Docker uses the "normal" port the service listens on.每当您在容器之间建立连接时,Docker 都会使用服务侦听的“正常”端口。 If you're connecting to a MySQL or MariaDB container, it always uses port 3306;如果您连接到 MySQL 或 MariaDB 容器,它始终使用端口 3306; ports: aren't required for this case, and if they are, they're ignored. ports:在这种情况下不需要,如果是,它们将被忽略。

services:
  bucket-mysql:
    image: mysql:latest
    # neither expose: nor ports: are required
  bucket:
    build: .
    environment:
      WAIT_HOSTS: bucket-mysql:3306 # ignores ports: remapping
      MYSQL_HOST: bucket-mysql
      # MYSQL_PORT: "3306"          # this is the default port

( expose: does almost nothing in modern Docker and it's safe to delete that; if you did have it, the port number there is the port number inside the container, and the mysql image already has EXPOSE 3306 in its Dockerfile. You can also safely rely on Compose's default values for networks: , container_name: , and the built image: name, plus you shouldn't usually need to override command: from what your Dockerfile specifies for its CMD . This might help simplify your docker-compose.yml file.) ( expose: does almost nothing in modern Docker and it's safe to delete that; if you did have it, the port number there is the port number inside the container, and the mysql image already has EXPOSE 3306 in its Dockerfile. You can also safely依赖于 Compose 的默认值networks: , container_name: , 和 build image: name, 而且你通常不需要覆盖command:从你的 Dockerfile 为它的CMD指定的。这可能有助于简化你的docker-compose.yml )5AEEF9.

Have a look at your setting:看看你的设置:

WAIT_HOSTS: bucket-mysql:3305 WAIT_HOSTS:bucket-mysql:3305

You are using the same in your GO app and it should be 3306!您在 GO 应用程序中使用的是相同的,它应该是 3306!

3305 is the port it is running on your host machine 3305 是它在您的主机上运行的端口

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

相关问题 使用docker-compose的Rails应用不会连接到mysql容器 - Rails app using docker-compose wont connect to the mysql container docker-compose:无法将应用程序容器与 mysql 容器连接 - docker-compose: Can't connect application container with mysql container Java 容器无法使用 docker-compose 连接到 MYSQL 容器 - Java container cant connect to MYSQL container with docker-compose Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose - Docker MySQL - cant connect SpringBoot app to MySQL database in docker container using docker-compose 无法在docker-compose中使用mysql容器和flyway连接docker Spring-Boot应用程序 - Cannot connect docker Spring-Boot app with mysql container and flyway in docker-compose Tomcat容器和Mysql容器使用docker-compose连接 - Tomcat container and Mysql container join using docker-compose Docker-compose 从另一个容器访问 MySQL 容器 - Docker-compose MySQL container access from another container 容器初始化后如何连接到docker-compose mysql? - How to connect to docker-compose mysql after container has initilized? 无法使用服务名称连接到 docker-compose 中定义的 mysql 容器 - Unable to connect to mysql container defined in docker-compose using service name 将NodeJS Docker容器连接到MySQL Docker容器-docker-compose 3 - Connecting NodeJS Docker container to a MySQL Docker container - docker-compose 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM