MySQL

mysql_secure_installationを実行したい

概要

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

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

確認した環境

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

入力ミスを防ぐため、コマンドラインのみで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の初期設定は完了です。

アカウントファイルを用いたDBログインとバックアップ。

概要

バックアップスクリプトなどでMySQLにログインして処理を行う場合のTIPSです。

さっくりとした手順

  1. アカウントファイルを作ります。
  2. ログインできることを確認します。
  3. アカウントファイルを用いたコマンドでバックアップできることを確認します。

アカウントファイル作成

sudo mkdir -p /home/hoge/db_password

運用に合わせて指定ください。

cd /home/hoge/db_password && pwd

指定したディレクトリに移動します

以下の内容を教義・信仰に沿ったエディタで作成します。(【】内は取り除き、自分の設定に合わせます)

[client]
user = 【RedmineのDBユーザ】
password = "【RedmineのDBユーザ用パスワード】"

password は""で囲みます。

chmod 400 account.txt
ls -l account.txt

パーミッションが400であることを確認します。

ファイルを用いてのログインを確認

mysql --defaults-extra-file=/path/to/directory/account.txt

--defaults-extra-file=は、アカウントファイルの絶対パスです。

ログインできることを確認します。

SHOW DATABASES;

アカウントの権限で指定されたDBの表示を確認します。

EXIT

MySQLから抜けます。

SQL Dumpの取得

mysqldump --defaults-extra-file=/path/to/directory/account.txt --no-tablespaces -h [DBサーバ] [DB名] > backup.sql
ls -l backup.sql

ファイルの内容にDBがあれば成功です。取り扱いには慎重を期してください。