简体   繁体   中英

Connecting my MySQL database ran with docker-compose to Django

I can't figure out how to get it connected.

When I run the command - python manage.py check --database default I get the error below.

ERROR: django.db.utils.OperationalError: (1049, "Unknown database 'bears-local-db'")

When I log into mysql with the CL I don't see the database when I run show databases;

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bears-local-db',
        'USER': 'user',
        'PASSWORD': 'pass',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }
}
services:
    local-db:
        image: mysql:8.0
        container_name: local-db
        ports:
            - 3308:3306
            # - ${MYSQL_OUTER_PORT}:${MYSQL_INNER_PORT}
        network_mode: host
        environment:
            MYSQL_ROOT_PASSWORD: 'password'
            MYSQL_USER: 'user1'
            MYSQL_DATABASE: bears-local-db
        volumes:
            - ./mysql:/var/lib/mysql

It took me 5 errors to get to this point so I haven't tried a whole lot but messing with the django settings.

Drop the network_mode directive, wrap your ports, and set the user password correctly. (ROOT_PASSWORD variable is required).

version: "3.9"
services:
  db:
    container_name: local-db
    image: mysql:8.0
    environment:
      MYSQL_USER: 'user1'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'rootpassword'
      MYSQL_DATABASE: bears-local-db
    volumes:
      - ./mysql:/var/lib/mysql
    ports:
      - "3308:3306"

At Django settings, you were using the wrong username , also, your user had no password to login with. And pointing to the wrong port where your service is available.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bears-local-db',
        'USER': 'user1',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '3308',
    }
}

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