Muhammed Iqbal P B
Muhammed Iqbal P B

Reputation: 51

How to create multiple mysql database users in a docker-compose file with in a single container?

I have a docker-compose file with more than one service(WordPress,nocodb) required a mysql database, but I don't want to create individual containers(mysql) for each services. I'm planing to use a single mysql container with multiple users. So, how can add multiple users using docker-compose

Upvotes: 5

Views: 11304

Answers (1)

SCcagg5
SCcagg5

Reputation: 648

First of all the whole concept of docker is to isolate each service into separate containers.

However, by using a volume to add sql files into the /docker-entrypoint-initdb.d folder, all the .sql files would be called by the mysql image entrypoint script.

version: '3'

services:
  db:
    image: mysql:5.7
    volumes:
      - ./<your-path>/init:/docker-entrypoint-initdb.d

You could then add a file ./<your-path>/init/01-users.sql

# create your root user
CREATE USER 'root'@'localhost' IDENTIFIED BY 'local';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
# create other users
CREATE USER '<username>'@'localhost' IDENTIFIED BY '<password>';
# grand appropriated rights

and a file ./<your-path>/init/02-databases.sql

# create databases
CREATE DATABASE IF NOT EXISTS `<first>`;
CREATE DATABASE IF NOT EXISTS `<second>`;

Upvotes: 5

Related Questions