
今はMySQL 8.0.x から 8.4.x LTS の過渡期な気がする。
MySQLは、クライアントとサーバーでメジャーバージョンを揃えた方が無難で、パッチバージョンはクライアントが新しい方が良さそう。
ということでmacOSでMySQLクライアントの8.0と8.4を共存させてバージョンを簡単に切り替える方法を調べてみた。
結論
Homebrew でインストールし、エイリアスで使い分けるのが良さそう。
確認した環境
nissy@macbook-air ~ % system_profiler SPHardwareDataType | egrep 'Model Name|Memory|Chip'
Model Name: MacBook Air
Chip: Apple M2
Memory: 16 GB
nissy@macbook-air ~ % sw_vers
ProductName: macOS
ProductVersion: 26.1
BuildVersion: 25B78
asdfかmiseを使えばいいのでは?
まず、PythonやNode.jsのバージョン管理のように asdf や mise で手軽に切り替えられれば、と思ったけど、残念な現実に直面した。
nissy@macbook-air ~ % asdf plugin add mysql
nissy@macbook-air ~ % asdf list all mysql | tail
8.0.30
8.0.31
8.0.31
8.0.32
8.0.32
8.0.33
8.0.33
8.0.34
8.0.34
nissy@macbook-air ~ % mise list-remote mysql | tail
8.0.18
8.0.19
8.0.25
8.0.26
8.0.27
8.0.28
8.0.30
8.0.31
8.0.32
8.0.33
8.0.34と8.0.33までしかない。
asdf-pluginsのプラグインリストから辿ると、iroddis/asdf-mysql にこんなメッセージが…
Deprecation
asdf-mysql is looking for a maintainer. 8.0.33 is the last supported MySQL version.
8.0.34が表示されたのはよく分からないけど、アップストリームの dbdeployer がメンテナンスを終了していて、それに依存する asdf-mysql も 8.0.33 でメンテナンス終了。
mise-mysql は iroddis/asdf-mysql からフォークしているので、こっちも 8.0.33 で終了…
Homebrewを使えばいいのでは?
macOSのパッケージマネージャーの Homebrew は、HOGE@VERSIONで複数バージョンをインストールできるから、これでいけるのでは?と。
確認、8.0と8.4がある。
nissy@macbook-air ~ % brew search mysql-client
==> Formulae
mysql-client mysql-client@8.0 mysql-client@8.4
==> Casks
mysql-shell
バージョンを指定しないと9.5.0、指定すると8.0.44と8.4.7がインストールできるようだ。
nissy@macbook-air ~ % brew info mysql-client | head -n 1
==> mysql-client: stable 9.5.0 (bottled) [keg-only]
nissy@macbook-air ~ % brew info mysql-client@8.0 | head -n 1
==> mysql-client@8.0: stable 8.0.44 (bottled) [keg-only]
nissy@macbook-air ~ % brew info mysql-client@8.4 | head -n 1
==> mysql-client@8.4: stable 8.4.7 (bottled) [keg-only]
keg-only
brew install したのに mysql コマンドが見つからないなーと思ったら、後で気付いたけど keg-only と書いてある。
この意味は brew info mysql-client を実行すると表示される以下のとおり。
mysql-client is keg-only, which means it was not symlinked into /opt/homebrew, because it conflicts with mysql (which contains client libraries).
公式FAQにも書いてあった。
What does “keg-only” mean?
It means the formula is installed only into the Cellar and is not linked into the default prefix. This means most tools will not find it. You can see why a formula was installed as keg-only, and instructions for including it in your PATH, by running brew info
. You can modify a tool’s build configuration to find keg-only dependencies. Or, you can link in the formula if you need to with brew link
, though this can cause unexpected behaviour if you are shadowing macOS software.
シンボリックリンクを作成しないのもPATHに追加しないのも、コンフリクトを起こさないようにインストールするだけ、というのが keg-only らしい。
keg-only でインストールしたものに手動で alias を追加すれば良さそうだぞ、と。
具体的な設定手順
- Homebrewで2つのバージョンをインストール
nissy@macbook-air ~ % brew install mysql-client@8.0 mysql-client@8.4 - シェルの設定ファイルにエイリアスを追記
zshなら~/.zshrc、bashやfishを使っているならそれぞれに対応するファイルを編集する
開いたファイルに以下を追記、場所は一番下が無難nissy@macbook-air ~ % vi ~/.zshrcalias mysql80="$(brew --prefix mysql-client@8.0)/bin/mysql" alias mysql84="$(brew --prefix mysql-client@8.4)/bin/mysql" # (オプション)他のコマンドも使う場合は同様に追加 alias mysqladmin80="$(brew --prefix mysql-client@8.0)/bin/mysqladmin" alias mysqladmin84="$(brew --prefix mysql-client@8.4)/bin/mysqladmin"$(brew --prefix)は、Apple Silicon Mac なら/opt/homebrewを、Intel Mac なら/usr/localを返してくれるけど、もちろん直接パスを書いてもいい。 - 設定をターミナルに反映
ファイルを保存したら、以下のコマンドでシェルを読み直す
exec $SHELL -l - バージョン確認
nissy@macbook-air ~ % mysql80 --version /opt/homebrew/opt/mysql-client@8.0/bin/mysql Ver 8.0.44 for macos26.0 on arm64 (Homebrew) nissy@macbook-air ~ % mysql84 --version /opt/homebrew/opt/mysql-client@8.4/bin/mysql Ver 8.4.7 for macos26.0 on arm64 (Homebrew)
8.0 と 8.4 の mysql コマンドを共存させて使い分けられるようになった。