【MySQL】Ubuntu上のMySQL8.xのルートパスワードを設定する

2020年9月9日Linux,Windows,環境構築

こんにちは、しきゆらです。
今回は、MySQLをまっさらな状態からインストールして最初にはまるであろうルートパスワードの設定方法で詰まったのでメモしておきます。

毎回MySQLの初期ルートパスワードで詰まる

これまで、5.xのころからMySQLさんを使ってきましたが、バージョンの違いなのかディストリビューションごとの差異なのかはわかりませんが、初期状態のルートパスワードが変わります。
パスワードなしでログインできたり、どこかにあるログファイルにかかれていたり、いくつかバリエーションがあるようです。

今回、MySQL8.xをインストール後にルートユーザとしてログインしようとしてもログインできませんでした。
パスワードなしでアクセスしても怒られ、ログがそもそもどこにあるのかわからず完全に詰んでいました。
そんな中、過去の記憶を掘り起こしたところ、MySQLの初期化コマンドでルートパスワードを設定していたことを思い出しました。
secure_mysql_installationコマンドです。


初期化コマンドで初期パスワードなどを変更する

前提として、MySQLのインストールとsystemctlを使って操作できるようにしておきます。
参考:【WSL2】Ubuntu20.04でPID1をSystemdにする

$ sudo systemctl mysql

$ systemctl status mysql
      Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
      Active: active (running) since Wed 2020-09-02 23:04:07 JST; 7s ago

インストール後にMySQLを起動させた後、以下のように実行できます。

$ sudo mysql_secure_installation
 Securing the MySQL server deployment.
 
 Connecting to MySQL using a blank password.
 
 VALIDATE PASSWORD COMPONENT can be used to test passwords
 and improve security. It checks the strength of password
 and allows the users to set only those passwords which are
 secure enough. Would you like to setup VALIDATE PASSWORD component?
 
 Press y|Y for Yes, any other key for No: y # パスワード周りのコンポーネントを使う
 
 There are three levels of password validation policy:
 
 LOW    Length >= 8
 MEDIUM Length >= 8, numeric, mixed case, and special characters
 STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file
 
 Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 # ローカルだけなので低いもので良いかと
 Please set the password for root here.
 
 New password: # rootユーザのパスワードを入力
 
 Re-enter new password: # 再度パスワードを入力
 
 Estimated strength of the password: 50
 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y # 
 By default, a MySQL installation has an anonymous user,
 allowing anyone to log into MySQL without having to have
 a user account created for them. This is intended only for
 testing, and to make the installation go a bit smoother.
 You should remove them before moving into a production
 environment.
 
 Remove anonymous users? (Press y|Y for Yes, any other key for No) : y # 匿名ユーザを削除する
 Success.
 
 
 Normally, root should only be allowed to connect from
 'localhost'. This ensures that someone cannot guess at
 the root password from the network.
 
 Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y # リモートからrootユーザとしてログインできないようにする
 Success.
 
 By default, MySQL comes with a database named 'test' that
 anyone can access. This is also intended only for testing,
 and should be removed before moving into a production
 environment.
 
 
 Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y # テスト用に作られたデータベースを削除する
  - Dropping test database...
 Success.
 
  - Removing privileges on test database...
 Success.
 
 Reloading the privilege tables will ensure that all changes
 made so far will take effect immediately.
 
 Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y # 設定周りのリロードをする
 Success.
 
 All done!

コメントで書きましたが、上記のコメントではルートパスワード変更以外に、デフォルトで作られるテーブルやユーザを削除したり、リモートからルートアカウントにログインできなくするなどの設定を行っています。
この辺は、必要に応じて良しなに設定しておきましょう。

さて、ルートパスワードを変更したら、ログインできるか確認しましょう。
ここでも私はハマりました。
いつの間にか、ルートユーザでログインするためにはsudo権限で実行する必要がありました。
sudo mysql -uroot -pでログインできました。

これにて、MySQLの最低限の初期設定が完了しました。
あとは、使うアプリやツールに合わせてDBを作ったりユーザを作ったりすればいいですね。

まとめ

今回は、MySQLの初期設定をメモしました。

久々すぎて詰まってしまいましたが、調べてみると意外とすんなり通り抜けられたので良かったです。
特に、最後の「ルートユーザとしてログインするときにsudo権限が必要」というのは知りませんでした。
この設定というか変更はいつの間に入ったんでしょうか。

今回は、ここまで。
おわり

Posted by しきゆら