あじちゃんの備忘録。

〜ここはメモ帳です

MySQLのReInstall

HomeBrewで入れたMySQLのrootパスワードを忘れたので入れ直すことにした……

コマンドリスト1〜順に実行していく

  1. brew remove mysql
  2. brew cleanup
  3. ~/Library/LaunchAgents/com.mysql.mysqld.plist の有無を確認
  4. 3.があった場合 remove する
  5. sudo rm -rf /usr/local/var/mysql
  6. brew install mysql
  7. unset TMPDIR (TMPDIR環境変数の解除)
  8. mkdir -p ~/Library/LaunchAgents
  9. cp /usr/local/Cellar/mysql/5.7.22/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/ (パスのバージョンはinstallした時に表示されたものを指定すること)
  10. launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
  11. mysql_secure_installation (セキュリティ関連セットアップ)--> YES/NO形式で答えていく
    1. root ユーザーのパスワードの変更
    2. VALIDATE PASSWORD プラグインのインストール
    3. ポリシーに沿った root ユーザーパスワードの設定 (VALIDATE PASSWORD プラグインをインストールした場合)
    4. anonymous ユーザーの削除
    5. リモートホストから root ユーザーでログインするのを禁止する
    6. testデータベースの削除 (存在する場合)
  12. 設定ファイルによる設定(my.cnf)

6.のinstall結果は以下のようになる(現在の最新は5.7.22)

$ brew install mysql
==> Installing dependencies for mysql: openssl
==> Installing mysql dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local,
because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries.

If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile

For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB
==> Installing mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.22.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.7.22.high_sierra.bottle.tar.gz
==> /usr/local/Cellar/mysql/5.7.22/bin/mysqld --initialize-insecure --user=asahinakamura --basedir=/usr/local/Cellar/mysql/5.7.22 --datadir=/usr/local/var/mysql --tmpdir=/tmp
==> Caveats
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 have launchd start mysql now and restart at login:
  brew services start mysql
Or, if you don't want/need a background service you can just run:
  mysql.server start
==> Summary
🍺  /usr/local/Cellar/mysql/5.7.22: 317 files, 234.2MB

12.設定ファイルは左から順に読み込まれる

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

▷一つずつみていくと /usr/local/etc/my.cnf だけファイルが有ったのでこれに以下の設定を追記

# デフォルトの文字コード
character-set-server = utf8

# ユーザーのパスワードの有効期限(0:無効)
default_password_lifetime = 0

▷default_password_lifetimeを無効にするのは、MySQL5.7からユーザパスワードの有効期限がデフォルトで360日になったために360日を過ぎてログインできなくなるのを防ぐための処置です

最後に

起動の確認。

$ mysql -u root -p

以上です。