Monday, 2 May 2022

Docker compose MYSQL user permission

 To grant some user speicifc permission when docker is up


Need to create a directory with 01.sql that does sql cmds and map to docker as volume 

MYSQL docker image will run .sql inside docker-entrypoint-initdb.d

https://github.com/docker-library/mysql/pull/18


https://stackoverflow.com/questions/39204142/docker-compose-with-multiple-databases

* If encountered permission issues(might not needed if docker-compose yml uses uid:gid)  init directory and 01.sql file need have a docker group (chown user:docker init/ -R *)

Project
├── docker-compose.yml (File)
├── init (Directory)
│   ├── 01.sql (File)

then point init directory inside the volumes in the docker-compose.yml file as following

volumes: 
  - ./init:/docker-entrypoint-initdb.d

01.sql

CREATE DATABASE IF NOT EXISTS `test`;
GRANT ALL ON `test`.* TO 'user'@'%';

docker-compose.yml

version: '3.6'
    
services: 
    # MySQL
    db:
        image: mysql
        command: --default-authentication-plugin=mysql_native_password
        restart: always
        environment:
            MYSQL_ROOT_PASSWORD: root
            MYSQL_DATABASE: mydb
            MYSQL_USER: user
            MYSQL_PASSWORD: user

        volumes: 
            - ./init:/docker-entrypoint-initdb.d
    
    adminer:
        image: adminer
        restart: always
        ports:
            - 8080:8080

No comments:

Post a Comment