欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Mac上使用Docker如何快速启动MySQL测试

程序员文章站 2023-11-18 22:42:52
本文主要讨论使用docker快速启动 mysql 测试的方法,包括mac环境。一起看看吧! 近来业界有很多对docker的讨论,其生态系统发展得很快,然而,从简单的“入门...

本文主要讨论使用docker快速启动 mysql 测试的方法,包括mac环境。一起看看吧!

近来业界有很多对docker的讨论,其生态系统发展得很快,然而,从简单的“入门”或“引导”类的文章中能容易地找到成熟的技术,但docker不然。我在mac上试玩过docker,但mac绝对是docker界的二等公民。当我在giuseppe的博客上看到关于在mac上使用新docker beta《docker for mac beta and mysql》一文时,决定自己来尝试下。这些步骤适用于mac(windows也可能),亦能适配linux环境(ga版本,docker 1.11.1)。

首先,在mac上注册新的docker测试版程序,接着从docker中获得下载代码。此过程我耗时一天,但应该不久就会发布完整版。安装完成后,我需要为常见的mysql版本设置一些docker容器,沙箱也就有了。方法如下:

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

一些经验总结:

我为我的容器创建了一个名为“测试”的网络以共享,本质是容器之间一个专用的私有网络。我喜欢这个是因为在相关的端口是监听多个容器,也不必设置主机操作系统的端口。
我将oracle的官方mysql docker容器启动一个mysql 5.7的镜像,在绑定到该测试网络。

我使用了mysql /shell镜像(来自oracle)来初始化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 binary。从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, 2016, 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) 2016, 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, 2016, 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, 2016, 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测试的方法,希望对大家有所帮助