MySQL8のmysql_secure_installationでパスワードが設定されない。

MySQLをインストール後にmysql_secure_installationを実行しrootパスワード設定を行う時にFailed! Errorが発生するようになりました。

AzureのUbuntu 20.04 LTSでは、エラーは下記のように表示されます。

... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

問題

検証環境はAzureでUbuntu 20.04 LTSです。

MySQLをインストールします。

># apt install mysql-server

下記のようにエラーが表示されます。

# 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: 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: 2
Please set the password for root here.

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) : y
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.

この状態でループします。

これを解消するために一旦終了します。ただし、パスワード入力時にはCtrl+Cで抜け出せません。

下記が表示されているときにCtrl+Cで終了します。

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

MySQLにログインしてから設定

デフォルトの認証用プラグインを変更する必要があります。

/etc/mysql/mysql.conf.d/mysqld.cnf

以下を追記してプラグインを変更します。

default_authentication_plugin=mysql_native_password

MySQLを再起動します。

systemctl restart mysql

MySQLに接続します。

mysql -uroot -p

{PASSWORD}を設定します。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{PASSWORD}';

設定されたか確認します。
rootにハッシュが入力されていれば設定されたことを示します。

mysql>  select user, host, authentication_string from mysql.user;
+------------------+-----------+------------------------------------------------------------------------+
| user             | host      | authentication_string                                                  |
+------------------+-----------+------------------------------------------------------------------------+
| debian-sys-maint | localhost | $A$005$>uffp6}=tz3^J1kAuA/kXSYTliNplLwplTSS2hKelmoT36qNnD488ZpKro8 |
| mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.session    | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| mysql.sys        | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| root             | localhost | *929D791403F393B43E9C36D4F32E9EDE5EEC2EE1                              |
+------------------+-----------+------------------------------------------------------------------------+

違うパスワードを入力して接続できないことを確認しましょう。

mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

PAGE TOP