macOS に Homebrew でインストールした MySQL 5.7 と MySQL 8.0 を同居させる

複数バージョンを共存させなければ Homebrew がよしなにインストールしてくれて brew services start mysql で終わりなのですが、複数バージョンを同時に起動する場合にはパスやポートを分けて管理する必要があります
本記事ではそれぞれのバージョンの MySQL をインストールしながら仕分けをおこない、自動起動の設定までを実施します

インストール

僕の環境では Docker で起動した MySQL への接続に利用していた mysql-client のみインストールされていました
MySQL のインストール時に mysql-client もインストールされると思いますが必要であれば別途セットアップしてください

MySQL 5.7 のインストール

まずはごく普通に brew install します

❯❯❯ brew list | grep mysql
mysql-client
❯❯❯ brew install mysql@5.7
# 中略
❯❯❯

設定ファイルとデータディレクトリの存在を確認します
バージョンに関わらず、次のパスにインストールされます

❯❯❯ ls -d /opt/homebrew/var/mysql
/opt/homebrew/var/mysql
❯❯❯ ls /opt/homebrew/etc/my.cnf
/opt/homebrew/etc/my.cnf
❯❯❯

後ほどインストールする MySQL 8.0 と競合しないようパスを変更します

❯❯❯ mv /opt/homebrew/var/mysql /opt/homebrew/var/mysql@5.7
❯❯❯ ls -d /opt/homebrew/var/mysql@5.7
/opt/homebrew/var/mysql@5.7
❯❯❯ mv /opt/homebrew/etc/my.cnf /opt/homebrew/etc/my@5.7.cnf
❯❯❯ ls /opt/homebrew/etc/my@5.7.cnf
/opt/homebrew/etc/my@5.7.cnf
❯❯❯

設定ファイルを編集します
複数起動した際に競合する箇所を変更します

❯❯❯ vi /opt/homebrew/etc/my@5.7.cnf

パスやポートの編集項目を下記に抜粋しました
ご自身の環境に合わせ、必要に応じて設定ファイルへ追記してください

[mysqld]
port=3307
socket=/tmp/mysql@5.7.sock
pid-file=/opt/homebrew/var/mysql@5.7/mysql.local.pid
log-error=/opt/homebrew/var/mysql@5.7/mysql.local.err
datadir=/opt/homebrew/var/mysql@5.7

MySQL 5.7 を手動で起動します

❯❯❯ /opt/homebrew/opt/mysql@5.7/bin/mysqld_safe --defaults-file=/opt/homebrew/etc/my@5.7.cnf
2021-12-02T06:23:46.6NZ mysqld_safe Logging to '/opt/homebrew/var/mysql@5.7/mysql.local.err'.
2021-12-02T06:23:47.6NZ mysqld_safe Starting mysqld daemon with databases from /opt/homebrew/var/mysql@5.7

無事起動したら別のTerminalからログインしてみます

❯❯❯ mysql -h 127.0.0.1 -P 3307 -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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>

無事にログインできたのでシャットダウンしておきます

❯❯❯ /opt/homebrew/opt/mysql@5.7/bin/mysqladmin shutdown -P 3307 -S /tmp/mysql@5.7.sock -u root

MySQL 8.0 のインストール

同様に、設定を変えながら MySQL 8.0 をインストールします

❯❯❯ brew list | grep mysql
mysql-client
mysql@5.7
❯❯❯ brew install mysql@8.0
# 中略
❯❯❯

設定ファイルとデータディレクトリの存在を確認します

❯❯❯ ls -d /opt/homebrew/var/mysql
/opt/homebrew/var/mysql
❯❯❯ ls /opt/homebrew/etc/my.cnf
/opt/homebrew/etc/my.cnf
❯❯❯

MySQL 8.0 も同様にパスを変更します

❯❯❯ mv /opt/homebrew/var/mysql /opt/homebrew/var/mysql@8.0
❯❯❯ ls -d /opt/homebrew/var/mysql@8.0
/opt/homebrew/var/mysql@8.0
❯❯❯ mv /opt/homebrew/etc/my.cnf /opt/homebrew/etc/my@8.0.cnf
❯❯❯ ls /opt/homebrew/etc/my@8.0.cnf
/opt/homebrew/etc/my@8.0.cnf
❯❯❯

設定ファイルを編集します

vi /opt/homebrew/etc/my@8.0.cnf

MySQL 8.0 では次の内容で編集しました

[mysqld]
port=3308
socket=/tmp/mysql@8.0.sock
pid-file=/opt/homebrew/var/mysql@8.0/mysql.local.pid
log-error=/opt/homebrew/var/mysql@8.0/mysql.local.err
datadir=/opt/homebrew/var/mysql@8.0

手動で起動し動作確認します

❯❯❯ /opt/homebrew/opt/mysql@8.0/bin/mysqld_safe --defaults-file=/opt/homebrew/etc/my@8.0.cnf
2021-12-02T06:49:15.6NZ mysqld_safe Logging to '/opt/homebrew/var/mysql@8.0/mysql.local.err'.
2021-12-02T06:49:15.6NZ mysqld_safe Starting mysqld daemon with databases from /opt/homebrew/var/mysql@8.0

MySQL 8.0 のデフォルトでは初回起動時にパスワード生成されているのでパスワードを控えておきます

❯❯❯ grep "temporary password" /opt/homebrew/var/mysql@8.0/mysql.local.err

控えたパスワードを使ってログインしてみます

❯❯❯ mysql -h 127.0.0.1 -P 3308 -uroot -ppassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27 Homebrew

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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> 

無事にインストールが完了したので MySQL 8.0 もシャットダウンします

❯❯❯ /opt/homebrew/opt/mysql@8.0/bin/mysqladmin shutdown -P 3308 -S /tmp/mysql@8.0.sock -uroot -ppassword

macOS で自動起動の設定をする

launchctl というサービスを使って自動起動の設定をします
launchctl はデーモンの管理のほか、crontab のようなスケジューラも設定できて便利なので気になった方は調べてみてください

MySQL 5.7 を起動する

launchctl に登録する設定ファイルを用意します

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>mysql@5.7</string>
  <key>ProgramArguments</key>
  <array>
    <string>/opt/homebrew/opt/mysql@5.7/bin/mysqld_safe</string>
    <string>--defaults-file=/opt/homebrew/etc/my@5.7.cnf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/opt/homebrew/var/mysql@5.7</string>
</dict>
</plist>

設定ファイルを launchctl で load するとデーモンが起動します
パスは適宜読み替えてください

❯❯❯ launchctl load path/to/mysql@5.7.plist

ログインできたら MySQL 5.7 のセットアップは無事完了です

❯❯❯ mysql -h 127.0.0.1 -P 3307 -uroot

MySQL 8.0 を起動する

launchctl に登録する設定ファイルを用意します

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>mysql@8.0</string>
  <key>ProgramArguments</key>
  <array>
    <string>/opt/homebrew/opt/mysql@8.0/bin/mysqld_safe</string>
    <string>--defaults-file=/opt/homebrew/etc/my@8.0.cnf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>WorkingDirectory</key>
  <string>/opt/homebrew/var/mysql@8.0</string>
</dict>
</plist>

launchctl で設定ファイルを load してデーモンを起動します

❯❯❯ launchctl load path/to/mysql@8.0.plist

ログインできたら MySQL 8.0 のセットアップも無事完了です

❯❯❯ mysql -h 127.0.0.1 -P 3308 -uroot -ppassword

以上で複数バージョンの MySQL の同居のセットアップが完了しました
launchctl で設定したので OS へログインした時に自動で起動するはずです

複数起動するのであれば Docker で立ち上げれば楽ですが、macOS での Docker のボリュームのパフォーマンスに少し不安がある人は試してみても良いかと思います


この記事が気に入ったらサポートをしてみませんか?