本記事対象
MariaDB10でrootのパスワードを初期化する方法が知りたい方
ども。あいしんくいっと(@ithinkitnet)です。
ithinkit
MariaDBでrootユーザーのパスワード忘れた・・・。
たまにしかrootユーザーを利用しないとよくある話ですよね。
さて。MariaDBでrootパスワードを初期化するにはどうすれば良いでしょうか。
試した環境
OS | MariaDB |
---|---|
CentOS 7.7 | 10.1.41 |
MariaDBのrootパスワードを初期化する方法
Point
5.x系とは手順が異なるので注意!
rootのパスワード初期化の流れ
rootユーザーのパスワード初期化の流れは以下の通り。
STEP.1
DB停止
STEP.2
環境変数セット
STEP.3
DB起動
STEP.4
rootパスワード変更
STEP.5
DB停止
STEP.6
環境変数削除
STEP.7
DB起動
rootユーザーのパスワード初期化手順
では、詳細な手順を見ていきましょう。
MariaDB停止させます。
MariaDB停止
systemctl stop mariadb
環境変数セットします。
“–skip-grant-tables”という環境編集オプションを設定し、MariaDBを起動します。
環境変数セット
systemctl set-environment MYSQLD_OPTS="--skip-grant-tables"
環境変数がセット出来たら、MariaDBを起動します。
MariaDB起動
systemctl start mariadb
MariaDBが起動したら接続。
ithinkit
おぉ。rootユーザでパスワードなしでログイン成功!
MariaDBへログイン
# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.41-MariaDB MariaDB Server
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)]>
rootユーザでDBに接続出来たら、パスワード変更SQL実行。
rootパスワード変更SQL
MariaDB [(none)]> update user set password=PASSWORD("新パスワード") where User='root';
では、実際にやってみます。
今回は「password」という一番やってはいけないパスワードをrootユーザーのパスワードとして設定。
(真似しないようにw)
rootパスワード再設定
# mysql -u root
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.41-MariaDB MariaDB Server
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)]> update user set password=PASSWORD("password") where User='root
';
ERROR 1046 (3D000): No database selected
MariaDB [(none)]> 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
MariaDB [mysql]> update user set password=PASSWORD("password") where User='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> quit
Bye
rootパスワードが変更出来たら、MariaDB停止。
MariaDB停止
systemctl stop mariadb
先ほど設定した、”–skip-grant-tables”オプション解除。
セットした環境変数を削除
systemctl unset-environment MYSQLD_OPTS
MariaDB起動。
MariaDB起動
systemctl start mariadb
あとは変更したパスワードでrootログイン出来ることが確認出来れば良い。
変更したパスワードでrootログイン
# 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.1.41-MariaDB MariaDB Server
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)]> quit
ithinkit
とりあえず、Mariadb10のrootパスワード忘れたら、上記手順で対応を。
「-(ハイフン)」含むDBの削除方法。
MariaDBでデータベース削除出来ない!?-(ハイフン)を含むDB名は要注意!
以上、あいしんくいっとでした。