简体   繁体   English

在docker容器中的mysql中导入多个数据库架构

[英]Import multiple database schemas in mysql in docker container

want to upload multiple database schema using backup.sql. 想要使用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 然后,当尝试迁移显示内容时(1044,“拒绝用户'pranay'@'%'对数据库'core'的访问”),我添加了文件快照以供参考

***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 core.sql

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

settings.py 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'") 步骤如下:docker-compose build >> docker-compose up >> docker-compose exec bash >> python manage.py migration(在docker容器内) 迁移时发生错误,错误为(1044,“用户'pranay的访问被拒绝'@'%'到数据库'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. 您需要在docker-compose.yml中转义它。Python使用@ 123密码,而compose对待密码的方式不同,因此不是您为mysql设置的正确密码。

Check the env variable in the container to get what compose really set it as password. 检查容器中的env变量,以了解将其实际设置为密码的内容。

For reference see: 供参考,请参阅:

https://symfony.com/doc/current/components/yaml/yaml_format.html : 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 : http://yaml.org/spec/1.2/spec.html#id2772075

The “@” (#x40, at) and “`” (#x60, grave accent) are reserved for future use. 保留“ @”(#x40,在)和“`”(#x60,重音)供将来使用。


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. 发生这种情况是因为当服务web执行python命令时,尚未在db服务中设置mysqldb。

See mysql docker readme ( https://hub.docker.com/_/mysql/ ): 参见mysql docker自述文件( https://hub.docker.com/_/mysql/ ):

No connections until MySQL init completes 在MySQL初始化完成之前没有连接

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. 使用自动化工具(例如docker-compose)时,这可能会导致问题,该工具同时启动多个容器。

Try starting up db service (docker-compose up db) and give it several seconds and then try to run the web service. 尝试启动数据库服务(docker-组成数据库),并给它几秒钟,然后尝试运行Web服务。

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. depends_on指令仅等待容器启动depends_on不知道容器中的服务何时“就绪”-开发人员需要自己实现。 Usually you just set the web container to start over and over again until it finally succeeds (the db will be ready). 通常,您只是将Web容器设置为一遍又一遍,直到它最终成功为止(数据库将准备就绪)。

Also, however less recommended, is just to give a sleep 10 before you execute your migration script. 另外,不管怎么说,建议您在执行迁移脚本之前sleep 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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