LoginSignup
61
65

More than 5 years have passed since last update.

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

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

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

まずはレシピ

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

それからテンプレート

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;
templates/default/create_db.sql.erb
CREATE DATABASE <%= @db_name %> CHARACTER SET utf8 COLLATE utf8_general_ci;
templates/default/create_db.sql.erb
GRANT ALL ON <%= @db_name %>.* TO '<%= @username %>'@'localhost' IDENTIFIED BY '<%= @password %>';
FLUSH PRIVILEGES;

参考サイト

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

61
65
1

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
61
65