MySQL has already provided Docker image, which makes it easy to open a MySQL server. There are two ways to connect the official
- Access the MySQL server container through--link in other app Docker containers
- Start another MySQL container as a client, access the MySQL server container via--link
Both of these methods are in the container, and it is simple if the client does not access the Docker container:
1. Pull Mysql Image
$ docker Pull MySQL
The premise is that Docker has been installed, this can be referred to the Docker website, the steps are very detailed.
2. Create an external data mount point
$ sudo mkdir-p/var/docker_data/mysql/data$ sudo chmod 775-r/var/docker_data/msyql/
In addition to this approach, you can also choose to use Docker data volumes.
3. Start the MySQL container
With the-e parameter, you can set the parameters of the MySQL server, where the root password is set and a user dev with a password of Dev is created.
$ docker run-p 3306:3306--name mysql-test-v/var/docker_data/mysql/data/:/var/lib/mysql-e mysql_root_password=root-e Mysql_user=dev-e mysql_password=dev-d MYSQL #查看docker是否启动 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a8ef529a9a1c mysql "docker-entrypoint.sh" secon ds ago Up seconds 0.0.0.0:3306->3306/tcp Mysql-test
4. Link MySQL container in host
Use the IP address portion of the ports display as the-h parameter, and if it is not on the host, use the host's IP address.
Using Docker Mysql 5.7