- 1. mysql_secure_installation
- 2. root パスワードの設定
1. mysql_secure_installation
「MySQL 5.7」からインストール後に「mysql_secure_installation」というスクリプトを実行してパスワード等を設定するのが正式な設定方法のようです。
「root」ユーザで
$ mysql_secure_installation
mysql_secure_installation: [ERROR] unknown variable 'prompt=\u@\h [\d]>\_'
プロンプトが変と言っているのですが無視して先へ進めます
Securing the MySQL server deployment.
Connecting to MySQL server using password in '/root/.mysql_secret'
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:
パスワードのレベルを決めますかってんで yEnter
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:
パスワードのレベルを0、1、2のいずれかで入力します(レベルは後述)
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
root のパスワードを変更するかってんで yEnter
New password: パスワードを入力します
Re-enter new password: パスワードを再入力します
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :
レベル 50 だがよいかってことかな? yEnter
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) :
「anonymous users」を削除しますかってんで yEnter
Success.
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) :
root ユーザのリモートログインを禁止しますかってんで yEnter
Success.
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) :
テストデーターベースを削除しますかってんで yEnter
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
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) :
権限が変更されたテーブルを再度読み込みますかってんで yEnter
Success.
All done!
これで、基本的な設定は、終わりです。
いったん、サーバを再起動します。
service mysql-server restart
2. root パスワードの設定
「root」のパスワードを変更した場合は、ログインして再度パスワードの設定を行います。
そうしないと、
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.28-log
Copyright (c) 2000, 2019, 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.
root@localhost [(none)]> SHOW VARIABLES LIKE 'validate_password%';
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
てな感じで怒られちゃうのです。
たまたま、「SHOW VARIABLES LIKE ...」てな感じですが、これは、たまたまで、ほぼどんな SQL でも受け付けてくれない。
不思議に「SET」は、受け付けられるので・・・。
そのまま続けて、パスワードを設定します。
わたしの場合、パスワードを最小4文字で、低めのポリシーにしますので、あらかじめ、そういう設定を行ってから、パスワードを設定します。
root@localhost [(none)]> SET GLOBAL validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
root@localhost [(none)]> SET GLOBAL validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)
root@localhost [(none)]> set password for root@localhost=password('パスワード');
Query OK, 0 rows affected, 1 warning (0.00 sec)
root@localhost [(none)]>
その後、おもむろに、先ほど受け付けてくれなかった、SQL を発行すると
root@localhost [(none)]> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 4 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.05 sec)
ちゃんと、受け付けてくれるのです。
|