16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ChefでCentOS7にMySQLを導入する方法

Last updated at Posted at 2015-07-16

概要

Chef デビューを果たしたので、MySQL を CentOS 7.1 に入れてみようと思い、knife cookbook create mysql -o site-cookbooksで cookbook を作り、下記の通りの recipe を書いてみました。

site-cookbooks/mysql/recipes/default.rb
package 'mysql-server' do
  action :install
end

service 'mysql' do
  action [:enable, :start]
end

すると、

Recipe: mysql::default
  * yum_package[mysql-server] action install
    * No candidate version available for mysql-server
    ================================================================================
    Error executing action `install` on resource 'yum_package[mysql-server]'
    ================================================================================

    Chef::Exceptions::Package
    -------------------------
    No candidate version available for mysql-server

    Resource Declaration:
    ---------------------
    # In /home/vagrant/chef-solo/cookbooks-3/mysql/recipes/default.rb

     10: package 'mysql-server' do
     11:   action :install
     12: end
     13:

    Compiled Resource:
    ------------------
    # Declared in /home/vagrant/chef-solo/cookbooks-3/mysql/recipes/default.rb:10:in `from_file'

    yum_package("mysql-server") do
      action [:install]
      retries 0
      retry_delay 2
      default_guard_interpreter :default
      package_name "mysql-server"
      flush_cache {:before=>false, :after=>false}
      declared_type :package
      cookbook_name :mysql
      recipe_name "default"
    end

というエラーでお亡くなりになってしまいました……。

原因

どうやら CentOS7では、標準リポジトリから MySQL のインストールが出来ないようです。
そのため、yum repository に MySQL のリポジトリを追加する必要があります。

ちなみに、CentOS7からは、MariaDB が標準でインストールできるようになっているそうです。
MySQL から MariaDB へ乗り換えろという CentOS からのメッセージですね。

対応策

CentOS7 用の rpm を追加するレシピを追加すればインストールができるようになります。

site-cookbooks/mysql/recipes/default.rb
remote_file "#{Chef::Config[:file_cache_path]}/mysql-community-release-el7-5.noarch.rpm" do
  source 'http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm'
  action :create
end

rpm_package 'mysql-community-release' do
  source "#{Chef::Config[:file_cache_path]}/mysql-community-release-el7-5.noarch.rpm"
  action :install
end

Chef::Config[:file_cache_path]にはファイルキャッシュとして使用するディレクトリのパス名が格納されているそうです。

結果

完成したレシピは以下のとおりです。

site-cookbooks/mysql/recipes/default.rb
remote_file "#{Chef::Config[:file_cache_path]}/mysql-community-release-el7-5.noarch.rpm" do
  source 'http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm'
  action :create
end

rpm_package 'mysql-community-release' do
  source "#{Chef::Config[:file_cache_path]}/mysql-community-release-el7-5.noarch.rpm"
  action :install
end

package 'mysql-server' do
  action :install
end

service 'mysqld' do
  action [:enable, :start]
end

それでは実行してみます!

Recipe: mysql::default
  * remote_file[/var/chef/cache/mysql-community-release-el7-5.noarch.rpm] action create (up to date)
  * rpm_package[mysql-community-release] action install
    - install version el7-5 of package mysql-community-release
  * yum_package[mysql-server] action install[2015-07-16T10:30:52+00:00] WARN: yum_package[mysql-server] matched multiple Provides for mysql-server but we can only use the first match: mysql-community-server. Please use a more specific version.

    - install version 5.6.25-2.el7 of package mysql-community-server
  * service[mysqld] action enable (up to date)
  * service[mysqld] action start
    - start service service[mysqld]

無事、CentOS7 に MySQL が導入できました!

参考

16
14
0

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
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?