mariadbのルートのパスワードを忘れた。

はじめに

01:25 午後 kimi@ryzen9-MS-7C37:~
$ mysql -u root -p
Enter password: 
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

rootのパスワードを忘れたのでリセットする。よく忘れるので、ここに記録しとこ

パスワード無しでログインする

--skip-grant-tablesオプションで起動する。特権テーブルのチェックが無効化されて、すべてのユーザーに特権が与えられた状態(パスワードなし)でログイン可能になる。

01:34 午後 kimi@ryzen9-MS-7C37:~
$ sudo systemctl stop mariadb
01:35 午後 kimi@ryzen9-MS-7C37:~
$ sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
01:36 午後 kimi@ryzen9-MS-7C37:~
$ sudo systemctl start mariadb

入れた

01:36 午後 kimi@ryzen9-MS-7C37:~
$ mysql -u root
$ sudo mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 33
Server version: 10.6.16-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

パスワードを変更

パスワード変更を試みるがエラーだ。

MariaDB [(none)]> alter user 'root'@'localhost' identified by '********';
ERROR 1290 (HY000): The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement

あ、特権テーブルを無効化しているので、通常の方法ではパスワード変更は無効、エラーとなる。flush privileges;で特権情報を再読込して、パスワードを変更。

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> alter user 'root'@'localhost' identified by '********';
Query OK, 0 rows affected (0.001 sec)

後処理

mariadbの起動オプションをもとに戻して再起動。新しいパスワードでログインできた。

01:46 午後 kimi@ryzen9-MS-7C37:~
$ sudo systemctl stop mariadb
01:46 午後 kimi@ryzen9-MS-7C37:~
$ sudo systemctl unset-environment MYSQLD_OPTS
01:47 午後 kimi@ryzen9-MS-7C37:~
$ sudo systemctl start mariadb
01:47 午後 kimi@ryzen9-MS-7C37:~
$ sudo mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.6.16-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA