Install MySQL based on docker

Install MySQL based on docker

Because of the needs of development and debugging, I want to build a docker development environment on this machine to manage various middleware

My machine configuration: Macbook Pro 2017 16G/256G System version: macOS Catalina 10.15.4

Install docker

docker installation address

Search mysql

docker search mysql
 

The output is as follows:

Because our company uses the mysql5.7 version, I will install the mysql5.7 version and the command is as follows

docker pull mysql:5.7
 

After a while, the console will output the following:

Then enter the following command to check the image that has been downloaded to the local

docker images
 

Start mysql

docker run --name mysql -e MYSQL_ROOT_HOST=172.17.%.% -e MYSQL_ROOT_PASSWORD=123456  -v $PWD/mysql-data/:/var/lib/mysql -d  -p 3306:3306 --restart=always  mysql:5.7
 

The meaning of the parameters is as follows:

  • --name mysql Name the container mysql, which can be used to start and pause the container later
  • -e MYSQL_ROOT_HOST=172.17.%.% Create a root user with the right to log in from the given IP address. If you want to allow login from any IP, you can specify MYSQL_ROOT_HOST=%
  • -e MYSQL_ROOT_PASSWORD=123456 Set MySQL password to 123456
  • -v $PWD/mysql-data/:/var/lib/mysql map and mount the storage directory of the host and container to prevent the container from restarting and data emptying
  • -d This container runs in the background and returns the ID of the container
  • -i Run the container in interactive mode
  • -p for port mapping, the format is host (host) port: container port
  • --restart=always When docker restarts, the container automatically restarts

Enter mysql

docker exec -it mysql/bin/bash
 

Enter the username and password to connect to mysql