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
1 Answer
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>`;
1 Comment
Muhammed Iqbal P B
is there is any way to connect containers running with docker-compose to mysql localhost without exposing ip to public
docker-composestacks? I don't want to create individual containers(mysql) for each services <- This doesn't make much sense. You would have one db service for all the other containers to use normally.