【问题标题】:Install mysql cookbook with Vagrant and Chef Solo使用 Vagrant 和 Chef Solo 安装 mysql 食谱
【发布时间】:2015-03-13 09:50:21
【问题描述】:

我很难弄清楚如何安装 mysql 食谱 (https://supermarket.chef.io/cookbooks/mysql) 我已按照说明进行操作,但这是我第一次使用 Vagrant 和 Chef,安装配方时一直卡住

这是我的流浪文件: http://pastebin.com/9Fgts4J6

这是我的厨师文件(我使用图书管理员): http://pastebin.com/EfxPrC9S

这是我的自定义配方“mysql_config”,位于 my_cookbooks/mysql_config/recipes/default.rb: http://pastebin.com/Zh5Y4z8b

这是我遇到的错误:

==> default: ================================================================================
==> default: Error executing action `create` on resource 'template[default :create /etc/mysql-default/conf.d/default.cnf]'
==> default: ================================================================================
==> default:
==> default: Chef::Mixin::Template::TemplateError
==> default: ------------------------------------
==> default: undefined method `name' for nil:NilClass

这是我运行 vagrant up 时的控制台输出: http://pastebin.com/ekkhHHru

好像我遗漏了一些配置,但我不知道该放在哪个以及放在哪里,我已经搜索了教程,但是 mysql_cookbook 和 vagrant 最近变化很大,所以我找不到任何好的那些。对此的任何帮助将不胜感激

流浪文件:

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|
  config.vm.box = "puphpet/debian75-x64"

  config.vm.synced_folder "../."

  config.vm.provision "shell" do |s|
    s.path = "provision/setup.sh"
  end

  config.vm.provision "chef_solo" do |chef|
    chef.cookbooks_path = ["./cookbooks", "./my_cookbooks"]
    chef.add_recipe "mysql_config"
    chef.json = {
      "mysql_config" => {
        "name" => "localhost"
      }
    }
  end

end

自定义配方:

mysql_service 'default' do
  port '3306'
  version '5.5'
  name 'localhost'
  initial_root_password 'p4ssw0rd'
  action [:create, :start]
end

mysql_config 'default' do
  source 'my.cnf.erb'
  notifies :restart, 'mysql_service[default]'
  action :create
end

我的控制台的输出:

iMacBert:Vagrant bert$ vagrant reload --provision
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'puphpet/debian75-x64' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/bert/Vagrant
    default: /tmp/vagrant-chef-3/chef-solo-1/cookbooks => /Users/bert/Vagrant/cookbooks
    default: /tmp/vagrant-chef-3/chef-solo-2/cookbooks => /Users/bert/Vagrant/my_cookbooks
==> default: Running provisioner: shell...
    default: Running: /var/folders/y5/hhjgvl5j22g54t22yw8g7mbm0000gp/T/vagrant-shell20150108-3959-gkjuye.sh
==> default: stdin: is not a tty
==> default: Provisioning virtual machine...
==> default: Running provisioner: chef_solo...
    default: Installing Chef (latest)...
Generating chef JSON and uploading...
==> default: Running chef-solo...
==> default: stdin: is not a tty
==> default: [2015-01-08T07:40:57+00:00] INFO: Forking chef instance to converge...
==> default: [2015-01-08T07:40:57+00:00] INFO: *** Chef 12.0.3 ***
==> default: [2015-01-08T07:40:57+00:00] INFO: Chef-client pid: 6289
==> default: [2015-01-08T07:40:58+00:00] INFO: Setting the run_list to ["recipe[mysql_config]"] from CLI options
==> default: [2015-01-08T07:40:58+00:00] INFO: Run List is [recipe[mysql_config]]
==> default: [2015-01-08T07:40:58+00:00] INFO: Run List expands to [mysql_config]
==> default: [2015-01-08T07:40:58+00:00] INFO: Starting Chef Run for packer-virtualbox-iso.vagrantup.com
==> default: [2015-01-08T07:40:58+00:00] INFO: Running start handlers
==> default: [2015-01-08T07:40:58+00:00] INFO: Start handlers complete.
==> default: [2015-01-08T07:41:27+00:00] INFO: service[localhost :create mysql] stopped
==> default: [2015-01-08T07:41:27+00:00] INFO: file[localhost :create /etc/mysql/my.cnf] backed up to /var/chef/backup/etc/mysql/my.cnf.chef-20150108074127.236557
==> default: [2015-01-08T07:41:27+00:00] INFO: file[localhost :create /etc/mysql/my.cnf] deleted file at /etc/mysql/my.cnf
==> default: [2015-01-08T07:41:27+00:00] INFO: link[localhost :create /usr/share/my-default.cnf] created
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost] created directory /etc/mysql-localhost
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost] owner changed to 105
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost] group changed to 107
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost] mode changed to 750
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost/conf.d] created directory /etc/mysql-localhost/conf.d
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost/conf.d] owner changed to 105
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost/conf.d] group changed to 107
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /etc/mysql-localhost/conf.d] mode changed to 750
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /run/mysql-localhost] created directory /run/mysql-localhost
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /run/mysql-localhost] owner changed to 105
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /run/mysql-localhost] group changed to 107
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /run/mysql-localhost] mode changed to 755
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/log/mysql-localhost] created directory /var/log/mysql-localhost
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/log/mysql-localhost] owner changed to 105
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/log/mysql-localhost] group changed to 107
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/log/mysql-localhost] mode changed to 750
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/lib/mysql-localhost] created directory /var/lib/mysql-localhost
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/lib/mysql-localhost] owner changed to 105
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/lib/mysql-localhost] group changed to 107
==> default: [2015-01-08T07:41:27+00:00] INFO: directory[localhost :create /var/lib/mysql-localhost] mode changed to 750
==> default: [2015-01-08T07:41:27+00:00] INFO: template[localhost :create /etc/mysql-localhost/my.cnf] created file /etc/mysql-localhost/my.cnf
==> default: [2015-01-08T07:41:27+00:00] INFO: template[localhost :create /etc/mysql-localhost/my.cnf] updated file contents /etc/mysql-localhost/my.cnf
==> default: [2015-01-08T07:41:27+00:00] INFO: template[localhost :create /etc/mysql-localhost/my.cnf] owner changed to 105
==> default: [2015-01-08T07:41:27+00:00] INFO: template[localhost :create /etc/mysql-localhost/my.cnf] group changed to 107
==> default: [2015-01-08T07:41:27+00:00] INFO: template[localhost :create /etc/mysql-localhost/my.cnf] mode changed to 600
==> default: [2015-01-08T07:41:27+00:00] INFO: bash[localhost :create initialize mysql database] ran successfully
==> default: [2015-01-08T07:41:27+00:00] INFO: bash[localhost :create initialize mysql database] sending run action to bash[localhost :create initial records] (delayed)
==> default: [2015-01-08T07:41:30+00:00] INFO: bash[localhost :create initial records] ran successfully
==> default: [2015-01-08T07:41:30+00:00] INFO: template[localhost :start /etc/init.d/mysql-localhost] created file /etc/init.d/mysql-localhost
==> default: [2015-01-08T07:41:30+00:00] INFO: template[localhost :start /etc/init.d/mysql-localhost] updated file contents /etc/init.d/mysql-localhost
==> default: [2015-01-08T07:41:30+00:00] INFO: template[localhost :start /etc/init.d/mysql-localhost] owner changed to 0
==> default: [2015-01-08T07:41:30+00:00] INFO: template[localhost :start /etc/init.d/mysql-localhost] group changed to 0
==> default: [2015-01-08T07:41:30+00:00] INFO: template[localhost :start /etc/init.d/mysql-localhost] mode changed to 755
==> default: [2015-01-08T07:41:30+00:00] INFO: service[localhost :start mysql-localhost] enabled
==> default: [2015-01-08T07:41:32+00:00] INFO: service[localhost :start mysql-localhost] started
==> default: [2015-01-08T07:41:32+00:00] INFO: directory[default :create /etc/mysql-default/conf.d] created directory /etc/mysql-default/conf.d
==> default: [2015-01-08T07:41:32+00:00] INFO: directory[default :create /etc/mysql-default/conf.d] owner changed to 105
==> default: [2015-01-08T07:41:32+00:00] INFO: directory[default :create /etc/mysql-default/conf.d] group changed to 107
==> default: [2015-01-08T07:41:32+00:00] INFO: directory[default :create /etc/mysql-default/conf.d] mode changed to 750
==> default: 
==> default: ================================================================================
==> default: Error executing action `create` on resource 'template[default :create /etc/mysql-default/conf.d/default.cnf]'
==> default: ================================================================================
==> default: 
==> default: Chef::Mixin::Template::TemplateError
==> default: ------------------------------------
==> default: undefined method `name' for nil:NilClass
==> default: 
==> default: Resource Declaration:
==> default: ---------------------
==> default: 
==> default: # In /tmp/vagrant-chef-3/chef-solo-1/cookbooks/mysql/libraries/provider_mysql_config.rb
==> default: 
==> default: 
==> default:  38:         template "#{new_resource.name} :create #{include_dir}/#{new_resource.config_name}.cnf" do
==> default:  39:           path "#{include_dir}/#{new_resource.config_name}.cnf"
==> default:  40:           owner new_resource.owner
==> default:  41:           group new_resource.group
==> default: 
==> default:  42:           mode '0640'
==> default:  43:           variables(new_resource.variables)
==> default:  44:           source new_resource.source
==> default:  45:           cookbook new_resource.cookbook
==> default: 
==> default:  46:           action :create
==> default:  47:         end
==> default: 
==> default:  48:       end
==> default: 
==> default: 
==> default: Compiled Resource:
==> default: ------------------
==> default: # Declared in /tmp/vagrant-chef-3/chef-solo-1/cookbooks/mysql/libraries/provider_mysql_config.rb:38:in `block in <class:MysqlConfig>'
==> default: 
==> default: template("default :create /etc/mysql-default/conf.d/default.cnf") do
==> default:   action [:create]
==> default:   retries 0
==> default:   retry_delay 2
==> default: 
==> default:   default_guard_interpreter :default
==> default:   path "/etc/mysql-default/conf.d/default.cnf"
==> default:   backup 5
==> default:   atomic_update true
==> default: 
==> default:   source "my.cnf.erb"
==> default: 
==> default:   declared_type :template
==> default:   cookbook_name :mysql_config
==> default:   owner "mysql"
==> default: 
==> default:   group "mysql"
==> default: 
==> default:   mode "0640"
==> default: 
==> default: end
==> default: 
==> default: 
==> default: Template Context:
==> default: -----------------
==> default: on line #1
==> default:   1: # Chef generated my.cnf for instance mysql-<%= @config.name %>
==> default: 
==> default:   2: 
==> default:   3: [client]
==> default: 
==> default:   4: <% if @config.charset %>
==> default:   5: default-character-set          = <%= @config.charset %>
==> default: 
==> default: 
==> default: [2015-01-08T07:41:32+00:00] INFO: Running queued delayed notifications before re-raising exception
==> default: 
==> default: ================================================================================
==> default: Error executing action `create` on resource 'mysql_config[default]'
==> default: ================================================================================
==> default: 
==> default: 
==> default: Chef::Mixin::Template::TemplateError
==> default: ------------------------------------
==> default: undefined method `name' for nil:NilClass
==> default: 
==> default: 
==> default: Resource Declaration:
==> default: ---------------------
==> default: # In /tmp/vagrant-chef-3/chef-solo-2/cookbooks/mysql_config/recipes/default.rb
==> default: 
==> default: 
==> default: 
==> default:  17: mysql_config 'default' do
==> default:  18:   source 'my.cnf.erb'
==> default:  19:   notifies :restart, 'mysql_service[default]'
==> default: 
==> default:  20:   action :create
==> default:  21: end
==> default: 
==> default: 
==> default: 
==> default: Compiled Resource:
==> default: ------------------
==> default: # Declared in /tmp/vagrant-chef-3/chef-solo-2/cookbooks/mysql_config/recipes/default.rb:17:in `from_file'
==> default: 
==> default: mysql_config("default") do
==> default: 
==> default:   action [:create]
==> default:   updated true
==> default:   updated_by_last_action true
==> default:   retries 0
==> default: 
==> default:   retry_delay 2
==> default: 
==> default:   default_guard_interpreter :default
==> default:   declared_type :mysql_config
==> default: 
==> default:   cookbook_name :mysql_config
==> default:   recipe_name "default"
==> default:   source "my.cnf.erb"
==> default:   group "mysql"
==> default:   owner "mysql"
==> default:   instance "default"
==> default: 
==> default:   config_name "default"
==> default: end
==> default: 
==> default: 
==> default: Template Context:
==> default: -----------------
==> default: on line #1
==> default:   1: # Chef generated my.cnf for instance mysql-<%= @config.name %>
==> default:   2: 
==> default: 
==> default:   3: [client]
==> default:   4: <% if @config.charset %>
==> default: 
==> default:   5: default-character-set          = <%= @config.charset %>
==> default: 
==> default: 
==> default: [2015-01-08T07:41:32+00:00] INFO: Running queued delayed notifications before re-raising exception
==> default: [2015-01-08T07:41:32+00:00] ERROR: Running exception handlers
==> default: [2015-01-08T07:41:32+00:00] ERROR: Exception handlers complete
==> default: [2015-01-08T07:41:32+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2015-01-08T07:41:32+00:00] ERROR: 
==> default: 
==> default: Chef::Mixin::Template::TemplateError (undefined method `name' for nil:NilClass) on line #1:
==> default: 
==> default:   1: # Chef generated my.cnf for instance mysql-<%= @config.name %>
==> default:   2: 
==> default:   3: [client]
==> default:   4: <% if @config.charset %>
==> default:   5: default-character-set          = <%= @config.charset %>
==> default: 
==> default:   (erubis):1:in `block in evaluate'
==> default:   /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in `instance_eval'
==> default:   /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/erubis-2.7.0/lib/erubis/evaluator.rb:74:in `evaluate'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/mixin/template.rb:126:in `_render_template'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/mixin/template.rb:112:in `render_template'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider/template/content.rb:43:in `file_for_provider'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/file_content_management/content_base.rb:40:in `tempfile'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider/file.rb:451:in `tempfile'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider/file.rb:337:in `do_generate_content'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider/file.rb:148:in `action_create'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider.rb:145:in `run_action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource.rb:582:in `run_action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:49:in `run_action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:81:in `block (2 levels) in converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:81:in `each'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:81:in `block in converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/resource_list.rb:83:in `block in execute_each_resource'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/resource_list.rb:81:in `execute_each_resource'
==> default:   /opt/chef/embedded/lib/ruby/2.1.0/forwardable.rb:183:in `execute_each_resource'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:80:in `converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider/lwrp_base.rb:61:in `recipe_eval_with_update_check'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider/lwrp_base.rb:45:in `block in action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/provider.rb:145:in `run_action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource.rb:582:in `run_action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:49:in `run_action'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:81:in `block (2 levels) in converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:81:in `each'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:81:in `block in converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/resource_list.rb:83:in `block in execute_each_resource'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:116:in `call'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:116:in `call_iterator_block'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:104:in `iterate'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/resource_collection/resource_list.rb:81:in `execute_each_resource'
==> default:   /opt/chef/embedded/lib/ruby/2.1.0/forwardable.rb:183:in `execute_each_resource'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/runner.rb:80:in `converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/client.rb:315:in `converge'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/client.rb:400:in `block in run'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/client.rb:399:in `catch'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/client.rb:399:in `run'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application.rb:261:in `block in fork_chef_client'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application.rb:249:in `fork'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application.rb:249:in `fork_chef_client'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application.rb:215:in `block in run_chef_client'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/local_mode.rb:38:in `with_server_connectivity'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application.rb:201:in `run_chef_client'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application/solo.rb:245:in `block in interval_run_chef_client'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application/solo.rb:234:in `loop'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application/solo.rb:234:in `interval_run_chef_client'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application/solo.rb:224:in `run_application'
==> default:   /opt/chef/embedded/apps/chef/lib/chef/application.rb:58:in `run'
==> default:   /opt/chef/embedded/apps/chef/bin/chef-solo:25:in `<top (required)>'
==> default:   /usr/bin/chef-solo:40:in `load'
==> default:   /usr/bin/chef-solo:40:in `<main>'
==> default: [2015-01-08T07:41:32+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.
iMacBert:Vagrant bert$ 

【问题讨论】:

  • 有人有想法吗?我已经在上面的问题中包含了代码
  • 有人有机会看看这个吗?乍一看,这是某个地方的错误还是我在设置/工作流程中做错了什么?
  • 我认为,但我真的不确定,您应该从 mysql_config 中删除源以保留默认模板(如果这是您希望的,我可能误解了)。错误在 config.name 可能资源没有正确设置,我不知道为什么。

标签: mysql vagrant chef-recipe chef-solo cookbook


【解决方案1】:

我也有同样的问题。我对 Chef 模板知之甚少,无法理解为什么没有设置 @config,但它绝对是 nil,这就是引发错误的原因。您甚至不能将源选项留空,因为 mysql 食谱会寻找一个不存在的 default.cnf.erb 并抛出一个稍微不同的错误。相反,您必须像 Ernie 所说的那样提供本质上是空的来源。

但是,你有一个完全空的 conf 文件,所以如果我们发现了这个,我们将不得不再次寻找它。相反,我这样做了:

<% if !@config.nil? %>

    <% if @config.name %># Chef generated my.cnf for instance mysql-<%= @config.name %><% end %>
    [client]
    default-character-set          = <%= @config.charset %>
    <% end %>
    <% if @config.port %>
    port                           = <%= @config.port %>
    <% end %>
    <% if @socket_file %>
    socket                         = <%= @socket_file %>
    <% end %>

    [mysql]
    <% if @config.charset %>
    default-character-set          = <%= @config.charset %>
    <% end %>

    [mysqld]
    <% if @config.run_user %>
    user                           = <%= @config.run_user %>
    <% end %>
    <% if @pid_file %>
    pid-file                       = <%= @pid_file %>
    <% end %>
    <% if @socket_file %>
    socket                         = <%= @socket_file %>
    <% end %>
    <% if @config.bind_address %>
    bind-address                   = <%= @config.bind_address %>
    <% end %>
    <% if @config.port %>
    port                           = <%= @config.port %>
    <% end %>
    <% if @data_dir %>
    datadir                        = <%= @data_dir %>
    <% end %>
    <% if @tmp_dir %>
    tmpdir                         = <%= @tmp_dir %>
    <% end %>
    <% if @lc_messages_dir %>
    lc-messages-dir                = <%= @lc_messages_dir %>
    <% end %>
    <% if @error_log %>
    log-error                      = <%= @error_log %>
    <% end %>
    <% if @include_dir %>
    !includedir <%= @include_dir %>
    <% end %>

    [mysqld_safe]
    <% if @socket_file %>
    socket                         = <%= @socket_file %>
    <% end %>
<% end %>

所以现在我们没有收到错误,但是一旦我们弄清楚如何正确设置@config,我们就可以删除这个块并且它与 endblock 匹配

<% if !@config.nil? %>

继续前进!

如果我能弄清楚如何让@config 变量工作,我会更新这个答案。

【讨论】:

    【解决方案2】:

    使用了来自灯具的配置文件: https://github.com/chef-cookbooks/mysql/tree/master/test/fixtures/cookbooks

    现在可以了:

    # /my_cookbooks/mysql_config/recipes/default.rb
    
    mysql_service 'default' do
      version '5.5'
      action [:create, :start]
    end
    
    mysql_config 'hello' do
      instance 'default'
      source 'hello.conf.erb'
      version '5.5'
      action :create
    end
    

    hello.conf.erb 为空

    【讨论】: