doujiaozhan4397 2018-08-29 10:56
浏览 2039
已采纳

无法连接到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 2018-08-29 11:09
    关注

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码