Ubuntu20.04にMySQL8.0をインストールでmysql_secure_installationがエラーになる

Ubuntu
スポンサーリンク


Ubuntu20.04にMySQL8.0をインストールする時にセキュリティ設定のmysql_secure_installationでエラーになってしまい、rootのパスワードが設定できない謎現象に遭遇

その解決策についてまとめました。

前回構築したUbuntu20.04にPHP8をインストールしたサーバを使用します。

GCPで構築したUbuntu20.04にPHP8をインストール
Ubuntu20の環境にPHP8をインストールする必要が発生しました。 デフォルトの状態だとUbuntu20にはPHP7.4がインストールされてしまうため、PHP8が含まれているリポジトリを追加する必要があります。 サーバはGCPのVM...
スポンサーリンク

MySQLパッケージ確認

aptでインストールできるMySQLのパッケージを確認

$ apt show mysql-server
Package: mysql-server
Version: 8.0.31-0ubuntu0.20.04.2
Priority: optional
Section: database
Source: mysql-8.0
Origin: Ubuntu
Maintainer: Ubuntu Developers 
Original-Maintainer: Debian MySQL Maintainers 
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 114 kB
Depends: mysql-server-8.0
Homepage: http://dev.mysql.com/
Task: lamp-server
Download-Size: 9540 B
:
:
:

aptでインストールすればMySQL8.0がインストールできそう。

MySQL8.0インストール

インストールをする。

$ sudo apt install mysql-server

インストールされたバージョンを念のため確認しておく。

$ mysql --version
mysql  Ver 8.0.31-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

セキュリティ設定が進まない

インストールしたMySQLのrootのパスワード設定とその他セキュリティの設定をする。
※root権限で実行する必要がある

$ sudo mysql_secure_installation

画面の指示通りに設定していけばOK

・VALIDATE PASSWORD COMPONENTを使用するか
・password validation policyのレベル
・rootのパスワード

ここで問題発生!!
入力したパスワードで続けるか?と聞いてきたので当然「y」と入力

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

しかし、なぜかまたNew passwordの設定を要求される。

 ... 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.

mysql_secure_installationではパスワードの変更でSET PASSWORDを使っているようですが、その認証方法がダメだからALTER USER使えってことを言っている?なぜ?

一旦、設定を抜けようとしますが、New password、Re-enter new passwordの画面ではCtrl+cが効かない

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

この画面で、Ctrl+cをすることでmysql_secure_installationから抜けることができる。

mysql_secure_installation解決策

指示通りにMySQLにパスワードが未設定状態のrootでログインして、ALTER USERでパスワードを設定する。

パスワードはブランクでEnterキーでOK

$ sudo mysql -u root -p
Enter password

ALTER USERでrootのパスワードを設定

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'パスワード';
Query OK, 0 rows affected (0.01 sec)

MySQLから抜けて再度mysql_secure_installationを実行するとrootのパスワードを要求されるので先ほど設定したパスワードを入力してEnter

$ sudo mysql_secure_installation
Enter password for user root: 

パスワードを変更するか聞かれるので、変更しない場合は何も入力せずにEnter

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

この後の設定項目は次の通り

# 匿名ユーザを削除するか?
Remove anonymous users? (Press y|Y for Yes, any other key for No) :

# rootユーザによるリモートログインを禁止するか?
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :

# デフォルトで作成される「test」データベースを削除するか?
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :

# 特権テーブルを今スグリロードするか?(リロードすると今までの設定が反映される)
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :

All done!

何かの仕様が変わった可能性があります。
今回の環境はUbuntu20.04、PHP8、MySQL8なのでそれ以外の環境では問題なかったりするかもしれません。