神刀安全网

Quick start MySQL testing using Docker (on a Mac!)

Quick start MySQL testing using Docker (on a Mac!) In this post, we’ll discuss how you can quick start MySQL testing using Docker, specifically in a Mac environment.

Like a lot of people, I’m hearing a lot about Docker and it’s got me curious. The Docker ecosystem seems to be moving quickly, however, and simple “getting started” or “how-to” type articles that are easy to find for well-established technologies seem to be out-of-date or non-existent for Docker. I’ve been playing with Docker on Mac for a bit, but it is definitely a second-class citizen in the Docker world. However, I saw Giuseppe’s blog on the new Docker beta for Mac and decided to try it for myself. These steps work for the beta version on a Mac (and probably Windows), but they should work with Linux as well (using the GA release, currently Docker 1.11.1).

The new Docker beta for Mac requires that you register for the beta program, and receive a download code from Docker. I got mine in about a day, but I would assume it won’t be long before the full version is released.

Once installed, I needed to setup some Docker containers for common MySQL versions so that I can easily have some sandboxes. The method I used is below:

jayj@~ [510]$ dockernetworkcreatetest 90005b3ffa9fef1f817ee4965e794a567404c9a8d5bf07320514e7d848d59ff9 jayj@~ [511]$ dockerrun --name=mysql57 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql/mysql-server:5.7 6c80fa89610dbd5418ba474ad7d5451cd061f80a8a72ff2e718341827a08144b jayj@~ [512]$ dockerrun -it --rm --net=test -e MYSQL_HOST=mysql57mysql/shellinit Creating a ClassicSessionto root@mysql57:3306 Enterpassword: Nodefault schemaselected.   enableXProtocol: Installingpluginmysqlx... enableXProtocol: done 

A quick summary of what I did above:

  1. I created a network called “test” for my containers to share, essentially this is a dedicated private network between containers.  I like this because multiple containers can listen on the same port and I don’t have to fight with ports on my host OS.
  2. I started a MySQL 5.7 image from Oracle’s official MySQL Docker container bound to that test network.
  3. I used the MySQL/shell image (also from Oracle) to initialize the mysqlx plugin on my 5.7 server. Notice I didn’t enter a password because I created the server without one (insecure, but it’s a sandbox).

The shell init uses a temporary container that is removed (–rm) after the run, so you don’t pollute your docker ps a a output.

So, now I want to be able to use the standard MySQL command line and/or the new MySQL shell to access this container.  To  make this really clean, I added some bash aliases:

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' 

With these in effect, I can call them directly and pass normal command line options to connect to my mysql57 image just as if I was using a native MySQL CLI binary.

Using the MySQL CLI from the 5.7 image:

jayj@~ [524]$ mysql -h mysql57 Welcometo theMySQLmonitor. Commandsend with ; or g. YourMySQLconnectionid is 4 Serverversion: 5.7.12 MySQLCommunityServer (GPL)   Copyright (c) 2000, 2016, Oracleand/or itsaffiliates. Allrightsreserved.   Oracleis a registeredtrademarkofOracleCorporationand/or its affiliates. Othernamesmaybetrademarksoftheirrespective owners.   Type 'help;' or 'h' for help. Type 'c' to clear thecurrentinputstatement.   mysql> showschemas; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rowsin set (0.01 sec) 

Using the MySQL shell:

jayj@~ [527]$ mysqlsh -h mysql57 -u root --session-type=node Creating a NodeSessionto root@mysql57:33060 Enterpassword: Nodefault schemaselected.   Welcometo MySQLShell 1.0.3 DevelopmentPreview   Copyright (c) 2016, Oracleand/or itsaffiliates. Allrightsreserved.   Oracleis a registeredtrademarkofOracleCorporationand/or its affiliates. Othernamesmaybetrademarksoftheirrespective owners.   Type 'help', 'h' or '?' for help.   Currentlyin JavaScriptmode. Use sqlto switch to SQLmodeand executequeries. mysql-js> sql Switchingto SQLmode... Commandsend with ; mysql-sql> showschemas; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rowsin set (0.00 sec)   mysql-sql> 

Now if I want to run check MySQL 5.5 for something, I can just do this:

jayj@~ [530]$ dockerrun --name=mysql55 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql/mysql-server:5.5 Unableto find image 'mysql/mysql-server:5.5' locally 5.5: Pullingfrommysql/mysql-server a3ed95caeb02: Alreadyexists ffe36b360c6d: Alreadyexists 646f220a8b5d: Pullcomplete ed65e4fea7ed: Pullcomplete d34b408b18dd: Pullcomplete Digest: sha256:12f0b7025d1dc0e7b40fc6c2172106cdf73b8832f2f910ad36d65228d9e4c433 Status: Downloadednewerimagefor mysql/mysql-server:5.5 6691dd9d42c73f53baf2968bcca92b7f4d26f54bb01d967be475193305affd4f jayj@~ [531]$ mysql -h mysql55 Welcometo theMySQLmonitor. Commandsend with ; or g. YourMySQLconnectionid is 1 Serverversion: 5.5.49 MySQLCommunityServer (GPL)   Copyright (c) 2000, 2016, Oracleand/or itsaffiliates. Allrightsreserved.   Oracleis a registeredtrademarkofOracleCorporationand/or its affiliates. Othernamesmaybetrademarksoftheirrespective owners.   Type 'help;' or 'h' for help. Type 'c' to clear thecurrentinputstatement.   mysql> showschemas; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rowsin set (0.00 sec) 

or, Percona Server:

jayj@~ [534]$ dockerrun --name=ps57 --net=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d percona/percona-server:5.7 Unableto find image 'percona/percona-server:5.7' locally 5.7: Pullingfrompercona/percona-server a3ed95caeb02: Pullcomplete a07226856d92: Pullcomplete eee62d87a612: Pullcomplete 4c6755120a98: Pullcomplete 10eab0da5972: Pullcomplete d5159a6502a4: Pullcomplete e595a1a01d00: Pullcomplete Digest: sha256:d57f0ce736f5403b1714ff8d1d6b91d5a7ee7271f30222c2bc2c5cad4b4e6950 Status: Downloadednewerimagefor percona/percona-server:5.7 9db503852747bc1603ab59455124663e8cedf708ac6d992cff9b43e2fbebd167 jayj@~ [537]$ mysql -h ps57 Welcometo theMySQLmonitor. Commandsend with ; or g. YourMySQLconnectionid is 2 Serverversion: 5.7.10-3 PerconaServer (GPL), Release 3, Revision 63dafaf   Copyright (c) 2000, 2016, Oracleand/or itsaffiliates. Allrightsreserved.   Oracleis a registeredtrademarkofOracleCorporationand/or its affiliates. Othernamesmaybetrademarksoftheirrespective owners.   Type 'help;' or 'h' for help. Type 'c' to clear thecurrentinputstatement.   mysql> 

So all this is nice – once the images are cached locally, spinning new containers up and down is painless and fast. All this sandbox work is cleanly separated from my workstation OS. There are probably other things I’d want to be able to do with this setup that I haven’t figured out yet (e.g., loading data files, running code to connect to these containers, etc.) – but I’ll figure those out in the future.

Related

Jay Janssen

Jay joined Percona in 2011 after 7 years at Yahoo working in a variety of fields including High Availability architectures, MySQL training, tool building, global server load balancing, multi-datacenter environments, operationalization, and monitoring. He holds a B.S. of Computer Science from Rochester Institute of Technology.

Tags:

Apple , Mac , MySQL testing using Docker

Categories:

,

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Quick start MySQL testing using Docker (on a Mac!)

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址