I need to start a MySQL server in order to do some tests. Luckily MySQL has an official docker image. So in 3 simple steps:
Step 1: Download the Image
docker pull mysql/mysql-server:8.0.13
In this case I wanted to download a specific version of MySQL, 8.0.13.
Step 2: Run the Container
docker run -d --name mysql-8 -v /Volumes/USBHDD/mysql/data/:/var/lib/mysql/ -e lower_case_table_names=1 -e MYSQL_ROOT_PASSWORD=root -e MYSQL_ROOT_HOST=% -p 3306:3306 mysql/mysql-server:8.0.13
Explanation of params:
Parameter | Explanation |
-d | Detached mode so that it runs the container in the background. |
–name | Provide a friendly name to the container so that we can start/stop it in the future |
-v | I want to store the MySQL data from the container to an external USB Volume mounted on the host at: /Volumes/USBHDD/mysql/data/. So all data saved by MySQL in will be available in the external USB drive. I believe the proper way is to do it using docker volumes, but this is a quick way as the title says. |
-e lower_case_table_names=1 | The -e parameter allows us to pass properties to the container process. lower_case_table_names=1 is required in a MacOS and Windows Host because the file system is case insensitive. |
-e MYSQL_ROOT_PASSWORD=root | The password of the root user. |
-e MYSQL_ROOT_HOST=% | By default the root user is only allowed to connect from ‘localhost’ (i.e. from within the container). I want to be able to connect as root from the host machine, that’s why this option is required. Note: This is obviously a security hole, but for a dev environment it should be ok. |
-p 3306:3306 | Map port 3306 (the default MySQL port) from container to host. |
Step 3: Connect to container from host
Using MySQL Workbench from the Host machine and connection params:
Host: 127.0.0.1 Username: root Password: root
we can connect to the MySQL running on the container and create a new DB.
That’s it! Now we may want to stop the container.
Step 4: Stop the container
docker stop mysql-8
Step 5: Start the container again
docker start mysql-8
Connecting again using the MySQL Workbench we can see our data and continue development.