大佬教程收集整理的这篇文章主要介绍了我无法使用 docker-compose 从服务器容器连接 mysql 容器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经尝试将 golang 容器与 MysqL 容器连接近 5 天,每次我都遇到有关“连接被拒绝”问题的问题,所以我添加了等待以便 golang 容器在 MysqL 连接时等待连接容器正在准备自己......但是在 docker 文件和 docker compose 文件中添加该 Wait 命令并没有解决任何问题。
这是我一直面临的错误
docker-compose up
CreaTing network "bucket-API_fullstack" with driver "br
CreaTing bucket-database ... done
CreaTing bucket-API ... done
Attaching to bucket-database,bucket-API
bucket-API | -------------------------------------
bucket-API | docker-compose-wait 2.7.2
bucket-API | ---------------------------
bucket-API | StarTing with configuration:
bucket-API | - Hosts to be waiTing for: [bucket-m
bucket-API | - Timeout before failure: 300 second
bucket-API | - TCP connection timeout before retr
bucket-API | - SleePing time before checking for
bucket-API | - SleePing time once all hosts are a
bucket-API | - SleePing time between retrIEs: 30
bucket-API | -------------------------------------
bucket-API | checking availability of bucket-MysqL
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Ent MysqL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Entd user 'MysqL'
bucket-database | 2021-02-13 06:01:44+00:00 [Note] [Ent MysqL Server 8.0.23-1debian10 started.
bucket-database | 2021-02-13T06:01:45.306754Z 0 [Systemn/MysqLd (MysqLd 8.0.23) starTing as process 1
bucket-database | 2021-02-13T06:01:45.415685Z 1 [Systemnitialization has started.
bucket-database | 2021-02-13T06:01:47.738724Z 1 [Systemnitialization has ended.
bucket-database | 2021-02-13T06:01:48.172811Z 0 [System ready for connections. Bind-address: '::' port: 33060,.sock
bucket-database | 2021-02-13T06:01:48.350857Z 0 [System XA crash recovery...
bucket-database | 2021-02-13T06:01:48.357430Z 0 [System recovery finished.
bucket-database | 2021-02-13T06:01:48.743684Z 0 [Warninificate ca.pem is self signed.
bucket-database | 2021-02-13T06:01:48.744238Z 0 [System
MysqL_main configured to support TLs. Encrypted connect chAnnel.
bucket-database | 2021-02-13T06:01:48.918000Z 0 [Warnine configuration for --pID-file: LOCATIOn '/var/run/mysqo all OS users. ConsIDer choosing a different directory
bucket-database | 2021-02-13T06:01:49.233036Z 0 [Systemn/MysqLd: ready for connections. Version: '8.0.23' socck' port: 3306 MysqL Community Server - GPl.
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Timeout! After 300 seconds some hosts
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Timeout! After 300 seconds some hosts
bucket-API exited with code 1
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Host bucket-MysqL:3305 not yet availa
bucket-API | Timeout! After 300 seconds some hosts
bucket-API exited with code 1
bucket-API | Host bucket-MysqL:3305 not yet availa
这是我的 Dockerfile
#Builder image build the go binary : SetTing the alias as builder
FROM golang:1.15-alpine as builder
RUN mkdir /app
# add this file directory to the image
ADD . /app
# directory where the source file execution takes place
workdir /app
# rUN some go commands
RUN go clean --R_213_11845@odcache
RUN go mod download
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main ./src/
# Our production image used to run our app
FROM alpine:latest
RUN apk --no-cache add ca-certificates
RUN apk add --no-cache git make musl-dev go
copY --from=builder /app/main .
# Add docker-compose-wait tool -------------------
ENV WAIT_VERSION 2.7.2
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
RUN chmod +x /wait
# Configure GO
ENV GOROOT /usr/lib/go
ENV GOPATH /go
ENV PATH /go/bin:$PATH
RUN mkdir -p ${GOPATH}/src ${GOPATH}/bin
EXPOSE 8084
CMD ["./main"]
这是我的 docker-compose 文件
version: '3.8'
services:
# MysqL DATABASE DOCKER
bucket-MysqL:
image: MysqL:latest
container_name: bucket-database
command: --default-authentication-plugin=MysqL_native_password
ports:
- "3305:3306"
expose:
- "3305"
environment:
MysqL_ROOT_password: "123"
MysqL_USER: "bu"
MysqL_password: "root"
MysqL_DATABASE: "rb_db"
networks:
- fullstack
volumes:
- database_MysqL:/var/lib/MysqL
restart: always
cap_add:
- SYS_NICE
healthcheck:
test: ["CMD-SHelL", "echo 'SELEct 1' |MysqL -u root -p'pagal!123' --silent"]
interval: 30s
timeout: 20s
retrIEs: 6
#API DOCKER
bucket:
image: bucketAPI
build: .
container_name: bucket-API
command: sh -c "/wait"
environment:
WAIT_HOSTS: bucket-MysqL:3305
WAIT_HOSTS_TIMEOUT: 300
WAIT_SLEEP_INTERVAL: 30
WAIT_HOST_CONNECT_TIMEOUT: 30
MysqL_ROOT_password: "pagal!123"
MysqL_USER: "bu"
MysqL_password: "123"
MysqL_DATABASE: "rb_db"
# MysqL_HOST: "bucket-database"
# MysqL_PORT: "3305"
ports:
- "8084:8084"
restart: on-failure
# volumes:
# - ./src:/usr/src/app/
depends_on:
- bucket-MysqL
networks:
- fullstack
volumes:
database_MysqL:
networks:
fullstack:
driver: brIDge
这是我的 Go MysqL 连接代码
func init() {
// loaDerr := godotenv.Load()
// if loaDerr != nil {
// log.Fatal("Error loading .env file")
// }
username := os.Getenv("MysqL_USER")
password := os.Getenv("MysqL_ROOT_password")
host := os.Getenv("WAIT_HOSTS")
port := os.Getenv("MysqL_PORT")
schema := os.Getenv("MysqL_DATABASE")
fmt.Println(username, password, host, port, schema)
datasourcename := fmt.Sprintf(
"%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local", username, password, host,// port, schema, )
var err error
//connect to the database server
clIEnt, err = sql.open("MysqL", datasourceName)
if err != nil {
panic(err)
}
//checking the connection
if connectionErr := clIEnt.Ping(); connectionErr != nil {
panic(connectionErr.Error())
}
fmt.Println("Database connection is been established succesfully")
}
请帮助我,我已经尝试了所有组合,但它不起作用,如果我将 MysqL 工作台与正在运行的 MysqL 容器连接,它会正常连接并且不会给出任何错误,但是与 docker 和 golang 连接它会给我一个错误谢谢
在您的 DSN 中,您没有设置端口,在您的 docker-compose
fmt.Println(username,password,host,port,scheR_213_11845@a)
datasourcename := fmt.Sprintf(
"%s:%s@tcp(%s)/%s?charset=utf8&parseTime=True&loc=Local",username,scheR_213_11845@a,)
添加端口到 DSN
fmt.Println(username,scheR_213_11845@a)
datasourcename := fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",port
scheR_213_11845@a,)
,
每当您在容器之间建立连接时,Docker 都会使用服务侦听的“普通”端口。如果您连接到 MySQL 或 MariaDB 容器,它总是使用端口 3306; ports:
在这种情况下不是必需的,如果是,它们将被忽略。
services:
bucket-mysql:
image: mysql:latest
# neither expose: nor ports: are required
bucket:
build: .
environment:
WAIT_HOSTS: bucket-mysql:3306 # ignores ports: remapping
MYSQL_HOST: bucket-mysql
# MYSQL_PORT: "3306" # this is the default port
(expose:
在现代 Docker 中几乎什么都不做,删除它是安全的;如果你有它,端口号就是容器内的端口号,而 @H_623_9@mysql 图像已经有了EXPOSE 3306
在其 Dockerfile 中。您还可以安全地依赖 Compose 的 networks:
、container_name:
和内置 image:
名称的默认值,此外您通常不需要覆盖command:
来自您的 Dockerfile 为其 CMD
指定的内容。这可能有助于简化您的 docker-compose.yml
文件。)
查看您的设置:
WAIT_HOSTS:bucket-mysql:3305
您在 GO 应用中使用的是相同的,应该是 3306!
3305 是它在主机上运行的端口
以上是大佬教程为你收集整理的我无法使用 docker-compose 从服务器容器连接 mysql 容器全部内容,希望文章能够帮你解决我无法使用 docker-compose 从服务器容器连接 mysql 容器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。