Syntax Error.

[Sy] MySQL5.7 にて root のパスワード変更などの初期設定をする(mysql_secure_installation 使用)

2017/10/26

MySQL5.7 をインストール後、 mysql_secure_installation というコマンドを使って root のパスワード変更などの初期設定をまとめてする手順です。

環境

Amazon Linux (2017.09-release) で動作確認していますが、Linux系であれば基本同じような手順になるかと思います。

1. mysqlの起動確認

まず、MySQLが起動していることを確認しておきます。

lsof コマンドがインストールされていれば、以下でプロセスがある(起動している)かチェックできます。

$ sudo lsof -i:mysql
COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
mysqld  22806 mysql   20u  IPv6 659348      0t0  TCP *:mysql (LISTEN)
⇒ [Sy] Mac/Linuxでポートを使っているプロセスを調べるのに便利なlsofコマンド

または、ps コマンドでもチェックできます。

$ ps ax | grep mysqld
22590 ?        S      0:00 /bin/sh /usr/libexec/mysql57/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
22806 ?        Sl     0:18 /usr/libexec/mysql57/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql57/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
27986 pts/0    S+     0:00 grep --color=auto mysqld

2. mysql_secure_installation 実行

MySQLが起動できていれば、 mysql_secure_installation を実行します。

$ mysql_secure_installation
Securing the MySQL server deployment.

以降、いくつか入力が必要になるので、そのタイミングごとに分けて説明していきます。

a. 現在の root のパスワード

次のように、現在の root のパスワードを求められた場合は、初期パスワードまたは設定済みの root のパスワードを入力します。

Enter password for user root:

Amazon Linux (2017.09-release) では、初期パスワードが付与されなかったので、以下のようなメッセージが出るだけで次に進みます。

Connecting to MySQL using a blank password.

b. パスワード検証プラグインのセットアップ

パスワードの強度をチェックしてくれるプラグインのセットアップをするかどうか聞かれるので、ここは 「y(セットアップする)」 と入力して進みましょう。

VALIDATE PASSWORD PLUGIN 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 plugin?

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

セットアップを選択すると、次に検証のレベルを3段階で選ぶように言われます。

それぞれ、このような制限でパスワードがチェックされます。

  • LOW:8文字以上
  • MEDIUM:8文字以上 + 数字・アルファベットの大文字と小文字・特殊文字を含む
  • STRONG:8文字以上 + 数字・アルファベットの大文字と小文字・特殊文字を含む + 辞書ファイルでのチェック

ここは本番環境を想定して、 「2(STRONG)」 を選択します。

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: 2

c. 新しいパスワードの設定

ここで新しいパスワードの設定になります。

先ほど選択した 検証レベルを満たすようなパスワードを入力 し、続けて確認のためにもう一度入力します。

Please set the password for root here.

New password: [新しいパスワードを入力]
Re-enter new password: [新しいパスワードを入力(確認)]

確認の入力を終えると、パスワードの強度が表示されます。(たぶん100は良い値だと思います)

そしてこのパスワードで確定して次に進んで良いか聞かれるので、 「y(確定して次へ)」 を入力します。

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

ここまででパスワードの変更は終わりです。

d. 匿名ユーザの削除

次に、匿名ユーザ( anonymous user )を削除するか聞かれるので、「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) : y

Success と表示されます。(匿名ユーザの削除完了)

Success.

e. リモートからの root ユーザでのログイン禁止

次は、リモートから 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) : y

Success と表示されます。(リモートからの root ユーザのログインができなくなる)

Success.

f. test データベースの削除

続いて、デフォルトで作成されている test という名前のデータベースを削除するか聞かれるので、 「y(削除する)」 を入力します。

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

test データベースの drop(削除)が行われ、test データベースに対する権限の削除も同時に実行されます。

 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

最後に、すぐに権限テーブルをリロードして変更を有効にするか聞かれるので、「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) : y

Success と表示されます。(権限テーブルの変更を有効化)

Success.

以上で全て終わりです。All done と表示されてコマンドが終了します。

All done!

3. 新しいパスワードを使って root でログイン

最後に確認として、新しいパスワードでログインしてみます。

$ mysql -u root -p
Enter password: [先ほど設定した新しいパスワードを入力]
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.18 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

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>

ログインできればパスワード設定成功です。

ついでに test データベースがなくなっているかチェックしてみます。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

なくなってますね。

以上で、MySQL5.7 の root パスワード変更などの初期設定は完了です。