LoginSignup
1
0

More than 1 year has passed since last update.

systemd環境でMySQL8のrootパスワードを初期化する方法

Last updated at Posted at 2021-12-03

Amazon Linux2で、MySQL8を運用していて軽くハマったので備忘録

rootのパスワードが分からなくなってリセットしようとググると、ほとんどの記事が「mysqld_safeで起動しなおして云々」というモノなのだが、systemdを採用しているディストリビューションだと「mysqld_safe」がインストールされないらしい。
そこで、もうちょっと調べて見つけた解決方法。

[root@hostname ~]# systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
[root@hostname ~]# systemctl restart mysqld
[root@hostname ~]# mysql -u root

これでrootでログイン出来るので、rootのパスワードを削除します。

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> UPDATE user SET authentication_string=null WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql>

次に、パスワードの再設定を行いますが、先程の「--skip-grant-tables」が設定されたままだと、パスワードの再設定が許可されていません。
なので、環境変数を削除した後にmysqldを再起動します。

mysql> exit
Bye
[root@hostname ~]# systemctl unset-environment MYSQLD_OPTS
[root@hostname ~]# systemctl restart mysqld
[root@hostname ~]#

再起動完了後に、再びmysqldに接続しパスワードを再設定します。

[root@hostname ~]# mysql -u root
mysql> ALTER USER 'root'@'localhost' identified BY '設定するパスワード';
Query OK, 0 rows affected (0.00 sec)

mysql>

パスワード変更コマンドを実行した時に、

Your password does not satisfy the current policy requirements

とエラーが出た場合は、パスワードポリシーが厳しいので怒られています。
おそらく「最短8文字、ローマ字小文字、ローマ字大文字、数字、記号、すべて混在」というポリシーになっているかと思います(頑張ってください)。

これで、systemd環境のMySQL8でルートのパスワード変更が出来ると思います。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0