外部cookbookを使ってmysqlのインストールから、DBの作成までを行います。
database cookbookの取得
- librarian-chefをインストール
gem install librarian-chef
- Cheffileを作成し、設定追加
librarian-chef init
してCheffileを作成後、
cookbook 'database'
を追記
- database cookbookのインストール
librarian-chef install
関連するcookbookもインストールされる。
mysql cookbookの取得
git clone https://github.com/opscode-cookbooks/mysql.git
設定
mysqlのインストールはjsonファイルに下記を追加する。
{
"mysql": {
"version":"5.6"
"server_root_password": "",
},
"run_list": [
"recipe[mysql::server]"
"recipe[mysql::database]"
"recipe[mysql-db]"
]
}
データベースを作成したいときは、recipeファイルにmysql::databaseをインクルードする。以下にコード例を示す。
# database::mysqlレシピを使用する
include_recipe "database::mysql"
# データベース接続に使用するアカウント情報を定義する
# パスワードはJSONファイル内に記述されたものを使用する
connection_info = ({
:host => "localhost",
:username => "root",
:password => ""
})
# 「testdb」データベースを作成する
mysql_database 'testdb' do
connection connection_info
action :create
end
# 「testuser」ユーザーを作成しそのアクセス権とパスワードを設定する
mysql_database_user 'testuser' do
connection connection_info
password 'testpassword'
database_name 'testdb'
host '%'
privileges [:all]
action :grant
end


