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

无法连接到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

我刚刚将旧公司的计算机替换为新计算机(MACOS),下载了项目,现在我正尝试连接 到mysql docker image,但我总是得到 p>

拨号TCP 127.0.0.1:3306:connect:连接被拒绝 p> blockquote>

在旧计算机上,一切正常,但现在出现了这个问题。 p>

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

 版本:“ 3” 
服务:
 mysql:
映像:mysql:5.6 
端口:
-“ 3306:3306” 
卷:
-mysql-data:/ var / lib  / mysql 
-./docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
环境:
 MYSQL_ROOT_PASSWORD:a 
 LANG:C.UTF-8 
管理员:
图像:adminer  
端口:
-8082:8080 
 nginx:
构建:../ docker-shared / nginx 
端口:
-443:443 
卷:
-“ ./nginx_proxy_settings.conf:  /etc/nginx/conf.d/nginx_proxy_settings.conf"
volumes:
 mysql-data:
  code>  p  re> 
 
 

因此,如果我执行 docker-compose up code>一切正常,则可以在管理员正在处理数据的下一张图像中检入: ”在此处输入图片描述 p>

”在此处输入图片描述“ ”在此处输入图片描述“ p> \ n

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

  func main(){
 
 dbConfig:= mysql.NewConfig()
 dbConfig.User  =“ root” 
 dbConfig.Passwd =“ a” 
 dbConfig.Addr =“ mysql” 
 dbConfig.DBName =“ company_prod” 
 
 db,错误:= sql.Open(“ mysql”,dbConfig。  FormatDSN())
如果错误!= nil {
 panic(err)
} 
延迟db.Close()\  n} 
  code>  pre> 
 
 

您知道我在做什么错吗? p>

谢谢 p> div >

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