undefinedfix
Sign in

Communication between docker compose containers, unable to connect to MySQL

mkysy edited in Mon, 02 Jan 2023

Docker compose deployment golang, mysql, remote, 127.0.0.1 can access the container mysql, but the go container can not connect. Docker- compose.yaml The documents are as follows:

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 uses the alias order_ MySQL address, network segment also assigned, order_ There is no problem in compiling golang container. Why can't the container connect to MySQL

dial tcp 172.21.0.2:3306: connect: connection refused
3 Replies
ecisse
commented on Mon, 02 Jan 2023

Add a command to the MySQL part -- default authentication plugin = MySQL_ native_ password。

Or you can specify that the image version of MySQL is 5.7 or earlier.

MaorBA
commented on Mon, 02 Jan 2023

Dependencies of docker_ On just starts first. It can't guarantee the success of startup. The main reason is that there is something wrong with golang's code. Golang connects to the database with its own thread pool and reconnection. It should be as long as you log in an error. You don't need to stop the service directly

tbmgp
commented on Mon, 02 Jan 2023

Hello, I tried to add command -- default authentication plugin = mysql_ native_ Connect: connection reused