データベース - MySQL - root パスワードを忘れたとき

 
1. 概要
2. 対処

1. 概要

 たまたま調べる機会がありましたので、備忘で・・・。  本項は「MySQL の root パスワード忘れた時」を参考にさせていただきました。

2. 対処

 要はセーフモードで起動してパスワードを設定しなおすってことのようです。  以下、やってみたプラットフォームは FreeBSD です。  以下 root ユーザで  サーバの停止

> service mysql-server stop
Stopping mysql.
Waiting for PIDS: 2059.
 セーフモードで起動

> /usr/local/bin/mysqld_safe --skip-grant-tables &
[1] 39613
2018-01-30T00:50:13.6NZ mysqld_safe Logging to '/var/db/mysql/vm.sing.ne.jp.err'.
2018-01-30T00:50:13.6NZ mysqld_safe Starting mysqld daemon with databases from /var/db/mysql
 MySQL に接続してパスワードを設定

> mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21-log Source distribution

Copyright (c) 2000, 2018, 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.

root@localhost [(none)]> use mysql
Database changed
 ここからのコマンドがバージョンによって変わってきます。  5.6 以前は

UPDATE USER SET PASSWORD=PASSWORD("パスワード") WHERE USER='root';
FLUSH PRIVILEGES;
 でよかったらしいのですが、少なくともバージョン 5.7.21 では

UPDATE USER SET AUTHENTICATION_STRING=PASSWORD("パスワード") WHERE USER='root';
FLUSH PRIVILEGES;
 なのです。  use mysql の続きでやってみます。

root@localhost [mysql]> UPDATE USER SET AUTHENTICATION_STRING=PASSWORD("パスワード") WHERE USER='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

root@localhost [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

root@localhost [mysql]> quit
Bye
 MySQL を再起動します。

> service mysql-server restart
Stopping mysql.
Waiting for PIDS: 467812018-01-30T01:34:30.6NZ mysqld_safe mysqld from pid file /var/db/mysql/vm.sing.ne.jp.pid ended
.
Starting mysql.
[1]  + 終了                        /usr/local/bin/mysqld_safe --skip-grant-tables
 設定したパスワードで接続できるか確認してみます。

> mysql -u root -p
Enter password:			←	設定したパスワードを入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21-log Source distribution

Copyright (c) 2000, 2018, 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.

root@localhost [(none)]>
 無事変更できたようです。
スポンサーリンク