MySQL - Docker setup
You don’t have MySQL installed to test this example? No problem, one option is to use Docker to run it.
Pull the MySQL Docker Image
docker pull mysql:8.0
Run the MySQL Docker
docker run -d -p 3306:3306 \
--name mysql-docker-container \
-e MYSQL_DATABASE=YNaMi \
-e MYSQL_USER=crazy \
-e MYSQL_PASSWORD=crazy \
-e MYSQL_ROOT_PASSWORD=root \
mysql:8.0
By the way, if you didn't pull the docker image explicitly by running the docker pull mysql:8.0 command, nothing
to worry about as the docker run command will pull the image if it's not found locally.
docker runcommand first creates a writeable container layer over the specified image i.e.mysql:8.0and then starts it using the specified command.-dprints the container ID and runs the container in the background.-ppublishes/exposes a container’s port(s) to the host.–namesets the name of the container i.e.mysql-docker-container.-esets environment variables:MYSQL_USER=crazyhas been setMYSQL_PASSWORD=crazyhas been setMYSQL_DATABASE=YNaMihas been setMYSQL_ROOT_PASSWORD=roothas been set- With the environment variables:
- we have changed the root user password
- named a database that will be created when the image starts up
- created a user that will be granted superuser permissions for the database created
- and set the password of that user
Connect to the MySQL Docker
Run the docker exec command in order to connect to the mysql-docker-container docker container:
docker exec -it mysql-docker-container bash
- The docker exec command runs a new command in an already running container.
- This command creates a new interactive bash shell in the
mysql-docker-containerthrough-itthat tellsDockerto allot apseudo-TTYconnected to the container’sstdin. - In the bash shell:
- enter the
mysql -u root -pcommand to invokeMySQLwith therootuser- Next, you will enter the password which you had set when running the
docker runcommand (i.e.rootin our case).
- Next, you will enter the password which you had set when running the
- or enter the
mysql -u crazy -pcommand to invokeMySQLwith thecrazyuser- Next, you will enter the password which you had set when running the
docker runcommand (i.e.crazyin our case).
- Next, you will enter the password which you had set when running the
- We can run any sql commands now, e.g.,
show databases;
- enter the
That's it, we have up and running MySQL container happily :)
Docker contains slow on macOS
Simplest solution given below but there are others like using NFS:
Delegated
The delegated configuration provides the weakest set of guarantees. For directories mounted with delegated the container’s view of the file system is authoritative, and writes performed by containers may not be immediately reflected on the host file system. In situations such as NFS asynchronous mode, if a running container with a delegated bind mount crashes, then writes may be lost.
Cached
The cached configuration provides all the guarantees of the delegated configuration, and some additional guarantees around the visibility of writes performed by containers. As such, cached typically improves the performance of read-heavy workloads, at the cost of some temporary inconsistency between the host and the container.
Ok, we can use them simply as volume option, for example:
my-delegated-volume:/var/volume2:delegated
my-cached-volume:/var/volume1:cached

