だいぶ久しぶりの投稿だけど、Ubuntu24.04 で MySQL をインストールしたとき、MySQL の root パスワードの設定ができずに苦労した。
まず、Ubuntu24.04 を Conoha VPS で構築。それで
$ sudo apt install mysql-server mysql-client
でふつうにインストール。MySQL8 が入る。そのうえで
$ sudo mysql_secure_installation
で設定していけばいいはずなんだが、ほかの方のを見るとこれで途中で root ユーザのパスワードを設定できるはずなんだが、
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: 0
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
てな感じで、root のパスワードの設定が飛ばされる。それで ALTER_USER をうんぬんとあるので、こちらのページなどを参考にしてやってみたがなぜかうまく行かず。
うーん、こんなところでつまづくなんてなかったよな、と途方にくれてしまったが、
https://askubuntu.com/questions/1487925/unable-to-set-password-in-mysql-installation
このページのやり方でうまく行った。
$ sudo mysql -u root mysql
で MySQL に入って、
mysql > UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql > FLUSH PRIVILEGES;
とやってやる。それで、上のページでも不要っぽいが、
mysql > ALTER USER root@localhost IDENTIFIED BY 'password';
そのうえで、sudo mysql_secure_installation をやってやれば、root パスワードを設定できる。
というか、別に改めて sudo mysql_secure_installation って、やらなくてもいい気もするけど、とにかくこれでうまく行った。ほっ