1、安裝docker環(huán)境
http://www.itdecent.cn/p/754cf17bd8c2
2、下載mysql鏡像
? docker pull mysql:5.7
5.7: Pulling from library/mysql
0a4690c5d889: Pull complete
98aa2fc6cbeb: Pull complete
0777e6eb0e6f: Pull complete
2464189c041c: Pull complete
b45df9dc827d: Pull complete
b42b00086160: Pull complete
bb93567627c7: Pull complete
419b68a254a1: Pull complete
8c4bc5b87c07: Pull complete
59234e88c262: Pull complete
2406ac6e266f: Pull complete
Digest: sha256:540488d8f0e04c1077d17934d1c1511fe417e2221dff508ce4621f5efe6131db
Status: Downloaded newer image for mysql:5.7
3、先啟動下docker里面的mysql
docker run -d \
--name mysql \
-p 3307:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7
這里把容器里面的3306端口映射為3307,因為我本地已經(jīng)安裝了mysql服務端,3306端口被占用了。
4、把容器里面生成的默認配置復制出來,修改為自定義配置
docker cp mysql:/etc/mysql/mysql.conf.d/mysqld.cnf /data/docker/mysql/mysqld.cnf
docker cp mysql:/var/lib/mysql/ /data/docker/mysql/data
這里我們建立/data/docker/mysql 文件夾來存放mysql在docker里面的配置,注意修改下/data的用戶權限
sudo chown -R yourname:staff /data
這里如果不把這些東西copy到容器外,每次重啟這些都會丟失掉,所以必須弄到外面來。
5、刪除臨時容器
docker ps -a |grep mysql
docker stop xxxxxxx
docker rm xxxxxxx
這里的xxxxxxx是mysql容器對應的id值
6、啟動正式的mysql測試環(huán)境
docker run -d \
--name mysql \
-p 3307:3306 \
-v /data/docker/mysql/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \
-v /data/docker/mysql/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.7
啟動報錯了,錯誤如下:
add11d25dd2039975fc0c51f07724d8e5ed5e332b81374d93f252a576a58cd9b
docker: Error response from daemon: Mounts denied:
The paths /data/docker/mysql/data and /data/docker/mysql/mysqld.cnf
are not shared from OS X and are not known to Docker.
You can configure shared paths from Docker -> Preferences... -> File Sharing.
See https://docs.docker.com/docker-for-mac/osxfs/#namespaces for more info.
按照提示把/data/docker/ 這個目錄設置到sharing配置即可。

image.png
7、測試下
/usr/local/mysql/bin ? ./mysql -uroot -P3307 -h127.0.0.1 -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.27 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>