chef:berkshelfの"database"で作成したデータベースの文字コードがlatinになっていた件
chef-solo練習中。そしてberkshelfを利用してみる。
berkshelfの"database"というコミュニティクックブックを入れてみた。
自分で作成したクックブックからそのコミュニティクックブックを利用する形式。
# site-cookbooks/set_database/recipes/default.rb user_data = Chef::EncryptedDataBagItem.load('users', 'xxxxx') include_recipe 'database::mysql' mysql_connection_info = {:host => "localhost", :username => 'root', :password => node['mysql']['server_root_password']} mysql_database "rails" do connection mysql_connection_info action :create end mysql_database_user "xxxxx" do connection mysql_connection_info password user_data['password'] database_name "rails" host "localhost" privileges [:all] action [:create, :grant] end
これだとデータベースの文字コードがlatinになってしまっていた。 適当にググってみたが原因がわからない。
そこでberkshelfの"database"のレシピを読み新規データベース作成部分 を確認。
# cookbooks/database/libraries/provider_database_mysql.rb def action_create unless exists? begin Chef::Log.debug("#{@new_resource}: Creating database `#{new_resource.database_name}`") create_sql = "CREATE DATABASE `#{new_resource.database_name}`" create_sql += " CHARACTER SET = #{new_resource.encoding}" if new_resource.encoding create_sql += " COLLATE = #{new_resource.collation}" if new_resource.collation Chef::Log.debug("#{@new_resource}: Performing query [#{create_sql}]") db.query(create_sql) @new_resource.updated_by_last_action(true) ensure close end end end
new_resourceにencodingが無いとCHARACTER SETしてくれないようなので一行追加した。
# site-cookbooks/set_database/recipes/default.rb mysql_database "rails" do connection mysql_connection_info encoding "utf8" #←これを追加 action :create end
上手くいった。