GluePear
GluePear

Reputation: 7715

Multiple MariaDB databases in Docker

Using Docker (v.2) I have a database set up in my docker-compose.yml:

mysql:
    image: mariadb:latest
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: 'test'
      MYSQL_DATABASE: 'test'
      MYSQL_USER: 'test'
      MYSQL_PASSWORD: 'test'

This works fine. What do I need to do to create another database in the same container?

Upvotes: 8

Views: 22175

Answers (1)

vpa2
vpa2

Reputation: 186

I understand that you want to create two databases in the same container, right? I think it goes against microservices architecture principles.

Anyway, I think this is possible when you are initializing a fresh instance:

When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order.

Update the file under mariadb/mysql directory link :

DROP USER IF EXISTS 'MYSQL_USER';
CREATE USER 'MYSQL_USER'@'%';
CREATE DATABASE IF NOT EXISTS MYSQL_DATABASE;
GRANT ALL ON MYSQL_DATABASE.* TO 'MYSQL_USER'@'%' IDENTIFIED BY 'MYSQL_PASSWORD';
 --------------your new NewDB----------------------
CREATE DATABASE IF NOT EXISTS NewDB;
GRANT ALL ON NewDB.* TO 'MYSQL_USER'@'%' IDENTIFIED BY 'MYSQL_PASSWORD';

Upvotes: 9

Related Questions