1、安装docker环境
https://www.jianshu.com/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,因为我本地已经安装了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测试环境
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.png7、测试下
/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>
网友评论