1) When installing the mysql2 gem on newer macOS, there is an error:

An error occurred while installing mysql2 (0.5.6), and Bundler cannot continue.

2) First, make sure the xcode-select is installed.

$ xcode-select --install
xcode-select: note: Command line tools are already installed. Use "Software Update" in System Settings or the softwareupdate command line interface to install updates

3) Make sure the required tools are up-to-date.

$ brew install zstd openssl

4) Prepare the paths.

export LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@3/include"
export LIBRARY_PATH=$LIBRARY_PATH:$(brew --prefix zstd)/lib/

5) Install the gem with 2 additional settings, like below:

$ gem install mysql2 -v '0.5.6' -- --with-opt-dir=$(brew --prefix openssl) --with-ldflags=-L$(brew --prefix zstd)

Adjust the version number as needed.