How to quickly launch MySQL tests using Docker on Mac

  • 2020-05-17 07:01:33
  • OfStack

This article focuses on ways to quickly launch MySQL tests using Docker, including the Mac environment. 1 take a look!

Recently there has been a lot of talk in the industry about Docker, whose ecosystem is growing rapidly. However, it is easy to find mature technologies in simple "get started" or "bootstrap" articles, but Docker is not. I tried Docker on Mac, but Mac is definitely a class 2 citizen of Docker. When I read about the new Docker beta Docker for Mac beta and MySQL on Giuseppe's blog, I decided to give it a try myself. These steps apply to Mac (Windows is possible) as well as to the Linux environment (GA version, Docker 1.11.1).

First, register the new Docker beta program on Mac, and then download the code from Docker. It took me a day to do this, but the full version should be out soon. Once the installation is complete, I need to set up some Docker containers for the common MySQL version and the sandbox will be there. The method is as follows:


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

1 some lessons learned:

I created a network for my container called "test" to share, essentially a private network between containers. I like this because you are listening to multiple containers on the relevant port, and you don't have to set the port of the host operating system.
I started an image of MySQL 5.7 from Oracle's official MySQL Docker container before binding to the test network.

I used the MySQL /shell image (from Oracle) to initialize the mysqlx plug-in on the MySQL 5.7 server. It is important to note that I did not enter the password because I did not create a server (it is not secure, but it is a sandbox).
The Shell in this one USES a temporary container that was deleted after the run, so it does not break the Docker ps-a output.

So now I want to be able to access this container using the standard MySQL command line or the new MySQL shell. To make it look 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'

After these, I can call them directly and connect to my MySQL 5.7 image via normal command-line options, as if using a native MySQL CLI binary. Use MySQL CLI from MySQL 5.7 mirror:


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)

Using 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>

Now, if you want to run MySQL 5.5 for 1 thing, you can do this:


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)
 Or, 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>

So, the 1 slice is good, the 1 dener image is locally cached, and adjusting the new container up and down can be painless and fast. All of this work is separated from my operating system workstation. There may be other things you can do with this setup that you haven't found a way to do (loading data files, running code to connect to these containers, etc.), but I'll fix that in the future.


Related articles: