简体   繁体   中英

docker-compose unable to connect mysql

I see multiple solutions for this question but still I am unable to make progress hence posting this question I have react+django+mysql app and I want to deploy that into docker, but when I am trying to do my sql image is not created, it says db uses and image, skipping. 在此处输入图像描述 but I see there was no image created for mysql (it had one but I force deleted, so it can get create new one), I tried solution given here and tried to run below command

docker run mysql -h 127.0.0.1 -P 3306

but it asked me give password, root password etc, what went wrong for me? below is my logs在此处输入图像描述

PFb my docker-compose.yaml

version: '3.2'

services:
  db:
    image: mysql:8.0
    ports:
      - '3302:3306'
    environment:
       MYSQL_DATABASE: 'motor_vehicle_collision'
       MYSQL_USER: 'creditshelf_user'
       MYSQL_PASSWORD: '12345678'
       MYSQL_ROOT_PASSWORD: '12345678'
    networks:
      - db-net

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/motor_vehicle_crash
    ports:
      - "5000:8000"
    depends_on:
      - db

  frontend:
    restart: always
    command: npm start
    container_name: front
    build:
      context: ../../../react code/collision-UI/collision-info
      dockerfile: Dockerfile
    ports:
      - "3000:3000"
    stdin_open: true
    depends_on:
      - web
    networks:
      - db-net



networks:
      db-net:
        driver: bridge

below is my python project Dockerfile

FROM python:3.8
ENV PYTHONUNBUFFERED 1
RUN mkdir /collision_bike_info
WORKDIR /collision_bike_info
ADD requirements.txt /collision_bike_info/
RUN pip install -r requirements.txt
ADD . /collision_bike_info/

below my settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'motor_vehicle_collision',
        'USER': 'creditshelf_user',
        'PASSWORD': '12345678',
        'HOST': 'localhost',
        'PORT' : '3306'
        # 'NAME': BASE_DIR / 'db.sqlite3',
    }
}

Solution:

below answer worked for me but however I ran into different problem now my sql says

(1045, 'Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory')

from google I got to know it is due to new mysql version (8.0 onwards) so either I need to downgrade my sql image or I need to change password policy for my user, how do I change policy this user of my sql docker? and if I downgrade it says unkow host db f0r mysql, what should I do? 在此处输入图像描述

Lest straighten some terminology first:

image - is an image of runnable thing, like MySql or Python. It is readonly. You can't connect to image but you can start container out of image. Container is what do the work. Container is based on image plus has it's writable layers. So all changes are saved in those writable layers even if you stop and start it again.

so db uses an image means that docker compose doesn't have to build new image for your db container. But needs to just start container with parameters you've defined. meanwhile docker composer have to build web image using your Dockerfile.

When you do docker run mysql.... you are just spinning up new container from MySql image without using docker compose, thus it is asking all the parameters you've entered in your docker compose file for db container. Don't do this, since you are using docker-compose.

Based on what log says can't connect to local mysql through socket your python script tries to connect to mysql on local host. However your docker file defines MySql as db .

So db should be the host you web tries to find mysql on. Another thing is that your web doesn't use same networks: as other pods.

ps you may want to check what continuers you already running before proceeding. stop all docker compose and check rest. like this:

  • docker-compose down - stop and drop containers defined in composer file.
  • docker ps - to see what containers are running. should be none.
  • docker ps -a - to see all containers including stopped ones.
  • docker stop {container id} to stop something.
  • docker rm {container id} to remove container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM