7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mysql_secure_installationのインタラクティブな入力が自動化の妨げになる

Posted at

掲題の通り。

最初はexpectしてたのですが「minimalで入らないパッケージに依存して何が自動化だ」と。
考え直して/usr/bin/mysql_secure_installationの中身を読みました。

結果。
してることは至極単純で以下SQLを発行してるだけでした。

UPDATE mysql.user SET Password=PASSWORD('********') WHERE User='root';
DELETE FROM mysql.user WHERE User='';
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
DROP DATABASE test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\_%';
FLUSH PRIVILEGES;

grep do_query /usr/bin/mysql_secure_installationすれば抽出できます。

以上より、expectはやめてこんな感じにしました。

# ランダムパスワード生成
vMariadbRootPasswd="$(cat /dev/urandom | tr -dc '[:alnum:]' | head -c 16 | tee -a ~/.mysql.secrets)"

# mysql_secure_installation
mysql -u root --password= -e "
    UPDATE mysql.user SET Password=PASSWORD('${vMariadbRootPasswd}') WHERE User='root';
    DELETE FROM mysql.user WHERE User='';
    DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1');
    DROP DATABASE test;
    DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
    FLUSH PRIVILEGES;"

以下の500,000,000倍いいですね。

expect -c '
    set timeout 10;
    spawn mysql_secure_installation;
    expect "Enter current password for root (enter for none):";
    send "\n";
    expect "Set root password?";
    send "y\n";
    expect "New password:";
    send "'"${vMariadbRootPasswd}"'\n";
    expect "Re-enter new password:";
    send "'"${vMariadbRootPasswd}"'\n";
    expect "Remove anonymous users?";
    send "y\n";
    expect "Disallow root login remotely?";
    send "y\n";
    expect "Remove test database and access to it?";
    send "y\n";
    expect "Reload privilege tables now?";
    send "y\n";
    interact;'

おわり。

7
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?