22
16

More than 1 year has passed since last update.

M1 Mac に homebrew で MySQL 8 をインストールする

Last updated at Posted at 2022-04-13

最初は M1 Mac に Docker で MySQL を利用してみようかと思ったが、 MySQL のデータを永続化する際に権限まわりの設定が面倒くさそうだったので、homebrew で MySQL をインストールした。

  1. homebrew で MySQL をインストールする
  2. MySQL の初期設定を行う
  3. MySQL の起動と停止について
  4. MySQL の設定ファイルについて

homebrew で MySQL をインストールする

$ brew install mysql
...
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To restart mysql after an upgrade:
  brew services restart mysql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql
==> Summary
🍺  /opt/homebrew/Cellar/mysql/8.0.28_1: 304 files, 294.3MB
==> Running `brew cleanup mysql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> mysql
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To restart mysql after an upgrade:
  brew services restart mysql
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/mysql/bin/mysqld_safe --datadir=/opt/homebrew/var/mysql

MySQL の初期設定を行う

インストール時のメッセージだと、すぐに mysql_secure_installation を実行するように表示されるが、MySQL が起動していない。

まずは、MySQL を起動する。

$ $ mysql.server start
Starting MySQL
. SUCCESS!

mysql_secure_installation を実行する

$ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

パスワードの強度を設定する

パスワードの強度は、次の 3通り。

LOW
8文字以上のパスワード
MEDIUM
数字、大文字と小文字の英数字、特殊文字を含めた 8文字以上のパスワード
STRONG
数字、大文字と小文字の英数字、特殊文字を含めた 8文字以上、かつ辞書ファイルに含まれていない文字列のパスワード
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

ローカルマシンの開発環境ならば、LOW でいいのでは?

MEDIUM を選択するのならば、STRONG を選んで脆弱な文字列を省ける STRONG を選んだ方がよい。

root ユーザーのパスワードを設定する

Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100

いま入力したパスワードを登録して良いのであれば、次のメッセージでは y を入力。入力し直すのであれば、 n を入力。

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

匿名ユーザーの削除

開発環境でも、匿名ユーザーは必要とされないと思うので、 y を入力して、匿名ユーザーを削除する。

By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother.
You should remove them before moving into a production environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :

root ユーザーのリモートログインを不許可にする

開発環境でも、本番環境でも、不許可にするべきだと思うので、y を入力する。

Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

test データベースを削除する

y を入力して、利用されない test データベースを削除する。 should be removed before moving into a production environment. とも表示されているしね。

By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

特権情報の更新をする

上記の変更をすぐに反映させるために、特権情報の再読み込みを実施するために y を入力する。

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

これで完了。

MySQL の起動と停止について

MySQL の一時的な起動

$ mysql.server start

MySQL の停止

$ mysql.server stop

MySQL の自動起動

brew services start mysql

MySQL の自動起動の停止

brew services stop mysql

MySQL の設定ファイルについて

homebrew でインストールされた設定ファイルは、

$ $ find /opt/homebrew/etc -type f -name "my*.cnf"
/opt/homebrew/etc/my.cnf

で見つかる。

この設定ファイルを、設置するディレクトリは、

$ mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /opt/homebrew/etc/my.cnf ~/.my.cnf

でわかる。

22
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
16