概要
MySQLの最高権限のログインアカウント root のパスワードを忘れてしまった場合は、一度MySQLサービスを停止後、セーフモードでログインしてから現在設定されている root のパスワードを再設定することで復旧させます。
尚、セーフモードのログイン方法はMySQLのバージョンにより異なりますが、本手順では MySQL 8 で行った際の手順を解説します。
構築環境
- CentOS Linux release 8.2.2004 (Core)
- mysql Ver 8.0.21
MySQLをセーフモードでログイン
▼MySQLのサービスを停止
command # systemctl stop mysqld
▼セーフモードでログインできるようにする
下記のコマンドで環境変数を変更することで、ユーザーデータの格納されたテーブルを参照して行われるユーザー認証の際にテーブルが見られなくなり、パスワード無しで root ログインが可能になります。
command # systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
▼MySQLの起動
command # systemctl start mysqld
root のパスワードを削除
▼MySQLに root でログイン
command # mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 8.0.21 MySQL Community Server - GPL
▼MySQLのユーザー管理テーブルに移動
command 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
▼ユーザー管理テーブルの確認
command mysql> SELECT user, host, authentication_string FROM user; +------------------+-----------+------------------------------------------------------------------------+ | user | host | authentication_string | +------------------+-----------+------------------------------------------------------------------------+ | mysql.infoschema | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | mysql.session | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | mysql.sys | localhost | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | | root | localhost | *7AA5134E27089F7821022EDECBF98A7055F842AA | +------------------+-----------+------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
▼root のパスワードを削除
暗号化されたパスワードの文字列を格納している「authentication_string」カラムを「空(null)」の値で置き換えることで削除となります。
command mysql> UPDATE user SET authentication_string=null WHERE User='root';
▼変更内容をデータベースに反映
command mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)
▼MySQLからログアウト
command mysql> exit Bye
セーフモードでのログインを戻す
現在は「--skip-grant-tables」が設定されたままなので、再びテーブルをみてログイン情報の照合がされるように先ほど設定した環境変数を削除して、元に戻します。
▼環境変数の設定状況確認
command # systemctl show-environment LANG=ja_JP.UTF-8 MYSQLD_OPTS=--skip-grant-tables PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
▼「MYSQLD_OPTS」の設定を削除
command # systemctl unset-environment MYSQLD_OPTS
▼「MYSQLD_OPTS」の削除確認
command # systemctl show-environment LANG=ja_JP.UTF-8 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
▼MySQLを再起動で環境変数の設定を反映
command # systemctl restart mysqld
MySQLのrootパスワードを再設定
パスワードの設定は mysql にログイン後にSQL文を実行して設定を行います。
SQL文
ALTER USER 'root'@'localhost' identified BY '新しいパスワード';
▼パスワード変更
command mysql> ALTER USER 'root'@'localhost' identified BY 'abcdEFG12345';
▼変更後の確認
一度MySQLからログアウト後に、先ほど設定したパスワードでログインができることを確認してみてください。
command mysql> exit Bye # mysql -u root -p Enter password: 先ほど設定したパスワード Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 57041 Server version: 8.0.21 Source distribution Copyright (c) 2000, 2020, 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>