Currently, I am using the following simple YAML file to start a MySQL server
version: '3.3' services: db: image: mysql/mysql-server restart: always environment: MYSQL_DATABASE: 'db' MYSQL_USER: 'user' MYSQL_PASSWORD: 'strongpasswordnobodycanguessorcrackatall' # Password for root access MYSQL_ROOT_PASSWORD: 'somethingevenmorestrongerthaneverbefore' ports: - '3306:3306' expose: - '3306' volumes: - compose-my-db volumes: - ./:compose-my-db:
However, I have to connect to the container and use the following command:
Grant 'c'@'%';refresh permission to all content on *.*;
This gives me all the permissions I need and allows remote connections through the user created in the compose file.
I know I can modify the my.cnf file or add an init script that runs after mysqld is initialized, but I want the user created in Docker-Compose.
I think we can achieve this by
MYSQL_INITDB
- setting up an init script in the MySQL container.First, you can add an environment variable for MYSQL_INITDB like below
Then you need to update the volume configuration as follows
Create a 01.sql file in the same directory as your docker-compose file.
View the official documentation here: https://hub.docker.com/_/mysql/ Under the Initializing a fresh instance subtopic.