MySQL (Docker)
Working with MySQL within Docker is extremely straightforward.
Begin by downloading MySQL from hub.docker using the pull command docker pull mysql
docker run -p 3306:3306 --name mysql-populated -e MYSQL_ROOT_PASSWORD=<password> -d -v <path to share with container>:/root <image id>
In the command above replace the parameters shown with the values indicated
parameter | value |
---|---|
<password> | memorable password, I keep it simple when developing |
<path to share with container> | //c/Users/Selvyn/demo/vm_share/mysql //c/Users/Selvyn/ must be the path to your account's home directory if you are using windows, or your home directory on Linux/Unix The demo/vm_share/mysql can be any directory under your home folder |
<image id> | Should be the image id for mysql if you run docker images |
Important
//c/ the drive letter must be lower case
Using the above table I use the following command
docker run -p 3306:3306 --name mysql-populated -e MYSQL_ROOT_PASSWORD=ppp -d -v //c/Users/Selvyn/demo/vm_share/mysql:/root mysql-db-unpopulated:latest
Within the demo/vm_share/mysql folder I have the following files (docker exec by default logs in as root)
file | purpose |
---|---|
.bashrc | When you mount an external volume to /root folder of the container the .bashrc will be removed if none is specified in the host environment |
.profile | When you mount an external volume to /root folder of the container the .profile will be removed if none is specified in the host environment |
.bash_history | When you mount an external volume to /root folder of the container the .bash_history will be removed if none is specified in the host environment |
init-populate-mysql-db.sh | Simply invokes mysql --password=<pwd> < <a script file> to execute a mysql script |
create-db-teams-and-lectures-for-2017.sql | Script to create users on the mysql account, must be done before schemas are created |
db_grad_cs_1917.sql | Script to create a schema and populate |
Don't try to access the DB either through docker exec or MySQL workbench for about 20-30 seconds. The docker run (don't confuse docker run with docker start) command will take about 20-30 seconds to fully initialise the container and completely run the scripts above.
I use the following script to do the complete bootstrap (assuming //C/Users/Selvyn/demo/vm_share/mysql has been setup correctly)
echo off echo Starting MySQL, root password=ppp docker run -p 3306:3306 --name mysql-populated -e MYSQL_ROOT_PASSWORD=ppp -d -v //c/Users/Selvyn/demo/vm_share/mysql:/root mysql-db-unpopulated:latest echo MySQL container now running echo echo Waiting for MySQL deamon to initialise, do not interrupt... (CTRL+C will termiate the install) timeout 30 /NOBREAK echo Initialising and populating the DB, can take approximately 5 minutes to complete, please wait... docker exec -it mysql-populated bash -c "cd /root; ./init-populate-mysql-db.sh" echo MySQL available for use... echo