Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.
  • 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;).

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした