Mac环境Docker MySQL测试背景
本文主要探讨如何在Mac环境下借助Docker快速搭建MySQL测试环境,欢迎参考实践。
尽管Docker生态发展迅速且讨论热烈,但市面上成熟的入门引导资料相对较少。作者在Mac平台体验后发现,Mac在Docker领域常被视为次要支持对象。受Giuseppe博客中《Docker for Mac beta and MySQL》一文的启发,作者决定亲自验证。该流程兼容Mac(Windows可能适用)及Linux环境(基于GA版本Docker 1.11.1)。
首先需在Mac上注册并下载Docker测试版客户端。此过程耗时约一天,预计正式版发布后将简化操作。安装完成后,即可为常用MySQL版本配置容器,从而建立沙箱环境。具体操作步骤如下:
jayj@~ [510]$ docker network create test
90005b3ffa9fef1f817ee4965e794a567404c9a8d5bf07320514e7d848d59ff9
jayj@~ [511]$ docker run --name=mysql57 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql/mysql-server:5.7
6c80fa89610dbd5418ba474ad7d5451cd061f80a8a72ff2e718341827a08144b
jayj@~ [512]$ docker run -it --rm --net=test -e MYSQL_HOST=mysql57 mysql/shell init
Creating a Classic Session to root@mysql57:3306
Enter password:
No default schema selected.
enableXProtocol: Installing plugin mysqlx...
enableXProtocol: done
Docker网络与容器配置经验
以下是实施过程中的关键经验总结:
作者为容器创建了名为“test”的网络以实现共享,这本质上是容器间的专用私有网络。此举优势在于多个容器监听相关端口时,无需在主机操作系统层面暴露端口。 随后,作者启动了Oracle官方MySQL Docker容器中的MySQL 5.7镜像,并将其绑定至上述测试网络。
使用来自Oracle的MySQL /shell镜像初始化MySQL 5.7服务器上的mysqlx插件。注意此处未设置密码,因为并未创建持久化服务器(虽不安全,但适用于沙箱环境)。 该Shell操作利用临时容器执行后即删除,因此不会干扰`docker ps -a`的输出结果。
为实现通过标准MySQL命令行或新版MySQL Shell访问容器,并保持界面整洁,作者添加了若干bash别名:
alias mysqlsh='docker run -it --rm --net=test mysql/shell'
alias mysql='docker run -it --rm -e MYSQL_ALLOW_EMPTY_PASSWORD=yes --net=test --entrypoint="mysql" mysql/mysql-server:5.7'
此后,可直接调用这些别名,并通过常规命令行选项连接MySQL 5.7镜像,体验如同使用原生MySQL CLI二进制文件。从MySQL 5.7镜像中使用MySQL CLI的效果如下:
jayj@~ [524]$ mysql -h mysql57
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.12 MySQL Community Server (GPL)
Copyright (c) 2000, 2026, 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> show schemas;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
若需使用MySQL Shell进行连接:
jayj@~ [527]$ mysqlsh -h mysql57 -u root --session-type=node
Creating a Node Session to root@mysql57:33060
Enter password:
No default schema selected.
Welcome to MySQL Shell 1.0.3 Development Preview
Copyright (c) 2026, 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', 'h' or '?' for help.
Currently in JavaScript mode. Use sql to switch to SQL mode and execute queries.
mysql-js> sql
Switching to SQL mode... Commands end with ;
mysql-sql> show schemas;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql-sql>
如需检查MySQL 5.5版本,可执行以下操作:
jayj@~ [530]$ docker run --name=mysql55 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql/mysql-server:5.5
Unable to find image 'mysql/mysql-server:5.5' locally
5.5: Pulling from mysql/mysql-server
a3ed95caeb02: Already exists
ffe36b360c6d: Already exists
646f220a8b5d: Pull complete
ed65e4fea7ed: Pull complete
d34b408b18dd: Pull complete
Digest: sha256:12f0b7025d1dc0e7b40fc6c2172106cdf73b8832f2f910ad36d65228d9e4c433
Status: Downloaded newer image for mysql/mysql-server:5.5
6691dd9d42c73f53baf2968bcca92b7f4d26f54bb01d967be475193305affd4f
jayj@~ [531]$ mysql -h mysql55
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 1
Server version: 5.5.49 MySQL Community Server (GPL)
Copyright (c) 2000, 2026, 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> show schemas;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
或者,Percona Server:
jayj@~ [534]$ docker run --name=ps57 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d percona/percona-server:5.7
Unable to find image 'percona/percona-server:5.7' locally
5.7: Pulling from percona/percona-server
a3ed95caeb02: Pull complete
a07226856d92: Pull complete
eee62d87a612: Pull complete
4c6755120a98: Pull complete
10eab0da5972: Pull complete
d5159a6502a4: Pull complete
e595a1a01d00: Pull complete
Digest: sha256:d57f0ce736f5403b1714ff8d1d6b91d5a7ee7271f30222c2bc2c5cad4b4e6950
Status: Downloaded newer image for percona/percona-server:5.7
9db503852747bc1603ab59455124663e8cedf708ac6d992cff9b43e2fbebd167
jayj@~ [537]$ mysql -h ps57
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.10-3 Percona Server (GPL), Release 3, Revision 63dafaf
Copyright (c) 2000, 2026, 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>
当镜像被本地缓存后,容器的上下线调整变得轻松快捷。整个工作流与操作系统工作站完全分离。虽然目前尚未探索出如加载数据文件或运行代码连接容器等更多用途,但未来将进一步挖掘该设置的潜力。
以上介绍了在Mac上使用Docker快速启动MySQL测试的方法,希望有所帮助。如有任何疑问,欢迎留言,小编将及时回复。感谢大家对本站的支持!