database docker-compose 与多个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39204142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:02:58  来源:igfitidea点击:

docker-compose with multiple databases

databasedockerdocker-compose

提问by Avanche

I'm trying to figure out how to implement docker using docker-compose.yml with 2 databases imported from sql dumps.

我试图弄清楚如何使用 docker-compose.yml 和从 sql 转储导入的 2 个数据库来实现 docker。

httpd:
    container_name: webserver
    build: ./webserver/
    ports:
        - 80:80
    links:
        - mysql
        - mysql2
    volumes_from:
        - app

mysql:
    container_name: sqlserver
    image: mysql:latest
    ports:
        - 3306:3306
    volumes:
        - ./sqlserver:/docker-entrypoint-initdb.d
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: dbname1
        MYSQL_USER: dbuser
        MYSQL_PASSWORD: dbpass

mysql2:
    extends: mysql
    container_name: sqlserver2
    environment:
        MYSQL_ROOT_PASSWORD: root
        MYSQL_DATABASE: dbname2
        MYSQL_USER: dbuser
        MYSQL_PASSWORD: dbpass

app:
    container_name: webdata
    image: php:latest
    volumes:
        - ../php:/var/www/html
    command: "true"

The above returns the following:

以上返回以下内容:

Kronos:mybuild avanche$ ./run.sh 
Creating sqlserver
Creating webdata
Creating sqlserver2

ERROR: for mysql2  driver failed programming external connectivity on endpoint sqlserver2 (6cae3dfe7997d3787a8d59a95c1b5164f7431041c1394128c14e5ae8efe647a8): Bind for 0.0.0.0:3306 failed: port is already allocated
Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "compose/cli/main.py", line 63, in main
AttributeError: 'ProjectError' object has no attribute 'msg'
docker-compose returned -1

Basically, I'm trying to get my whole stack setup in a single docker compose file, create 2 databases and import the respective sql dumps. Anyone have any suggestions?

基本上,我试图在单个 docker compose 文件中设置我的整个堆栈,创建 2 个数据库并导入相应的 sql 转储。有人有什么建议吗?

采纳答案by Avanche

Just as an update to anyone else who may look into this.

就像对可能调查此问题的任何其他人的更新一样。

I solved this by removing:

我通过删除解决了这个问题:

MYSQL_DATABASE: dbname 

from docker-compose.ymland adding the relevant create database statements directly to the sql file being passed to docker-entrypoint-initdb.d.

docker-compose.yml并将相关的 create database 语句直接添加到传递给docker-entrypoint-initdb.d的 sql 文件中。

At that stage, sql commands are performed under root, so you'll also need to add a statement to grant relevant permissions to the database user you want to use.

在那个阶段,sql命令是在root下执行的,所以你还需要添加一条语句,为你要使用的数据库用户授予相关权限。

回答by mahemoff

Multiple databases in a single Docker container

单个 Docker 容器中的多个数据库

The answers elsewhere on this page set up a dedicated container for each database, but a single MySQL server is capable of hosting multiple databases. Whether you should is a different question, but if you want multiple databases in a single container, here's an example.

本页其他地方的答案为每个数据库设置了一个专用容器,但单个 MySQL 服务器能够托管多个数据库。你是否应该是一个不同的问题,但如果你想在一个容器中包含多个数据库,这里有一个例子

docker-compose.yml:

docker-compose.yml:

version: '3'

volumes:
    db:
        driver: local

    services:
        db:
            image: mysql:5.7
            command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
            volumes:
              - ./docker/provision/mysql/init:/docker-entrypoint-initdb.d
            environment:
              MYSQL_ROOT_PASSWORD: local

docker/provision/mysql/init/01-databases.sql:

docker/provision/mysql/init/01-databases.sql:

# create databases
CREATE DATABASE IF NOT EXISTS `primary`;
CREATE DATABASE IF NOT EXISTS `secondary`;

# create root user and grant rights
CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

How does this work?

这是如何运作的?

This works because the MySQL Docker projecthas an entrypoint scriptthat will run through all files in the /docker-entrypoint-initdb.dfolder, if it exists. This is useful for setting up databases and initializing their schema and data. In docker-compose, we're using volumesto map that virtual folder to a folder on the host system.

这是有效的,因为MySQL Docker 项目有一个入口点脚本,它将运行文件/docker-entrypoint-initdb.d夹中的所有文件(如果存在)。这对于设置数据库和初始化它们的模式和数据很有用。在 docker-compose 中,我们使用volumes将该虚拟文件夹映射到主机系统上的文件夹。

回答by Scadge

You're trying to bind both database containers to the same port - 3306. Which is essentially impossible. You need to change the port-mapping for one of the databases, for example mysqlkeeps 3306:3306, and mysql2should use 3307:3306.

您正在尝试将两个数据库容器绑定到同一个端口 - 3306。这基本上是不可能的。您需要更改其中一个数据库的端口映射,例如mysqlkeep 3306:3306,并且mysql2应该使用3307:3306.

回答by david_adler

version: '3'
services:
  mysql1:
    image: mysql:5.6.26
    environment:
     MYSQL_ROOT_PASSWORD: asdf
     MYSQL_USER: asdf
     MYSQL_HOST: localhost
     MYSQL_PASSWORD: asdf
     MYSQL_DATABASE: asdf
    ports:
      - "3307:3306"
  mysql2:
    image: mysql:5.6.26
    environment:
     MYSQL_ROOT_PASSWORD: asdf
     MYSQL_USER: asdf
     MYSQL_HOST: localhost
     MYSQL_PASSWORD: asdf
     MYSQL_DATABASE: asdf
    ports:
      - "3308:3306"
  • After docker-compose up
  • Connect to mysql1

    mysql -h localhost -uasdf -P 3307 -pasdf asdf --protocol=tcp -D asdf
    
  • Connect to mysql2

    mysql -h localhost -uasdf -P 3308 -pasdf asdf --protocol=tcp -D asdf
    
  • docker-compose up
  • 连接到mysql1

    mysql -h localhost -uasdf -P 3307 -pasdf asdf --protocol=tcp -D asdf
    
  • 连接到mysql2

    mysql -h localhost -uasdf -P 3308 -pasdf asdf --protocol=tcp -D asdf