複数バージョンのMySQLをローカルで同時に使い分けできるdbdeployerを試してみた

ローカルでMySQLを使う時の選択肢だったMySQL-Sandboxがdeprecatedになっており、代替としてピックアップされていたdbdeployerを試してみました。
2021.06.25

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

便利に使っていたものも、気がつくと主流ではなくなっていたというのはありがちです。brew doctorを実行したところ、以下の表示がでてきました。

Warning: Some installed formulae are deprecated or disabled.
You should find replacements for the following formulae:
  mysql-sandbox

mysql-sandboxは様々なバージョンのMySQLサーバをお手軽にローカル上に建てたり、データが破損しても簡単に入れ直せるため、前職以前はローカル環境でMySQLを扱うロジックの検証が非常に快適になったライブラリでした。一応クラスメソッド入社後も念の為入れておいたのですが、直接MySQLを弄る機会が減り、結果deprecatedになったことにも気がついていなかったようです。

リポジトリを確認したところ、

This project is replaced by dbdeployer, which is now GA. It can already support all MySQL-Sandbox features, plus many new ones. This project is now archived and will not be updated.

とあったため、dbdeployerを触ってみました。

dbdeployerの導入

リポジトリwikiの導入手順には含まれていませんが、HomeBrew経由でのインストールも可能です。Formulaはreadme_templateに記載されている手順を使った処理となります。init時にbash-completionを求められるため、合わせてインストールしておきます。

dbdeployer/readme_template.md at 59ba6184e184d3ca0309289b2e3e13e480c3010f · datacharmer/dbdeployer - https://github.com/

% brew install bash-completion dbdeployer

==> Downloading https://ghcr.io/v2/homebrew/core/dbdeployer/manifests/1.62.0
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/dbdeployer/blobs/sha256:d96b7c9c43b6e46cdb82c43e834056f17ef53807d3d0ad67c109d76a0a158176
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:d96b7c9c43b6e46cdb82c43e834056f17ef53807d3d0ad67c109d76a0a158176?se
######################################################################## 100.0%
==> Pouring dbdeployer--1.62.0.big_sur.bottle.tar.gz
?  /usr/local/Cellar/dbdeployer/1.62.0: 7 files, 11.7MB

dpdeployerを使ってみる

MySQL-Sandboxと大きく異なるのは、初回セットアップ時にはMySQLのアーカイブを自動でダウンロードしてくれる点です。

% dbdeployer init
SANDBOX_BINARY /Users/xxxxxxx/opt/mysql
SANDBOX_HOME   /Users/xxxxxxx/sandboxes

--------------------------------------------------------------------------------
Directory /Users/xxxxxxx/opt/mysql ($SANDBOX_BINARY) already exists
This directory is the destination for expanded tarballs

--------------------------------------------------------------------------------
Directory /Users/xxxxxxx/sandboxes ($SANDBOX_HOME) already exists
This directory is the destination for deployed sandboxes

--------------------------------------------------------------------------------
# dbdeployer defaults enable-bash-completion --run-it --remote
  94 kB
Download of file dbdeployer_completion.sh was successful
# completion file: dbdeployer_completion.sh
Files 'dbdeployer_completion.sh' and '/usr/local/etc/bash_completion.d/dbdeployer_completion.sh' have the same checksum - Copy is not needed

初回ダウンロードしたバージョンでやってみることにします。

% dbdeployer versions
Basedir: /Users/xxxxxxx/opt/mysql
8.0.25

% dbdeployer deploy single 8.0.25
Database installed in $HOME/sandboxes/msb_8_0_25
run 'dbdeployer usage single' for basic instructions'
... sandbox server started

バージョン番号は短縮系も利用可能です。この場合はマイナーバージョンのうち最新版が使われます。既に同等のバージョンが導入されている場合は以下のエラーとなります。

% dbdeployer deploy single 8.0
# 8.0 => 8.0.25
error creating sandbox: 'check directory directory /Users/xxxxxxx/sandboxes/msb_8_0_25 already exists. Use --force to override'

そこで--forceフラグにて回避します。

% dbdeployer deploy single 8.0 --force
# 8.0 => 8.0.25
Overwriting directory /Users/xxxxxxx/sandboxes/msb_8_0_25
stop /Users/xxxxxxx/sandboxes/msb_8_0_25
Database installed in $HOME/sandboxes/msb_8_0_25
run 'dbdeployer usage single' for basic instructions'
.. sandbox server started

異なるバージョンを選択したい場合は追加ダウンロードします。

MySQL-Sandboxはアーカイブを毎回配布サーバから実際に使えるかどうか分からないながらダウンロードし、かつ手作業で解凍する必要がありました。dbdeployerは有効なバージョンの提示(list)とダウンロード(downloads)、解凍(get-unpack)まで一通り担当してくれます。

% dbdeployer downloads list
Available tarballs  ()
name                            OS     version     flavor        size   minimal
--------------------------------------------------- -------- --------- ------------- -------- ---------
mysql-5.0.96-osx10.5-x86_64.tar.gz                  Darwin    5.0.96   mysql          61 MB
mysql-5.1.73-osx10.6-x86_64.tar.gz                  Darwin    5.1.73   mysql          82 MB
mysql-5.5.53-osx10.9-x86_64.tar.gz                  Darwin    5.5.53   mysql         114 MB
mysql-5.6.41-macos10.13-x86_64.tar.gz               Darwin    5.6.41   mysql         176 MB
mysql-5.7.25-macos10.14-x86_64.tar.gz               Darwin    5.7.25   mysql         337 MB
mysql-5.7.26-macos10.14-x86_64.tar.gz               Darwin    5.7.26   mysql         337 MB
mysql-5.7.27-macos10.14-x86_64.tar.gz               Darwin    5.7.27   mysql         337 MB
mysql-5.7.28-macos10.14-x86_64.tar.gz               Darwin    5.7.28   mysql         374 MB
mysql-5.7.29-macos10.14-x86_64.tar.gz               Darwin    5.7.29   mysql         361 MB
mysql-5.7.30-macos10.14-x86_64.tar.gz               Darwin    5.7.30   mysql         360 MB
mysql-5.7.31-macos10.14-x86_64.tar.gz               Darwin    5.7.31   mysql         225 MB
mysql-8.0.15-macos10.14-x86_64.tar.gz               Darwin    8.0.15   mysql         139 MB
mysql-8.0.16-macos10.14-x86_64.tar.gz               Darwin    8.0.16   mysql         153 MB
mysql-8.0.17-macos10.14-x86_64.tar.gz               Darwin    8.0.17   mysql         155 MB
mysql-8.0.18-macos10.14-x86_64.tar.gz               Darwin    8.0.18   mysql         166 MB
mysql-8.0.19-macos10.15-x86_64.tar.gz               Darwin    8.0.19   mysql         167 MB
mysql-8.0.20-macos10.15-x86_64.tar.gz               Darwin    8.0.20   mysql         166 MB
mysql-8.0.21-macos10.15-x86_64.tar.gz               Darwin    8.0.21   mysql         122 MB
mysql-8.0.22-macos10.15-x86_64.tar.gz               darwin    8.0.22   mysql         168 MB
mysql-8.0.24-macos11-x86_64.tar.gz                  darwin    8.0.24   mysql         169 MB
mysql-8.0.25-macos11-x86_64.tar.gz                  Darwin    8.0.25   mysql         169 MB
mysql-cluster-8.0.16-dmr-macos10.14-x86_64.tar.gz   Darwin    8.0.16   ndb           252 MB
mysql-cluster-8.0.17-rc-macos10.14-x86_64.tar.gz    Darwin    8.0.17   ndb           255 MB
mysql-cluster-8.0.19-macos10.15-x86_64.tar.gz       Darwin    8.0.19   ndb           268 MB
mysql-cluster-8.0.20-macos10.15-x86_64.tar.gz       Darwin    8.0.20   ndb           273 MB
mysql-cluster-8.0.22-macos10.15-x86_64.tar.gz       darwin    8.0.22   ndb           279 MB
mysql-cluster-8.0.25-macos11-x86_64.tar.gz          Darwin    8.0.25   ndb           264 MB
mysql-cluster-gpl-7.6.10-macos10.14-x86_64.tar.gz   Darwin    7.6.10   ndb           482 MB
mysql-cluster-gpl-7.6.11-macos10.14-x86_64.tar.gz   Darwin    7.6.11   ndb           482 MB
mysql-shell-8.0.17-macos10.14-x86-64bit.tar.gz      Darwin    8.0.17   mysql-shell    17 MB
mysql-shell-8.0.21-macos10.15-x86-64bit.tar.gz      Darwin    8.0.21   shell          37 MB
mysql-shell-8.0.22-macos10.15-x86-64bit.tar.gz      darwin    8.0.22   shell          38 MB
mysql-shell-8.0.25-macos11-x86-64bit.tar.gz         Darwin    8.0.25   shell          38 MB
tidb-master-darwin-amd64.tar.gz                     Darwin     3.0.0   tidb           26 MB

# 8.0.22指定でダウンロードする場合
% dbdeployer downloads get-by-version 8.0.22
# listの結果を元に取得する場合 
% dbdeployer downloads get mysql-8.0.22-macos10.15-x86_64.tar.gz

Downloading mysql-8.0.22-macos10.15-x86_64.tar.gz
.........105 MB......  168 MB
File /Users/xxxxxxx/path/to/mysql-8.0.22-macos10.15-x86_64.tar.gz downloaded
Checksum matches

# ダウンロードと解凍を同時にする場合はファイル名直指定
% dbdeployer downloads get-unpack mysql-8.0.22-macos10.15-x86_64.tar.gz
Downloading mysql-8.0.22-macos10.15-x86_64.tar.gz
.........105 MB......  168 MB
File /Users/xxxxxxx/path/to/mysql-8.0.22-macos10.15-x86_64.tar.gz downloaded
Checksum matches
Unpacking tarball mysql-8.0.22-macos10.15-x86_64.tar.gz to $HOME/opt/mysql/8.0.22
.........100.........200....243
Renaming directory /Users/xxxxxxx/opt/mysql/mysql-8.0.22-macos10.15-x86_64 to /Users/xxxxxxx/opt/mysql/8.0.22

# サーバの起動
% dbdeployer deploy single 8.0.22
Database installed in $HOME/sandboxes/msb_8_0_22
run 'dbdeployer usage single' for basic instructions'
.. sandbox server started

快適ですね。

--guess-latestフラグで最新版推測もできますが、万能ではありません。有効な最新版を取る場合は--newestにしておきましょう。

% dbdeployer downloads get-by-version 8.0 --guess-latest
Downloading mysql-8.0.26-macos11-x86_64.tar.gz
Guessed mysql-8.0.26-macos11-x86_64.tar.gz file not ready for download : [DownloadFile] received code 404
% dbdeployer downloads get-by-version 8.0 --newest
file '/Users/xxxxxxx/path/to/mysql-8.0.25-macos11-x86_64.tar.gz' already exists

deploy後、存在する環境を確認してuseを実行します。

% dbdeployer sandboxes --header
            name               type    version       port
---------------------------- -------- --------- ---------------
 msb_8_0_22               :   single   8.0.22    [8022 18022 ]
 msb_8_0_25               :   single   8.0.25    [8025 18026 ]
% $HOME/sandboxes/msb_8_0_22/use

UNIX Socketを取得する場合はstatusを実行します。

% $HOME/sandboxes/msb_8_0_22/use
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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 [localhost:8022] {msandbox} ((none)) > status
--------------
/Users/xxxxxxx/opt/mysql/8.0.22/bin/mysql  Ver 8.0.22 for macos10.15 on x86_64 (MySQL Community Server - GPL)

Connection id:        10
Current database:
Current user:        msandbox@localhost
SSL:            Not in use
Current pager:        stdout
Using outfile:        ''
Using delimiter:    ;
Server version:        8.0.22 MySQL Community Server - GPL
Protocol version:    10
Connection:        Localhost via UNIX socket
Server characterset:    utf8mb4
Db     characterset:    utf8mb4
Client characterset:    utf8mb4
Conn.  characterset:    utf8mb4
UNIX socket:        /var/folders/pw/281km8lx4wn21ryf4y4v74h80000gq/T/mysql_sandbox8022.sock
Binary data as:        Hexadecimal
Uptime:            13 min 17 sec

Threads: 2  Questions: 47  Slow queries: 0  Opens: 146  Flush tables: 3  Open tables: 65  Queries per second avg: 0.058
--------------

あとがき

他にもレプリケーション等、MySQLを使う場合に必要な機能が一通り揃っています。

Amazon RDSを使う前にローカルで検証したいけれど、バージョンの都合で環境編集が大変な場合にはとても有用なライブラリです。見慣れなくてスルーしてたけど便利そう、と思ったら触ってみることをおすすめします。