首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决在码头连接拒绝中用MySQL运行Golang时的错误

如何解决在码头连接拒绝中用MySQL运行Golang时的错误
EN

Stack Overflow用户
提问于 2022-03-19 05:24:04
回答 1查看 613关注 0票数 0

我已经搜索并使用了这个方法,但是它仍然不工作,连接不工作Golang MySQL Docker connection refused

我的文件

代码语言:javascript
复制
# Start from golang base image
FROM golang:alpine as builder

# ENV GO111MODULE=on

# Install git.
# Git is required for fetching the dependencies.
RUN apk update && apk add --no-cache git

# Set the current working directory inside the container 
WORKDIR /app

# Copy go mod and sum files 
COPY go.mod go.sum ./

# Download all dependencies. Dependencies will be cached if the go.mod and the go.sum files are not changed 
RUN go mod download 

# Copy the source from the current directory to the working Directory inside the container 
COPY . .

# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Start a new stage from scratch
FROM alpine:latest
RUN apk --no-cache add ca-certificates

WORKDIR /root/

# Copy the Pre-built binary file from the previous stage. Observe we also copied the .env file
COPY --from=builder /app/main .
COPY --from=builder /app/.env .       

# Expose port 8080 to the outside world
EXPOSE 2345

#Command to run the executable
CMD ["./main"]

Docker-compose.yml

代码语言:javascript
复制
version: '3.9'
services:
  app:
    container_name: golang_api_container
    build: .
    ports: 
      - 5000:2345 
    restart: on-failure
    volumes:
      - api:/usr/src/app/
    depends_on:
      - golang-mysql          
    networks:
      - fullstack


  golang-mysql:
    image: mysql:5.7
    container_name: db_mysql_container
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 12345
    ports: 
      - 3306:3306 
    volumes:
      - database_mysql:/var/lib/mysql
    networks:
      - fullstack
  
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin_container
    depends_on:
      - golang-mysql
    environment:
      - PMA_HOST=golang-mysql # Note the "golang-mysql". Must be the name of the what you used as the mysql service.
      # - PMA_USER=root
      # - PMA_PORT=${DB_PORT}
      # - PMA_PASSWORD=
      - PMA_ARBITRARY=1
    ports:
      - 3001:80
    restart: always
    networks:
      - fullstack


volumes:
  api:
  database_mysql:                  

# Networks to be created to facilitate communication between containers
networks:
  fullstack:
    driver: bridge

.env文件

代码语言:javascript
复制
# Mysql Live
DB_HOST=db_mysql_container                      
# DB_HOST=127.0.0.1                           # when running the app without docker 
DB_DRIVER=mysql 
API_SECRET=sipil_api2022                          # Used for creating a JWT. Can be anything 
DB_USER=root
DB_PASSWORD=12345
DB_NAME=macan
DB_PORT=3306
# DB_PASSWORD_ROOT=root


# Mysql Test
TEST_DB_HOST=db_mysql_container                        
# TEST_DB_HOST=127.0.0.1                       # when running the app without docker 
TEST_DB_DRIVER=mysql
TEST_API_SECRET=sipil_api2022
TEST_DB_USER=root
TEST_DB_PASSWORD=12345
TEST_DB_NAME=macan_test
TEST_DB_PORT=3306
# DB_PASSWORD_ROOT=root

我的db-config.go

代码语言:javascript
复制
func SetupDBConnection() *gorm.DB { 
    errEnv := godotenv.Load() 
    if errEnv != nil {
        panic("Failed to load env file")
    }

    dbUser := os.Getenv("DB_USER")     
    dbPass := os.Getenv("DB_PASSWORD") 
    dbHost := os.Getenv("DB_HOST")     
    dbName := os.Getenv("DB_NAME")     

    dsn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbName) 
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})                                                             
    if err != nil {
        log.Println("Failed to create a connection to DB")
    } else {
        log.Println("Connection Established to DB")
    }
    return db
}

静态误差

我已经将DB_HOST调整为DB_HOST=db_mysql_container,因为它与mysql的停靠容器上的名称相匹配,但它仍然不连接,连接被拒绝。

EN

回答 1

Stack Overflow用户

发布于 2022-03-19 05:30:05

在部署堆栈/组合时,应该通过服务名称而不是容器名称进行通信。

所以你的DB_HOST应该是golang-mysql。在生产中,您也不希望将端口3306:3306映射到主机。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71535598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档