Linuxの実用的な小さなスクリプトシリーズ(2)-インタラクティブな実行なしのmysqlセキュア初期化スクリプト-mysql_secure_installation
通常、mysqlのインストール後、mysqlのセキュリティを強化するスクリプトがあり、初期化パスワードもこのスクリプトを介してすばやく実現できますが、独自のスクリプトを使用してmysqlをインストールする場合(バイナリ、コンパイル済み、yumのいずれであるかに関係なく)インストール)、インストール完了後、スクリプトを手動で実行する必要があります-mysql_secure_installation、これはそれほどイライラしません。この問題を解決するにはどうすればよいですか?期待スクリプトは、非常にアプリコットのようで高速な、対話なしでスクリプトを実装するのに役立ちます。
これで、mysqlがインストールされていると想定されます。ここで、セキュリティスクリプトをすばやく実行するためのexpectスクリプトの作成を開始します。
yum install -y expect #expectスクリプトインタープリターをインストールします
vim mysql_secure.shの内容は次のとおりです:(設定されたパスワード行の後に書き込む必要のあるパスワード)
#!/usr/bin/expect
set passwd 要设定的密码
spawn mysql_secure_installation
expect {
"Enter current password" { send "\r"; exp_continue }
"Y/n" { send "Y\r"; exp_continue }
"New password" { send "$passwd\r"; exp_continue }
"Re-enter new password" { send "$passwd\r"; exp_continue }
"Remove anonymous users" { send "Y\r"; exp_continue }
"Disallow root login remotely" { send "Y\r"; exp_continue }
"Remove test database and access to it" { send "Y\r"; exp_continue }
"Reload privilege tables now" { send "Y
スクリプトの実行:スクリプト名、または./script名を期待します。もう1つ、通常はシェルスクリプトを実行する1つの方法は、bashスクリプト名です。次に、bashをexpectに置き換える必要があります。./script nameの方法はシェルスクリプトと同じで、実行権限が必要です。chmod+ xスクリプト名。
上記のスクリプトでは、パスワードがスクリプトにハードコードされています。セキュリティを向上させるためにパラメータを渡すなど、より柔軟にしたい場合は、わずかな変更を加えるだけで、パラメータを使用してスクリプトを実行できます。
#!/usr/bin/expect
set passwd [lindex $argv 0]
spawn mysql_secure_installation
expect {
"Enter current password" { send "\r"; exp_continue }
"Y/n" { send "Y\r"; exp_continue }
"New password" { send "$passwd\r"; exp_continue }
"Re-enter new password" { send "$passwd\r"; exp_continue }
"Remove anonymous users" { send "Y\r"; exp_continue }
"Disallow root login remotely" { send "Y\r"; exp_continue }
"Remove test database and access to it" { send "Y\r"; exp_continue }
"Reload privilege tables now" { send "Y\r" }
}
スクリプトを実行するときは、パスワードという1つのパラメーターを取得するだけです。
たとえば、スクリプト名パラメーター1が必要な場合、パラメーター1の値はパスワードであり、mysqlにログインすることで確認できます。