ChefでMySQL5.6をインストール


* MySQLの提供しているYumリポジトリを利用してMySQLの5.6を「セキュアインストール」したい
* DBサーバ建てるからにはひとつはDB欲しいしそれ用のユーザも欲しい

ということでそのあたりをカバーするChefレシピを作成してみました。
「変数」に当たる部分は適宜DataBagなりを使っていただくほうがベターでしょうね。

## まずはレシピ

```ruby:recipes/default.rb
# add mysql yum repository
remote_file "#{chef::config[:file_cache_path]}/mysql-community-release-el6-5.noarch.rpm" do
  source 'http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm'
  action :create
end

rpm_package "mysql-community-release" do
  source "#{chef::config[:file_cache_path]}/mysql-community-release-el6-5.noarch.rpm"
  action :install
end

# install mysql community server
yum_package "mysql-community-server" do
  action :install
  version "5.6.15-1.el6"
  flush_cache [:before]
end

service "mysqld" do
  supports :status => true, :restart => true, :reload => true
  action [ :enable, :start ]
end

# secure install
root_password = node["mysql"]["root_password"]
execute "secure_install" do
  command "/usr/bin/mysql -u root < #{chef::config[:file_cache_path]}/secure_install.sql"
  action :nothing
  only_if "/usr/bin/mysql -u root -e 'show databases;'"
end

template "#{chef::config[:file_cache_path]}/secure_install.sql" do
  owner "root"
  group "root"
  mode 0644
  source "secure_install.sql.erb"
  variables({
    :root_password => root_password,
  })
  notifies :run, "execute[secure_install]", :immediately
end

# create database
db_name = node["mysql"]["db_name"]
execute "create_db" do
  command "/usr/bin/mysql -u root -p#{root_password} < #{chef::config[:file_cache_path]}/create_db.sql"
  action :nothing
  not_if "/usr/bin/mysql -u root -p#{root_password} -D #{db_name}"
end

template "#{chef::config[:file_cache_path]}/create_db.sql" do
  owner "root"
  group "root"
  mode 0644
  source "create_db.sql.erb"
  variables({
    :db_name => db_name,
  })
  notifies :run, "execute[create_db]", :immediately
end

# create user
user_name     = node["mysql"]["user"]["name"]
user_password = node["mysql"]["user"]["password"]
execute "create_user" do
  command "/usr/bin/mysql -u root -p#{root_password} < #{chef::config[:file_cache_path]}/create_user.sql"
  action :nothing
  not_if "/usr/bin/mysql -u #{user_name} -p#{user_password} -D #{db_name}"
end

template "#{chef::config[:file_cache_path]}/create_user.sql" do
  owner "root"
  group "root"
  mode 0644
  source "create_user.sql.erb"
  variables({
    :db_name => db_name,
    :username => user_name,
    :password => user_password,
  })
  notifies :run, "execute[create_user]", :immediately
end
```

## それからテンプレート

```sql:templates/default/secure_install.sql.erb
-- test データベースが存在したら削除
DROP DATABASE IF EXISTS test;

-- 匿名ユーザの削除
DELETE FROM mysql.user WHERE user = '';

-- root ユーザのパスワードを設定
SET PASSWORD FOR 'root'@'::1'                   = PASSWORD('<%= @root_password %>');
SET PASSWORD FOR 'root'@'127.0.0.1'             = PASSWORD('<%= @root_password %>');
SET PASSWORD FOR 'root'@'localhost'             = PASSWORD('<%= @root_password %>');
SET PASSWORD FOR 'root'@'localhost.localdomain' = PASSWORD('<%= @root_password %>');

-- 権限情報をフラッシュ
FLUSH PRIVILEGES;
```
```sql:templates/default/create_db.sql.erb
CREATE DATABASE <%= @db_name %> CHARACTER SET utf8 COLLATE utf8_general_ci;
```
```sql:templates/default/create_db.sql.erb
GRANT ALL ON <%= @db_name %>.* TO '<%= @username %>'@'localhost' IDENTIFIED BY '<%= @password %>';
FLUSH PRIVILEGES;
```

### 参考サイト

このあたりを参考にさせていただきました。ありがとうございます。多謝。Thank You;).

* [MySQL ::   Using the MySQL Yum Repository :: 1 Installing MySQL on Linux Using the MySQL Yum Repository](http://dev.mysql.com/doc/mysql-repo-excerpt/5.6/en/linux-installation-yum-repo.html)
* [vagrant - Chef correct way to load new rpm and install package - Stack Overflow](http://stackoverflow.com/questions/18174557/chef-correct-way-to-load-new-rpm-and-install-package)
* [Chefのレシピでmysql_secure_installation(bashリソース利用)](http://qiita.com/sawanoboly/items/90d2b038f0f15282b872)