Homebrewでよく使うコマンド

homebrew自体のupdate, formulaeリストの更新

% brew update

インストールされているformulaeの一覧

% brew list
% brew list --versions

インストール可能なformulaeの取得

% brew search <formulae名>
% brew search mysql
==> Formulae
automysqlbackup               mysql++                       mysql-client@5.7              mysql-sandbox                 mysql@5.6                     mysqltuner
mysql ✔                       mysql-client                  mysql-connector-c++           mysql-search-replace          mysql@5.7                     qt-mysql

チェックマークが入っているものはインストール済み。 思ったよりインストールできるバージョンが少ないですね、細かくバージョン指定したい場合は直接ダウンロードする感じかな。

formulaeの詳細情報

% brew info <formulae名>
% brew info mysql
mysql: stable 8.0.28 (bottled)
Open source relational database management system
https://dev.mysql.com/doc/refman/8.0/en/
Conflicts with:
  mariadb (because mysql, mariadb, and percona install the same binaries)
  percona-server (because mysql, mariadb, and percona install the same binaries)
/usr/local/Cellar/mysql/8.0.25 (301 files, 301.5MB) *
  Poured from bottle on 2021-05-20 at 01:52:38
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql.rb
License: GPL-2.0-only with Universal-FOSS-exception-1.0
==> Dependencies
Build: cmake ✘, pkg-config ✔
Required: icu4c ✔, libevent ✔, libfido2 ✘, lz4 ✔, openssl@1.1 ✔, protobuf ✘, zstd ✔
....

formulaeであるmysqlは最新が8.0.28で、今インストールされているのは8.0.25なので、アップデートできる。

% brew upgrade mysql
# インストール済みのformulaeを全部最新にしたい場合
% brew upgrade

インストールされていない場合は、Not installedと出る。

% brew info mysql@5.7
mysql@5.7: stable 5.7.37 (bottled) [keg-only]
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Not installed
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql@5.7.rb
License: GPL-2.0-only
==> Dependencies
Build: cmake ✘
Required: openssl@1.1 ✔
...

brew infoインストール済みのパッケージの情報ではなく、現在のバージョンのhomebrewが管理できる全formulaeに関する情報を取得できる。
つまりこれ。https://formulae.brew.sh/formula/
今pcにインストールされているhomebrewが、この最新のformulaeのリストを参照するようにbrew updateをまずするのが基本だと思うが、今は、brew installすると、

Running `brew update --preinstall`...

と最初に出るので勝手にやってくれるみたい。

バージョン切り替え

最新版から(8.0系)から5.7系のmysqlに切り替えたいとする。一旦現在のformulaeをunlinkしてからlinkを張る。

% brew install mysql@5.7
% brew unlink mysql && brew link mysql@5.7 --force

使われてないformulaeの削除

% brew cleanup

Remove stale lock files and outdated downloads for all formulae and casks, and remove old versions of installed formulae. If arguments are specified, only do this for the given formulae and casks. Removes all downloads more than 120 days old. This can be adjusted with HOMEBREW_CLEANUP_MAX_AGE_DAYS.

120日以上の前のダウンロードが対象みたい。--dry-runができるので確認してから消す方が良さそう。