doujiaozhan4397
doujiaozhan4397
2018-08-29 10:56

无法连接到mysql docker镜像连接被拒绝

已采纳

I just replaced my old company computer for a new one(MACOS), download the projects and now Im trying to connect to mysql docker image but I always get

dial tcp 127.0.0.1:3306: connect: connection refused

In my old computer everything worked correctly but now I've this problem.

My docker compose(Not showing all the content):

version: "3"
services:
  mysql:
    image: mysql:5.6
    ports:
      - "3306:3306"
    volumes:
      - mysql-data:/var/lib/mysql
      - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    environment:
      MYSQL_ROOT_PASSWORD: a
      LANG: C.UTF-8
  adminer:
    image: adminer
    ports:
      - 8082:8080
  nginx:
    build: ../docker-shared/nginx
    ports:
      - 443:443
    volumes:
      - "./nginx_proxy_settings.conf:/etc/nginx/conf.d/nginx_proxy_settings.conf"
volumes:
  mysql-data:

So if I do a docker-compose up everything works, you can check in the next image the Adminer is working with data: enter image description here

enter image description here enter image description here

This is my Golang code to connect to mysql:

func main() {

    dbConfig := mysql.NewConfig()
    dbConfig.User = "root"
    dbConfig.Passwd = "a"
    dbConfig.Addr = "mysql"
    dbConfig.DBName = "company_prod"

    db, err := sql.Open("mysql", dbConfig.FormatDSN())
    if err != nil {
        panic(err)
    }
    defer db.Close()
}

Do you know what Im doing wrong??

Thank you

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douxi9245 douxi9245 3年前

    The problem is that your go code can't resolve the mysql address, as it's not being deployed in the compose file.

    To fix that, you have two solutions:

    1. Add your app to your docker-compose file by Dockerizing your code if it isn't already done, then it should be able to connect to your mysql container.
    2. Expose your mysql container's ports and change the address used in your go code from mysql to localhost:3306 (I see that you edited your compose and the ports are exposed, so you just need to change the address in your code)

    For the first solution, you can build a simple Go app into a Docker image like this:

    # Build stage
    FROM golang:alpine AS build-env
    
    COPY . /go/src/your/project/path
    WORKDIR /go/src/your/project/path
    
    RUN apk update && \
        apk upgrade && \
        <install your deps here if needed>
    
    # Install dep if needed
    ENV DEP_VERSION="0.4.1"
    RUN curl -L -s https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 -o $GOPATH/bin/dep
    RUN chmod +x $GOPATH/bin/dep
    RUN dep ensure
    
    # Build your app
    RUN go build -o myapp
    
    # Final stage
    FROM alpine
    
    WORKDIR /app/myapp
    COPY --from=build-env /go/src/your/project/path /app/myapp
    ENTRYPOINT ["/app/myapp/myapp"]
    

    Then add it to your compose file:

    version: "3"
    services:
      mysql:
        image: mysql:5.6
        ports:
          - "3306:3306"
        volumes:
          - mysql-data:/var/lib/mysql
          - ./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
        environment:
          MYSQL_ROOT_PASSWORD: a
          LANG: C.UTF-8
      adminer:
        image: adminer
        ports:
          - 8082:8080
      myapp:
        build: .
        depends_on:
          - mysql
      nginx:
        build: ../docker-shared/nginx
        ports:
          - 443:443
        volumes:
          - "./nginx_proxy_settings.conf:/etc/nginx/conf.d/nginx_proxy_settings.conf"
    volumes:
      mysql-data:
    

    And add the port and the transport to your app's code:

    func main() {
    
        dbConfig := mysql.NewConfig()
        dbConfig.User = "root"
        dbConfig.Passwd = "a"
        dbConfig.Addr = "mysql:3306"
        dbConfig.DBName = "websays_prod"
        dbConfig.Net = "tcp"
    
        db, err := sql.Open("mysql", dbConfig.FormatDSN())
        if err != nil {
            panic(err)
        }
        defer db.Close()
    }
    

    Second solution if you don't want to dockerize your app is to just change your code to:

    func main() {
    
        dbConfig := mysql.NewConfig()
        dbConfig.User = "root"
        dbConfig.Passwd = "a"
        dbConfig.Addr = "localhost:3306"
        dbConfig.DBName = "websays_prod"
        dbConfig.Net = "tcp"
    
        db, err := sql.Open("mysql", dbConfig.FormatDSN())
        if err != nil {
            panic(err)
        }
        defer db.Close()
    }
    
    点赞 评论 复制链接分享

相关推荐