4
1

More than 1 year has passed since last update.

【MySQL】Warning: The post-install step did not complete successfullyと表示されないVALIDATE PASSWORD PLUGINの解決策

Last updated at Posted at 2022-03-24

はじめに

はじめまして、Yoshiwoと申します。
初めてのMySQLの環境構築の経験から

  1. MySQLの環境構築で躓いてしまった、インストール時のWarning: The post-install step did not complete successfullyと表示されないVALIDATE PASSWORD PLUGINの解決策

  2. MySQLインストール後の動作確認とパスワードの設定

  3. MySQLの起動・再起動・停止・ログイン・ログアウトのコマンドまとめ

  4. 解決策に臨む前に試したこと

などについて執筆しました。
この記事は、備忘録、アウトプット、そして私と同じような状況で困ってしまった方への一助となることを目的としています。

1.環境

  • OS:macOS Big Sur 11.6
  • Homebrew:3.3.16

2.対象となる方

この記事の長たらしいタイトルにもありますが、ターミナルでHomebrewからMySQL5.7をインストール時にWarning: The post-install step did not complete successfullyが表示され、

ターミナル
  Warning: The post-install step did not complete successfully
  You can try again using:
    brew postinstall mysql@5.7

かつ、パスワード設定時にVALIDATE PASSWORD PLUGINが表示されずに以下の2つの文が表示された方を対象としています。

ターミナル
  Securing the MySQL server deployment.

  Enter password for user root:🔑

3.解決策の概要

1. MySQLのアンインストール
2. MySQL関連のファイル、ディレクトリ等を削除
3. MySQLの再インストール
の順となっています。

4.解決策の手順

4.1.MySQLのインストールの確認

ターミナル
  % mysql --version
  mysql  Ver 14.14 Distrib 5.7.37, for osx10.16 (x86_64)using  EditLine wrapper

% mysql --versionでMySQLのインストールの確認を行います。

4.2.MySQLをアンインストール

ターミナル
  % brew uninstall mysql@5.7

% brew uninstall mysql@5.7でMySQLのアンインストールを行います。

4.3.MySQLに関連するファイル、ディレクトリを削除

ターミナル
  % rm -rf /usr/local/var/mysql
  % rm -rf /usr/local/Cellar/mysql*
  % rm -rf /usr/local/bin/mysql*
  % rm -rf /usr/local/var/mysql*
  % rm -rf /usr/local/etc/my.cnf
  % rm -rf /usr/local/share/mysql*
  % rm -rf /usr/local/opt/mysql*
  % rm -rf /etc/my.cnf

「rm」はファイルやディレクトリを削除するコマンドです。「rm -r ディレクトリ」でディレクトリを中にあるファイルごと削除します。

アンインストールだけでは、MySQLに関するファイルが残ります。残ったファイルの中にはエラーを引き起こす物もありますのでコマンドを1つずつ打って削除を行います。

参考にさせていただいたサイトではsudoコマンドを入力しています。「sudo」は一般のユーザーがrootユーザー(管理者)権限が必要なコマンドを実行するときに使用するコマンドです。今回はsudoを省いてファイルとディレクトリを削除しました。

4.4.mysql.sockを削除

ターミナル
  % rm -rf mysql.sock mysql.sock.lock

mysql.sockはMySQLが起動すると自動で生成されるファイルのことで、「ソケットファイル」と呼ばれます。

ソケットとは「プログラムとネットワークを繋げる接続口」のことです。このソケットがなければサーバーに接続することができず、MySQL本来のデータベースとしての役割を果たすことができなくなってしまいます。

そもそもMySQLが起動できていないためmysql.sock(ファイル)は生成されていないと推測しますが、念のためコマンドを入力して実行します。

4.5.不要なプロセスを削除

ターミナル
  % ps aux | grep mysql
  31610   0.0  0.0  4296072    784 s000  S+   10:35AM   0:00.01 grep mysql
  % kill 31610

「ps」はLinux上で起動中のプロセスの一覧を表示するコマンドです。ps auxですべてのプロセスをユーザーフォーマットで表示、さらにgrepコマンドで特定のプロセス(ここでは、myspl(MySQL))を確認します。ここで言う「プロセス」とは、ひとつひとつの処理のことを指します。

ps auxで表示される内容は

  • PID(プロセスID)
  • %CPU(プロセスのCPU使用率(CPUの使用率/実際の時間))
  • %MEN(プロセスが確保している物理メモリを百分率で表示)
  • VSZ(仮想メモリ領域のサイズ)
  • RSS(物理メモリ領域のサイズ)
  • TTY(仮想端末ファイル)
  • STAT(状態)
  • START(接続を開始した時間)
  • TIME(接続した時間)
  • COMMAND(プロセスの実行コマンド)

となっています。
その中から5桁の数字で表されたPID(プロセスID)をkillコマンドで削除します。

4.6.MySQLを再インストール

ターミナル
  % brew install mysql
  Running `brew update --preinstall`...
  ==> Auto-updated Homebrew!
  Updated 1 tap (homebrew/core).
  ==> Updated Formulae
  Updated 3 formulae.

  ==> Downloading https://ghcr.io/v2/homebrew/core/mysql/manifests/8.0.28
  Already downloaded: /Users/ユーザー名/Library/Caches/Homebrew/downloads/e7b7a521d0af95379c30a39ed96f03fb17953ad5bb4e4180a719a08d696daec1--mysql-8.0.28.bottle_manifest.json
  ==> Downloading https://ghcr.io/v2/homebrew/core/mysql/blobs/sha256:fcd71d1bba2787a0e388c29eaf20d33611bb2d24c49ad0c9a0ac4d333111f33d
  Already downloaded: /Users/ユーザー名/Library/Caches/Homebrew/downloads/e6784626be51aa512ee33891e2db354c13a5df34c270c674bc995ad02418d3c4--mysql--8.0.28.big_sur.bottle.tar.gz
  ==> Pouring mysql--8.0.28.big_sur.bottle.tar.gz
  ==> /usr/local/Cellar/mysql/8.0.28/bin/mysqld --initialize-insecure --user=ユーザー名 --basedir=/usr/local/Cellar/mysql/8.0.28 --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 restart mysql after an upgrade:
    brew services restart mysql
  Or, if you don't want/need a background service you can just run:
    /usr/local/opt/mysql/bin/mysqld_safe --datadir=/usr/local/var/mysql
  ==> Summary
  🍺  /usr/local/Cellar/mysql/8.0.28: 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`).

% brew install mysqlで再インストールを行います。
※最終的にMySQL8.0.28をインストールすることとなりました。

4.7.MySQLが再インストールされたかどうかの確認

ターミナル
  % which mysql
  /usr/local/bin/mysql

% which mysqlで再インストールされたかどうか確認を行います。「which」はターミナル(コマンドライン)上で対象となるモノがどこにあるかを探す際に使用するコマンドです。上記のように/usr/local/bin/mysqlと表示されていれば、MySQLはインストールされています。

4.8.MySQLの起動とその確認

ターミナル
  % mysql.server start
  Starting MySQL
   SUCCESS!
  ユーザー名@使用している端末名 ~ % 2022-02-28T03:28:09.6NZ mysqld_safe A mysqld process already exists

% mysql.server startでMySQLを起動します。
「SUCCESS!」と表示されましたので起動の確認を行います。

ターミナル
  % mysql.server status
   SUCCESS! MySQL running (38458)

$ mysql.server statusでMySQLのサーバーの状態を確認します。
「SUCCESS! MySQL running (38458)」と表示されて起動を確認できました。

解決策は以上です。

5.MySQLインストール後の動作確認とパスワードの設定

5.1.MySQLの動作確認

ターミナル
  % mysql -u root
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 8
  Server version: 8.0.28 Homebrew

  Copyright (c) 2000, 2022, 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 -u rootで、実際にMySQLをrootユーザー(管理者)で使用できるか確認し、MySQLが正しく動作しているのかを確認します。上記のような内容が表示されれば大丈夫です。

ちなみに、ターミナル上でmysql>と表示されると通常のターミナルコマンドが入力できなくなるため、mysql>の後ろにquitまたはexitを入力してMySQLのコマンドプロンプトを終了させます。

ターミナル
  # MySQLのコマンドプロンプトを終了(無事に終了すれば Bye が表示される)。
  mysql> quit
  Bye 

5.2.パスワードの設定

ターミナル
  # MySQL起動。
  % mysql_secure_installation

  Securing the MySQL server deployment.

  Connecting to MySQL using a blank password.

  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:
  Please set the password for root here.  # ここでreturnキーを押すと、New password:とRe-enter new password: 🔑が下に表示。

パスワードの設定はMySQLが起動している時に行います。
% brew services start mysql@バージョン番号(バージョン番号が 5.7.37 なら @5.7 、 8.0.28 なら @8.0 と入力)でMySQLを起動させて、% mysql_secure_installationを実行します。

「VALIDATE PASSWORD COMPONENTを使用して、パスワードとセキュリティを向上させます。パスワードの強度をチェックします。また、ユーザーは次のようなパスワードのみを設定できます。十分に安全です。VALIDATE PASSWORDコンポーネントをセットアップしますか?(Google翻訳)」と訊かれますが、何も入力はせずにreturnキーを押します。

  New password: # パスワードを入力しても非表示。

  Re-enter new password: 🔑 # New passwordで入力したパスワードを入力。

すると上記の内容が表示されます。ここで任意のパスワードを設定します。「New password」にパスワードを入力しても画面には何も表示されませんが臆せずに一打ずつゆっくり入力してください。入力後はreturnキーを押します。

「Re-enter new password: 🔑」にも「New password」で入力したパスワードを入力します。

※設定したパスワードは忘れないように必ず控えてください。

パスワードの設定が終わると幾つか質問されますが、returnキーを押して質問を飛ばします。

ターミナル
  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) : # ここでreturnキーを押す。

  ... skipping.


  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) : # ここで returnキーを押す。

  ... skipping.
  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) : # ここでreturnキーを押す。

  ... skipping.
  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) : # ここでreturnキーを押す。

  ... skipping.
  All done! 

全ての質問を終えるとAll done!が表示されます。
私の場合は4つの質問があり、4つ全てreturnキーを押して先に進みました。

5.3.最後の動作確認

ターミナル
  # MySQL起動。
  % mysql --user=root --password
  Enter password: # ここに設定したパスワードを入力。
  Welcome to the MySQL monitor.  Commands end with ; or \g.
  Your MySQL connection id is 8
  Server version: 8.0.28 Homebrew

  Copyright (c) 2000, 2022, 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の動作の確認を行います。

まず% brew services start mysql@バージョン番号でMySQLを起動させます。次に% mysql --user=root --passwordを入力してコマンドを実行するとEnter password:が表示されるので「New password」で設定したパスワードを入力します。

パスワード入力後、mysql>が表示されていればログインは成功です。
mysql>quitまたはexitを入力してログアウトします。

6.MySQLの停止

ターミナル
  % brew services stop mysql@バージョン番号
  Stopping `mysql@バージョン番号`... (might take a while)
  ==> Successfully stopped `mysql@バージョン番号` (label: homebrew.mxcl.mysql@バージョン番号)

% brew services stop mysql@バージョン番号でMySQLを停止させます。

7.MySQLの起動・再起動・停止・ログイン・ログアウトのコマンドまとめ

7.1.MySQLの起動

  % mysql.server start
  または
  % brew services start mysql@バージョン番号

7.2.MySQLの再起動

  % mysql.server restart
  または
  % brew services start@バージョン番号 # 起動中に起動コマンドを入力することで再起動扱いされる。

7.3.MySQLの停止

  mysql.server stop
  または
  brew services stop mysql@バージョン番号

7.4.MySQLへのログイン

  mysql --user=root --password
  # コマンド入力後、設定したrootユーザーパスワードを入力。

7.5.MySQLからログアウト

  # exit または quit
  mysql > exit
  mysql > quit

※MySQLからログアウト後は必ずMySQLを停止させること。

8.解決策に臨む前に試したこと

8.1.インストール時のWarning: The post-install step did not complete successfully

ターミナル
  Warning: The post-install step did not complete successfully
  You can try again using:
    brew postinstall mysql@5.7
  # 警告:インストール後の手順は正常に完了しませんでした。次を使用して再試行できます。 brew postinstall mysql@5.7(Google 翻訳)

インストール時に表示されたこちらの内容。指示通りに% brew postinstall mysql@5.7を実行しますがWarningの表示は無くならず、MySQLのアンインストールからの再インストールでも同様の結果となりました。

8.2.パスワード設定時に表示されないVALIDATE PASSWORD PLUGIN

MySQL5.7をインストール後はMySQL専用のコマンドが使用できるようにするためのパスを設定し、次いで% mysql_secure_installationを入力するとVALIDATE PASSWORD PLUGINが表示されるはずなのですが、実際に表示されたのは以下の2行でした。

ターミナル
  Securing the MySQL server deployment.

  Enter password for user root:🔑

rootの後ろの鍵のマークにパスワードを入力する必要があるのですが、そのパスワードはMySQL5.7では初回起動と同時にランダムな文字列がパスワードとして設定されます。

設定されたパスワードはログファイル/var/log/myspld.logのログの中に表示されるので探してみますが、インストールが不十分でMySQLの起動ができていない状態なのでログファイルは存在しません。

最後に自身のmacOSを起動するためのパスワードを入力しますが状況は変わらず、作業は停滞してしまいます。

9.参考文献

この記事は以下の情報を参考にして執筆いたしました。
本当にありがとうございました。

おわりに

貴重なお時間を割いてご覧くださりありがとうございます。

「環境構築は大変で、ここを越えられずに挫折してしまう方が多くいます」と先輩エンジニアの方のコメントを目にすることがありました。挫折してしまうことのある環境構築だからこそ、これを乗り越えれば、乗り越える前の自分から一歩前進できる良い機会だと考えて取り組みました。

初めての環境構築に躓いて右往左往しながらもMySQLを使える環境を用意できたのも、また、Qiitaに記事を投稿できたのも、有益な情報を発信してくださる方々のお陰です。

ありがとうございました。

修正・追記箇所

  • 2022年3月24日
    • @Tetsu_Oikawa さんからのご指摘に基づき、タグに『エラー』を追加しました。
4
1
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
4
1