C
C
corruptzero2021-12-03 14:33:49
MySQL
corruptzero, 2021-12-03 14:33:49

Docker. MySQL. How to map container to docker volume?

Hello.
Help with launching the container, it's missing here:
Ensure the service is available on port 3306
Ensure databases are saved (mysql-data volume mapping)
Create a database at startup (specify the database name, database user, user password)

Here's what I did:
docker run --name mysql --restart on-failure -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7.36 --collation-server=utf8_general_ci

Stuck at this point, please help.
Docker hub says docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
but I don't understand what to map and why

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-12-03
@q2digger

It's better, for starters, the official dock - https://docs.docker.com/storage/volumes/
in the example there is such a construction -v /my/own/datadir:/var/lib/mysql
, it means - to mount the local folder /my/own/datadir in the container.
In the container, the database will be saved to the /var directory /lib/mysql , as usual, and you mounted your folder to this place when you started the container. If in the future you crash the container, the data folder will remain.
With creating a base, everything is also simple.
You need to prepare a file something-there.sql, in which to describe the creation of the database, the creation of users.
something like

CREATE DATABASE mydb;

CREATE USER 'readuser'@'%' IDENTIFIED BY 'passwordforreaduser';
GRANT SELECT ON mydb.* TO 'readuser'@'%';

CREATE USER 'writeuser'@'%' IDENTIFIED BY 'passwordforwriteuser';
GRANT INSERT, UPDATE, SELECT, DELETE ON mydb.* TO 'writeuser'@'%';

and mount this file -v path/to/you/file:/docker-entrypoint-initdb.d/init.sql
at startup, the database will find it in this folder and execute it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question