Contents

Docker Compose Start Mutiple Database

原因

最近某个业务中,因为第三方系统限制,数据存在两个不同的DB 中。而项目初始化时,要连接这两个不同的db,所以导致在本地调试时候,和跑ci 的时候 就需要可以访问到这两个数据库。为了方便直接用docker-compose 安装

本地playground


version: '3'
    services:
        mysql:
            image: mysql:5.6
            container_name: mysql
            environment:
            MYSQL_USER: user
            MYSQL_PASSWORD: password
            MYSQL_ROOT_PASSWORD: password
            volumes:
            - /data/mysql:/var/lib/mysql
            entrypoint:
            sh -c
                "
                echo 'CREATE DATABASE IF NOT EXISTS firest database; CREATE DATABASE IF NOT EXISTS second_database;' > /docker-entrypoint-initdb.d/init.sql;
                /entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
            "
            ports:
            - "3306:3306"
            restart:
            always
            networks:
            app_net:
                ipv4_address: 172.26.239.15

networks:
  app_net:
    ipam:
      driver: default
      config:
        - subnet: "172.26.239.0/24"

在gitlab ci 中使用

service 中增加一个服务

  services:
    - docker-地址/mysql:5.6

ci 环境变量设置

  MYSQL_DATABASE: database
  MYSQL_USER: username
  MYSQL_PASSWORD: password
  MYSQL_ROOT_PASSWORD: password 

script 中增加相应的脚本

script:
  - apt update && apt-get install -y  libmariadb-dev-compat libmariadb-dev mariadb-client
  - mariadb -h mysql-addr -u root -prootpassword -e "grant all privileges on *.* to database@'%';"
    - mariadb -h  mysql-addr -u username -ppassword -e "create database \`aliyun-sophon\`;"