Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

docker-compose 容器间通信,连不上mysql

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

给 MySql 的部分加个 command --default-authentication-plugin=mysql_native_password

或者你指定一下 mysql 的镜像版本为 5.7 及以前。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...