メインコンテンツへスキップ

mysql_secure_installationを実行したい

概要

5.7以降では実行する必要がなくなったとはいえ、mysql_secure_installationは以下の機能を持つため重要です。

  • rootユーザーのパスワードを設定
  • リモートからのrootユーザーのログインを無効にする
  • 匿名ユーザーアカウントを削除
  • testデータベースを削除し、testデータベースへのアクセス権限を持つユーザーを削除

バージョンアップによって、以下の手順が必要になりました。

確認した環境

  • Ubuntu 20.04 LTS
  • MySQL 8.0.32

mysql設定変更

  • 設定ファイルのバックアップ
sudo cp -pi /etc/mysql/mysql.conf.d/mysqld.cnf /path/to/backup/directory/mysqld.cnf.$(date +%Y%m%d)

任意のバックアップディレクトリを指定します。

  • バックアップ確認
diff -u /etc/mysql/mysql.conf.d/mysqld.cnf /etc/old/mysqld.cnf.$(date +%Y%m%d)

エラーがなければ(差分がなければ)バックアップは成功です。

  • ファイル追記
echo -e "default_authentication_plugin=mysql_native_password" | sudo tee -a /etc/mysql/mysql.conf.d/mysqld.cnf

sedを用いてmysqld.cnfに追記をします

設定後の差分確認

  • 差分確認
diff -u /path/to/backup/directory/mysqld.cnf.$(date +%Y%m%d) /etc/mysql/mysql.conf.d/mysqld.cnf
  • 差分内容
+default_authentication_plugin=mysql_native_password

設定反映

sudo systemctl restart mysql.service

mysql rootパスワード変更

sudo mysql

この時点ではパスワードは設定されていません。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
#''のpasswordは任意のものを入力ください

flush privileges;

exit

mysql_secure_installation

sudo mysql_secure_installation

質問事項

Enter password for user root: 
# 上記で設定したパスワードを入力します

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を入力してEnter

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/1/2を入力

Estimated strength of the password: 50 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 
# 既に設定しているのでn

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) : 
# anonymousユーザーを削除するためY

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) : 
# rootユーザのリモートログインを禁止するためY

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 
# テストDBを削除するためY

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 
# 設定を反映するためy

以上でMySQLの初期設定は完了です。