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

无法连接到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 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建