我是红宝石和厨师的新人。我建立了一个厨师服务器并成功引导节点。我想通过我的菜谱访问节点上的Mysql,没有Mysql和数据库cookbook。我使用ruby / mysql API。在单个ruby文件中编程时,它运行良好。代码如下。
require "rubygems"
require "mysql"
begin
con = Mysql.new("localhost", "root", "123")
res = con.query("create database abc")
rescue Mysql::Error => e
puts e.errno
puts e.error
ensure
con.close if con
end
但是当我将这段代码添加到我的cookbook和chef-client中的ruby_block时,我在节点上收到了错误。
================================================================================
Error executing action `run` on resource 'ruby_block[test1]'
================================================================================
LoadError
---------
cannot load such file -- mysql
Cookbook Trace:
---------------
/var/chef/cache/cookbooks/db_test/recipes/default.rb:14:in `block (2 levels) in from_file'
Resource Declaration:
---------------------
# In /var/chef/cache/cookbooks/db_test/recipes/default.rb
11: ruby_block "test1" do
12: block do
13: require "rubygems"
14: require "mysql"
15:
16: begin
17: con = Mysql.new("localhost", "root", "123")
18: res = con.query("create database abc")
19: rescue Mysql::Error => e
20: puts e.errno
21: puts e.error
22: ensure
23: con.close if con
24: end
25: end
26: # action :run
Compiled Resource:
------------------
# Declared in /var/chef/cache/cookbooks/db_test/recipes/default.rb:11:in `from_file'
ruby_block("test1") do
action "run"
retries 0
retry_delay 2
default_guard_interpreter :default
block_name "test1"
declared_type :ruby_block
cookbook_name "db_test"
recipe_name "default"
block #<Proc:0x000000051907a8@/var/chef/cache/cookbooks/db_test/recipes/default.rb:12>
end
Running handlers:
[2015-04-07T22:14:12-04:00] ERROR: Running exception handlers
Running handlers complete
[2015-04-07T22:14:12-04:00] ERROR: Exception handlers complete
[2015-04-07T22:14:12-04:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
Chef Client failed. 0 resources updated in 0.962999999 seconds
[2015-04-07T22:14:12-04:00] ERROR: ruby_block[test1] (db_test::default line 11) had an error: LoadError: cannot load such file -- mysql
[2015-04-07T22:14:12-04:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
我无法找出问题的原因。任何人都可以帮忙吗?
答案 0 :(得分:1)
Chef在自己的ruby实例中运行(假设您已使用omnibus安装)。您需要使用chef_gem资源将mysql gem安装到Chef ruby中。然后你应该能够加载mysql ruby
答案 1 :(得分:0)
在需要声明之前,在块的顶部添加chef_gem "mysql2"
。