【MySQL8】rootユーザー認証エラー/パスワード忘れの対応方法
はじめに
MySQL8でrootユーザーでログインしようとして以下のようなエラーが発生しました。
[root@wpserver html]# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
パスワードが間違っているようなのですが、どうしてもパスワードが思い出せず、何度リトライしてもエラーに。。。
結果としてはrootユーザーのパスワードを変更することで、無事にrootユーザーでログイン出来ましたので、その手順について説明します。
rootユーザーのログイン認証エラー対応手順
認証処理のスキップ設定
rootユーザーのパスワードを変更する為にはMySQLにログインする必要がありますが、このままではログイン出来ない為、一時的にパスワードなしでログイン可能とする設定を行います。
// 設定変更を行う為、MySQLを停止します
systemctl stop mysqld.service
// 設定変更を行う為、MySQLを停止します
systemctl stop mysqld.service
// 次に認証処理をスキップ(パスワードなしでログインする)為の設定を追加します。
vi /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
// 以下を追記
[mysqld]
skip-grant-tables
なお、my.cnfを書き換えずに以下のコマンドで設定変更する方法もあるようですが、なぜか私の環境では設定が変更になりませんでした。
systemctl set-environment MYSQLD_OPTS="–skip-grant-tables"
// 設定変更を反映させる為、MySQLを再起動します。
systemctl start mysqld.service
// この状態でrootユーザーでMySQLへログインしてみると
[root@wpserver etc]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 7
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.
mysql>
rootユーザーのパスワード変更
// mysqlデータベースを使用するコマンド
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
// rootユーザーのパスワードにnullを設定
mysql> UPDATE mysql.user SET authentication_string=null WHERE User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
// mysqlからログアウト
mysql> exit
// 認証処理スキップを解除
vi /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
// 以下を削除
[mysqld]
skip-grant-tables
なお、systemctlコマンドで設定変更していた場合、以下のコマンドで認証設定を元に戻します。
systemctl unset-environment MYSQLD_OPTS
// 設定反映する為にMySQLを再起動
systemctl restart mysqld.service
// パスワード不要でログイン可能なことを確認
[root@wpserver etc]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 8
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.
mysql>
// 改めてrootユーザーのパスワードを設定
mysql> ALTER USER 'root'@'localhost' identified BY 'password';
Query OK, 0 rows affected (0.00 sec)
// パスワード設定後のログイン確認
[root@wpserver html]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or ¥g.
Your MySQL connection id is 12
Server version: 8.0.17 Source distribution
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.
mysql>
終わりに
以上で終わりになります。ログイン後のパスワード変更SQLは、rootユーザー以外にも適用可能です。