docker-compose 部署golang,mysql, 远程,127.0.0.1均可访问容器mysql, 但go容器里连不上. docker-compose.yaml文件如下:
version: '2'
services:
order_mysql:
networks:
- test_network
image: mysql
command: --default-authentication-plugin=mysql_native_password
container_name: "order_mysql"
ports:
- "3333:3306"
expose:
- "3306"
environment:
MYSQL_DATABASE: order
MYSQL_USER: root
MYSQL_PASSWORD: 123456
TZ: Asia/Shanghai
restart: always
volumes:
- "./mysql/db:/var/lib/mysql"
- "./mysql/conf/my.cnf:/etc/my.cnf"
- "./mysql/init:/docker-entrypoint-initdb.d/"
order_redis:
image: redis
command: --requirepass 12345678
container_name: "order_redis"
ports:
- "6666:6379"
volumes:
- "./redis:/var/lib/redis"
networks:
- test_network
order_golang:
depends_on:
- order_mysql
- order_redis
build: .
ports:
- "2020:2020"
tty: true
networks:
- test_network
links:
- order_mysql:order_mysql
- order_redis:order_redis
networks:
test_network:
host使用的是别名order_mysql的地址, 网段也分配了,order_golang容器编译没问题,容器连mysql怎么连不上
dial tcp 172.21.0.2:3306: connect: connection refused
给 MySql 的部分加个 command
--default-authentication-plugin=mysql_native_password
。或者你指定一下 mysql 的镜像版本为 5.7 及以前。