在docker-compose中拒绝了Mysql连接。

huangapple go评论80阅读模式
英文:

Mysql connection refused in docker-compose

问题

我在docker-compose中的golang应用程序中连接MySQL时遇到了问题。我可以从控制台连接到数据库:mysql -u user -D data -h 0.0.0.0 -P3306 -p
但是,使用docker-compose时无法连接。

我的docker-compose.yml文件如下:

version: '3'

services:

  app:
    build: ./
    volumes:
      - ./internal/app:/app
      - ./logs:/var/log/parser
    links:
      - db

  db:
    image: mysql:8
    container_name: mysqldb
    ports:
      - "3306:3306"
    volumes:
      - ./docker/mysql/conf:/etc/mysql/conf/conf.d
      - ./docker/mysql/logs/:/var/log/mysql
      - ./docker/mysql/init:/docker-entrypoint-initdb.d
      - ./docker/mysql/data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: data
      MYSQL_USER: user
      MYSQL_PASSWORD: pass

在我的app main.go文件中:

	dbConfig := mysql.NewConfig()
	dbConfig.User = "user"
	dbConfig.Passwd = "pass"
	dbConfig.Addr = "mysqldb"
	dbConfig.DBName = "data"
	dbConfig.Net = "tcp"
	db, err := sql.Open("mysql", dbConfig.FormatDSN())
	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

但是我得到了以下错误:

panic: dial tcp 172.20.0.2:3306: connect: connection refused
英文:

I have a problem connecting to MySQL from my golang app in docker-compose. I can connect to db from console: mysql -u user -D data -h 0.0.0.0 -P3306 -p
But, I can't connect when using docker-compose.

My docker-compose.yml

version: '3'

services:

  app:
    build: ./
    volumes:
      - ./internal/app:/app
      - ./logs:/var/log/parser
    links:
      - db

  db:
    image: mysql:8
    container_name: mysqldb
    ports:
      - "3306:3306"
    volumes:
      - ./docker/mysql/conf:/etc/mysql/conf/conf.d
      - ./docker/mysql/logs/:/var/log/mysql
      - ./docker/mysql/init:/docker-entrypoint-initdb.d
      - ./docker/mysql/data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: data
      MYSQL_USER: user
      MYSQL_PASSWORD: pass

In my app main.go

	dbConfig := mysql.NewConfig()
	dbConfig.User = "user"
	dbConfig.Passwd = "pass"
	dbConfig.Addr = "mysqldb"
	dbConfig.DBName = "data"
	dbConfig.Net = "tcp"
	db, err := sql.Open("mysql", dbConfig.FormatDSN())
	if err != nil {
		panic(err.Error())
	}
	defer db.Close()

But I get this error:

panic: dial tcp 172.20.0.2:3306: connect: connection refused

答案1

得分: 2

我认为你的golang服务在MySQL服务之前启动。所以你必须先启动MySQL服务,然后再启动golang服务,可以使用depends_on来实现这一点。

新的docker-compose.yml文件中包含depends_on:

version: '3'

services:

  app:
    build: ./
    volumes:
      - ./internal/app:/app
      - ./logs:/var/log/parser
    depends_on:
      - mysqldb
    links:
      - db

  db:
    image: mysql:8
    container_name: mysqldb
    ports:
      - "3306:3306"
    volumes:
      - ./docker/mysql/conf:/etc/mysql/conf/conf.d
      - ./docker/mysql/logs/:/var/log/mysql
      - ./docker/mysql/init:/docker-entrypoint-initdb.d
      - ./docker/mysql/data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: data
      MYSQL_USER: user
      MYSQL_PASSWORD: pass

英文:

I think your golang service starts before the MySQL service. so you have to start MySQL service first then golang service so, use depends_on to achieve that.

new docker-compose.yml with depends_on

version: '3'

services:

  app:
    build: ./
    volumes:
      - ./internal/app:/app
      - ./logs:/var/log/parser
    depends_on:
      - mysqldb
    links:
      - db

  db:
    image: mysql:8
    container_name: mysqldb
    ports:
      - "3306:3306"
    volumes:
      - ./docker/mysql/conf:/etc/mysql/conf/conf.d
      - ./docker/mysql/logs/:/var/log/mysql
      - ./docker/mysql/init:/docker-entrypoint-initdb.d
      - ./docker/mysql/data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: pass
      MYSQL_DATABASE: data
      MYSQL_USER: user
      MYSQL_PASSWORD: pass

huangapple
  • 本文由 发表于 2021年10月8日 16:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/69493131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定