Skip to content

Instantly share code, notes, and snippets.

@naoyes
Last active January 4, 2016 11:29
Show Gist options
  • Select an option

  • Save naoyes/8615303 to your computer and use it in GitHub Desktop.

Select an option

Save naoyes/8615303 to your computer and use it in GitHub Desktop.
ChefでMySQL5.6をインストール ref: http://qiita.com/naoyes/items/a49637bfa4bc6a64b87b
GRANT ALL ON <%= @db_name %>.* TO '<%= @username %>'@'localhost' IDENTIFIED BY '<%= @password %>';
FLUSH PRIVILEGES;
# 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
-- 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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment