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

时间:2018-08-29 10:56:08

标签: mysql docker go docker-compose

我只是将旧公司的计算机换成了新的(MACOS),下载了项目,现在我试图连接到mysql docker映像,但是我总是得到

  

拨号TCP 127.0.0.1:3306:连接:连接被拒绝

在我的旧计算机上,一切正常,但是现在我遇到了这个问题。

我的docker compose(未显示所有内容)

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:

因此,如果我执行docker-compose up一切正常,则可以检入下一张管理员正在处理数据的图像: enter image description here

enter image description here enter image description here

这是我连接到mysql的Golang代码:

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()
}

你知道我在做什么错吗?

谢谢

1 个答案:

答案 0 :(得分:1)

问题是您的执行代码无法解析mysql地址,因为该地址未部署在撰写文件中。

要解决此问题,您有两种解决方法:

  1. 通过将代码进行Docker化(如果尚未完成)将您的应用添加到docker-compose文件中,那么它应该能够连接到您的mysql容器。
  2. 公开您的mysql容器的端口,并将go代码中使用的地址从mysql更改为localhost:3306(我看到您编辑了撰写文件,并且这些端口已公开,因此您只需要更改地址输入您的代码)

对于第一个解决方案,您可以像这样在Docker映像中构建一个简单的Go应用:

# 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"]

然后将其添加到您的撰写文件中:

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:

并将端口和传输方式添加到您应用的代码中:

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()
}

如果您不想对应用程序进行Docker化,第二种解决方案是将代码更改为:

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()
}