Skip to content

Instantly share code, notes, and snippets.

@alastori
Last active December 20, 2019 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alastori/96774977e46158d8c3e26a5ddfbca847 to your computer and use it in GitHub Desktop.
Save alastori/96774977e46158d8c3e26a5ddfbca847 to your computer and use it in GitHub Desktop.
# Get latest MySQL image available at Docker Hub
# https://hub.docker.com/_/mysql/ (not Oracle MySQL)
docker pull mysql:latest
docker images
# Create and run a container with MySQL named as 'mysql8'
docker run --name mysql8 -e MYSQL_ROOT_PASSWORD=Root123! -d mysql:latest
# Check if the constainer 'mysql8' is running
docker ps
# Check the IP to connect to MySQL
docker inspect mysql8 | grep IPAddress
# Start another client container and use mysql client to connect to the running MySQL in 'mysql8'
docker run -it --link mysql8:mysql --rm mysql sh -c 'exec mysql -h172.17.0.2 -P3306 -uroot -p"Root123!"'
# mysql> SELECT VERSION(); quit
# Stop the container
docker stop mysql8
docker ps
# Remove the container
docker rm mysql8
docker ps -a
# Remove the MySQL image
docker images
docker rmi mysql
# Remove everything unused
docker system prune -a
# Using an external client (ex. MySQL Workbench)
# Create and run a container with mapped 3306 port
docker run --name mysql8at3306 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=Root123! -d mysql:latest
docker port mysql8at3306
# Connect and create a user with permissions to connect remotely
docker inspect mysql8at3306 | grep IPAddress
docker run -it --link mysql8at3306:mysql --rm mysql sh -c 'exec mysql -h172.17.0.2 -P3306 -uroot -p"Root123!"'
# mysql> SELECT user, host FROM mysql.user WHERE host = '%';
# must see the MySQL users that can connect remotely
# In Workbench use 'localhost' and port '3306'
# References
# https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment