最終更新:2023-06-13 (火) 03:21:12 (290d)
mysql_secure_installation
Top / mysql_secure_installation
Improve MySQL Installation Security
http://dev.mysql.com/doc/refman/5.1/en/mysql-secure-installation.html
使い方
- まずmysqldを起動しておく。
設定項目
- Enter current password for root (enter for none):
- Set root password? [Y/n]
- New password:
- Re-enter new password:
- Remove anonymous users? [Y/n]
- Disallow root login remotely? [Y/n]
- Remove test database and access to it? [Y/n]
- Reload privilege tables [Y/n]
Ubuntu Serverでエラー
- ... 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サーバーに認証データを保存しないため、SET PASSWORDはユーザー 'root'@'localhost' に対して意味を持ちません。認証パラメータを変更したい場合は、代わりにALTER USERを使用することを検討してください。
解決法
- https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-22-04
- https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04
- As of July 2022, an error will occur when you run the mysql_secure_installation script without some further configuration.
- The reason is that this script will attempt to set a password for the installation’s root MySQL account but, by default on Ubuntu installations, this account is not configured to connect using a password.
- Ubuntuではrootユーザの認証はパスワードを使わない方式になっている (auth_socket)
- →認証方式にmysql_native_passwordを指定してalter userする
エラーの原因: SET PASSWORD
- 現在接続しているユーザーに新しいパスワードを設定するコマンド
SET PASSWORD = 'auth_string'
- これがエラーになっている
- sudo mysqlでログインして試すと同じエラー
mysql> set password = 'password'; ERROR 4102 (HY000): 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のユーザー情報 (mysql.user)
- Ubuntu Server 20.04
mysql> SELECT user, host, plugin FROM mysql.user; +------------------+-----------+-----------------------+ | user | host | plugin | +------------------+-----------+-----------------------+ | debian-sys-maint | localhost | caching_sha2_password | | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | | root | localhost | auth_socket | +------------------+-----------+-----------------------+ 5 rows in set (0.00 sec)
- rootのplugin(認証方式)がauth_socketになっているのが原因
- RHEL 9の場合
mysql> SELECT user, host, plugin FROM mysql.user; +------------------+-----------+-----------------------+ | user | host | plugin | +------------------+-----------+-----------------------+ | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | | root | localhost | caching_sha2_password | +------------------+-----------+-----------------------+ 4 rows in set (0.00 sec)
auth_socket
- MySQLのユーザ名とOSのユーザ名が一致している場合に接続を許可する認証方式
- auth_socketプラグインはクライアントから渡されたパスワードは見ないしサーバー側でもパスワードハッシュを保管していない
- Ubuntuの場合、rootという名前のユーザを作成することはできないので、この認証方式を利用する限りsudo使う必要がある
- https://gn-office.pro/archives/348
認証方法別のrootでのMySQLへのアクセス方法
auth_socket
- 現在のLinuxユーザ名で認証するのでパスワードを入れなくてよい
$ sudo mysql
- しかしmysql_secure_installationがコケるので実行したい場合下記に変更する必要がある
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql_native_password
$ mysql -u root -p
- こっちだと接続時にパスワードが必要になるので元に戻したい場合は
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
- を実行