本文主要討論使用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 test90005b3ffa9fef1f817ee4965e794a567404c9a8d5bf07320514e7d848d59ff9jayj@~ [511]$ docker run --name=mysql57 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql/mysql-server:5.76c80fa89610dbd5418ba474ad7d5451cd061f80a8a72ff2e718341827a08144bjayj@~ [512]$ docker run -it --rm --net=test -e MYSQL_HOST=mysql57 mysql/shell initCreating a Classic Session to root@mysql57:3306Enter 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 mysql57Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 4Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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=nodeCreating a Node Session to root@mysql57:33060Enter password:No default schema selected.Welcome to MySQL Shell 1.0.3 Development PreviewCopyright (c) 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help', 'h' or '?' for help.Currently in JavaScript mode. Use sql to switch to SQL mode and execute queries.mysql-js> sqlSwitching 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.5Unable to find image 'mysql/mysql-server:5.5' locally5.5: Pulling from mysql/mysql-servera3ed95caeb02: Already existsffe36b360c6d: Already exists646f220a8b5d: Pull completeed65e4fea7ed: Pull completed34b408b18dd: Pull completeDigest: sha256:12f0b7025d1dc0e7b40fc6c2172106cdf73b8832f2f910ad36d65228d9e4c433Status: Downloaded newer image for mysql/mysql-server:5.56691dd9d42c73f53baf2968bcca92b7f4d26f54bb01d967be475193305affd4fjayj@~ [531]$ mysql -h mysql55Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 1Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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.7Unable to find image 'percona/percona-server:5.7' locally5.7: Pulling from percona/percona-servera3ed95caeb02: Pull completea07226856d92: Pull completeeee62d87a612: Pull complete4c6755120a98: Pull complete10eab0da5972: Pull completed5159a6502a4: Pull completee595a1a01d00: Pull completeDigest: sha256:d57f0ce736f5403b1714ff8d1d6b91d5a7ee7271f30222c2bc2c5cad4b4e6950Status: Downloaded newer image for percona/percona-server:5.79db503852747bc1603ab59455124663e8cedf708ac6d992cff9b43e2fbebd167jayj@~ [537]$ mysql -h ps57Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 2Server version: 5.7.10-3 Percona Server (GPL), Release 3, Revision 63dafafCopyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql>
所以,這一切都很好,一旦鏡像被本機快取,上下調整新的容器可以實現無痛和快速。所有這一切的工作都是與我的作業系統工作站分離的。可能還有其他事情可以使用這種設定,但還沒找到方法(如載入資料檔案、運行代碼來串連這些容器等),但將來我會解決。
以上所述是小編給大家介紹的Mac上使用Docker快速啟動MySQL測試的方法,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!