LoginSignup
19
22

More than 5 years have passed since last update.

Chefのレシピでmysql_secure_installation(bashリソース利用)

Last updated at Posted at 2013-08-22

この記事は最終更新から1年以上経過しています。 気をつけてね。

まずmysql_secure_installationするじゃないですか。もののついでにChefでやっておこうと思いました。

mysql_secure_installation実行時の質問を抜粋。

  1. Change the root password [Y/n]
  2. Remove anonymous users? [Y/n]
  3. Disallow root login remotely? [Y/n]
  4. Remove test database and access to it? [Y/n]
  5. Reload privilege tables now? [Y/n]

で、同様のことを実施。

mysql_secure_install.rb
bash 'mysql_secure_install emulate' do
  code <<-"EOH"
    /usr/bin/mysqladmin drop test -f
    /usr/bin/mysql -e "delete from user where user = '';" -D mysql
    /usr/bin/mysql -e "delete from user where user = 'root' and host = \'#{node[:hostname]}\';" -D mysql
    /usr/bin/mysql -e "SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpassword');" -D mysql
    /usr/bin/mysql -e "SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpassword');" -D mysql
    /usr/bin/mysql -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');" -D mysql
    /usr/bin/mysqladmin flush-privileges -pnewpassword
  EOH
  action :run
  only_if "/usr/bin/mysql -u root -e 'show databases;'"
end

パスワードはencrypted_data_bagに置くとして、フルbashじゃないですか。

追記: 3. Disallow root login remotely? [Y/n]に当たる処理はコメント欄にある方が適切です。

result

試してみます、パスワード無しでmysqlに接続できるようならmysql_secure_installation相当の処理をさせます。

chef-apply
# cat <<'EOL' | chef-apply -s
> bash 'mysql_secure_install emulate' do
>   code <<-"EOH"
>     /usr/bin/mysqladmin drop test -f
>     /usr/bin/mysql -e "delete from user where user = '';" -D mysql
>     /usr/bin/mysql -e "delete from user where user = 'root' and host = \'#{node[:hostname]}\';" -D mysql
>     /usr/bin/mysql -e "SET PASSWORD FOR 'root'@'::1' = PASSWORD('newpassword');" -D mysql
>     /usr/bin/mysql -e "SET PASSWORD FOR 'root'@'127.0.0.1' = PASSWORD('newpassword');" -D mysql
>     /usr/bin/mysql -e "SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newpassword');" -D mysql
>     /usr/bin/mysqladmin flush-privileges -pnewpassword
>   EOH
>   action :run
>   only_if "/usr/bin/mysql -u root -e 'show databases;'"
> end
> EOL
Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * bash[mysql_secure_install emulate] action run
    - execute "bash"  "/tmp/chef-script20130822-12249-10qdpq7-0"
mysql_user_table
# mysql -e 'select user,host,password from user;' -D mysql -pnewpassword
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *FE4F2D624C07AAEBB979DA5C980D0250C37D8F63 |
| root | 127.0.0.1 | *FE4F2D624C07AAEBB979DA5C980D0250C37D8F63 |
| root | ::1       | *FE4F2D624C07AAEBB979DA5C980D0250C37D8F63 |
+------+-----------+-------------------------------------------+

まあできてるか。

2度目以降はonly_ifが効いてお断りしてくれます。

Recipe: (chef-apply cookbook)::(chef-apply recipe)
  * bash[mysql_secure_install emulate] action run (skipped due to only_if)
19
22
2

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
19
22