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

ChefでCentOS6.5にMySQLを入れる

More than 5 years have passed since last update.

Chefで管理しているレポジトリにMySQLを入れたので、ひと通りの手順を記しておく。

やりたいこと

ChefでCentOS 6.5にMySQLを入れ、サービスとして起動する

セットアップ

  • ゲストOS: CentOS 6.5 (vagrant)

boxはこれを使用

  • ホストOSにchefとknifeを入れておく

MySQL用のcookbook作成

自作cookbookは./site-cookbookで管理する

$ knife cookbook create mysql -o ./site-cookbooks

Berksfileを編集

yum-epelとmysqlのcookbookのパス指定を追加

Berksfile
cookbook "yum-epel"
cookbook "mysql", path: "./site-cookbooks/mysql"

Attributeにデフォルト値を設定

Vagrantではデフォルトのユーザがvagrantになる

site-cookbooks/mysql/attributes/default.rb
default['mysql']['user'] = 'vagrant'
default['mysql']['group'] = 'vagrant'
default['mysql']['server_root_password'] = ''

recipeを作成

  • mysqlとmysql-serverのパッケージをインストール
  • mysqlをサービスとして起動
  • mysqlにrootユーザのパスワードを設定
site-cookbooks/mysql/recipes/default.rb
%w{mysql mysql-server}.each do |pkg|
  package pkg do
    action :install
  end
end

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

execute "set root password" do
  command "mysqladmin -u root password #{node['mysql']['server_root_password']}"
  only_if "mysql -u root -e 'show databases;'"
end

Vagrantfileを編集

Vagranftile
config.vm.provision "chef_solo" do |chef|
  chef.cookbook_path = ["./cookbooks", "./site-cookbooks"]
  chef.run_list = %w[
    recipe[yum-epel]
    recipe[mysql]
  ]
  chef.json = {
    mysql: {
      server_root_password: 'rootpass'
    }
  }
end

サーバをプロビジョニングしてみる

$ berks vendor ./cookbooks
$ vagrant provision

と、ここで問題のエラーが発生。Chefから出力されているスタックトレースは以下

略
================================================================================
==> default: Error executing action `install` on resource 'package[mysql]'
==> default: ================================================================================
==> default:
==> default:
==> default: Chef::Exceptions::Exec
==> default: ----------------------
==> default:  returned 1, expected 0
==> default:
==> default:
==> default: Resource Declaration:
==> default: ---------------------
==> default: # In /tmp/vagrant-chef-3/chef-solo-2/cookbooks/mysql/recipes/default.rb
  1 #
略
==> default:
==> default: [2014-11-11T12:11:54+00:00] INFO: Running queued delayed notifications before re-raising exception
==> default: [2014-11-11T12:11:54+00:00] ERROR: Running exception handlers
==> default: [2014-11-11T12:11:54+00:00] ERROR: Exception handlers complete
==> default: [2014-11-11T12:11:54+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2014-11-11T12:11:54+00:00] ERROR: package[mysql] (mysql::default line 11) had an error: Chef::Exceptions::Exec:  returned 1, expected 0
==> default: [2014-11-11T12:11:54+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.

ゲスト側で直接MySQLを入れてみる

Vagrantにsshログインして、直接mysqlパッケージのインストールを試みる

$ vagrant ssh
Last login: Tue Nov 11 12:00:56 2014
[vagrant@vagrant-centos65 ~]$ sudo yum install mysql

が、ここでもやはりダウンロードが出来ず。エラーメッセージは以下。

略
Downloading Packages:
Running rpm_check_debug
ERROR with rpm_check_debug vs depsolve:
libmysqlclient.so.16()(64bit) is needed by postfix-2:2.6.6-6.el6_5.x86_64
libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by postfix-2:2.6.6-6.el6_5.x86_64
 You could try running: rpm -Va --nofiles --nodigest
Your transaction was saved, rerun it with: yum load-transaction /tmp/yum_save_tx-2014-11-11-12-28pc8O57.yumtx

どうやらmysqlが依存しているライブラリと衝突している模様。libmysqlclientというのは、chefで入れようとしているmysqlパッケージに含まれるものな気がしたので、postfix-2というやつが原因じゃないかと推測。

postfixパッケージを消してみる

## ライブラリ名を調べる

[vagrant ~]$ yum list | grep postfix
Failed to set locale, defaulting to C
postfix.x86_64                         2:2.6.6-2.2.el6_1              @anaconda-CentOS-201311272149.x86_64/6.5
postfix.x86_64                         2:2.6.6-6.el6_5                base
postfix-perl-scripts.x86_64            2:2.6.6-6.el6_5                base
spamass-milter-postfix.noarch          0.3.2-3.el6                    epel

## postfix.x86_64を消す

[vagrant@vagrant-centos65 ~]$ sudo yum remove postfix.x86_64
略
Removed:
  postfix.x86_64 2:2.6.6-2.2.el6_1

Dependency Removed:
  cronie.x86_64 0:1.4.4-12.el6        cronie-anacron.x86_64 0:1.4.4-12.el6        crontabs.noarch 0:1.10-33.el6

Complete!

ホスト側で再度プロビジョンしてみる

$ vagrant provision
略
==> default: [2014-11-11T12:32:08+00:00] INFO: package[mysql] installing mysql-5.5.40-1.el6.remi from remi repository
==> default: [2014-11-11T12:32:12+00:00] INFO: package[mysql-server] installing mysql-server-5.5.40-1.el6.remi from remi repository
==> default: [2014-11-11T12:33:01+00:00] INFO: service[mysqld] enabled
==> default: [2014-11-11T12:33:03+00:00] INFO: service[mysqld] started
==> default: [2014-11-11T12:33:03+00:00] INFO: execute[set root password] ran successfully
==> default: [2014-11-11T12:33:03+00:00] INFO: Chef Run complete in 58.23973756 seconds
==> default: [2014-11-11T12:33:03+00:00] INFO: Running report handlers
==> default: [2014-11-11T12:33:03+00:00] INFO: Report handlers complete

できた!

確認

$ vagrant ssh
Last login: Tue Nov 11 12:50:12 2014
[vagrant@vagrant-centos65 ~]$ service --status-all | grep mysql
mysqld (pid 3622) is running...

ちゃんとserviceとして動いている。

ところで

Repro株式会社では一緒に切磋琢磨できる仲間を募集しています。 ぜひこちらをごらんください!

repro
世界59か国6,500以上の導入実績を持つCE(カスタマーエンゲージメント)プラットフォーム「Repro(リプロ)」を提供
https://repro.io/
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