简体   繁体   中英

Import multiple database schemas in mysql in docker container

want to upload multiple database schema using backup.sql. Then when try to migrate showing (1044, "Access denied for user 'pranay'@'%' to database 'core'") I have added snapshot of my files for reference

***docker-compose.yml***
version: '3'
services:
db:
image: mysql:5.7
container_name: mirror_core
volumes:
  - ./mirror/core.sql:/docker-entrypoint-initdb.d/core.sql:rw
  - ./mysql:/var/lib/mysql:rw
expose:
  - "3306"
restart: always
environment:
  - MYSQL_ROOT_PASSWORD=mobigo@123
  - MYSQL_USER=pranay
  - MYSQL_PASSWORD=mobigo@123
web:
build: .
container_name: mirrorweb
command: bash -c "python manage.py collectstatic --no-input && gunicorn mirror.wsgi -b 0.0.0.0:8000"
links:
  - db
volumes:
  - ./mirror:/mirror
expose:
  - "8000"
depends_on:
  - db

core.sql

CREATE DATABASE  `core` ;
CREATE DATABASE  `murad` ;
CREATE DATABASE  `mysqltest` ;

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'core',
        'USER':'pranay',
        'PASSWORD':'mobigo@123',
        'HOST':'db',
        'PORT':'',
    }
}

steps are as follows : docker-compose build >> docker-compose up >> docker-compose exec web bash >> python manage.py migrate (within docker container) on migrate getting error as (1044, "Access denied for user 'pranay'@'%' to database 'core'")

The problem is the @ in the password. You need to escape it in you docker-compose.yml Python uses password with @123 while compose treats it differently and therefore is not the correct password you've set up for mysql.

Check the env variable in the container to get what compose really set it as password.

For reference see:

https://symfony.com/doc/current/components/yaml/yaml_format.html :

Strings containing any of the following characters must be quoted. Although you can use double quotes, for these characters it is more convenient to use single quotes, which avoids having to escape any backslash :

:, {, }, [, ], ,, &, *, #, ?, |, -, <, >, =, !, %, @, `

http://yaml.org/spec/1.2/spec.html#id2772075 :

The “@” (#x40, at) and “`” (#x60, grave accent) are reserved for future use.


Maybe also possible, but less likely :

This happens because the moment when service web executes python command, the mysqldb in db service is not yet set up.

See mysql docker readme ( https://hub.docker.com/_/mysql/ ):

No connections until MySQL init completes

If there is no database initialized when the container starts, then a default database will be created. While this is the expected behavior, this means that it will not accept incoming connections until such initialization completes. This may cause issues when using automation tools, such as docker-compose, which start several containers simultaneously.

Try starting up db service (docker-compose up db) and give it several seconds and then try to run the web service.

The depends_on directive only waits for the container to start - docker has no knowledge when the service inside container will be 'ready' - the developer needs to implement this on his own. Usually you just set the web container to start over and over again until it finally succeeds (the db will be ready).

Also, however less recommended, is just to give a sleep 10 before you execute your migration script.

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