Reputation: 508
I have been trying to get my head around the changes in the chef mysql cookbook (https://github.com/chef-cookbooks/mysql) between version 5.x and version 6.x, and I am struggling a little bit.
I have it successfully installing, but I don't understand how to use the default my.cnf template provided by the cookbook. Because it's in a different cookbook, my database recipe can't find it. I tried copying it to my cookbook, but when I do that the @config variable is not initialized, so it has a fatal error.
Here's what my database recipe looks like at the moment (basically the example provided in the README):
mysql_service 'default' do
port '3306'
version '5.5'
initial_root_password 'change me'
action [:create, :start]
end
mysql_config 'default' do
source 'my.cnf.erb'
notifies :restart, 'mysql_service[default]'
action :create
end
Following the suggestions in the replies, I now have this for my mysql_config:
mysql_config 'default' do
source 'my.cnf.erb'
cookbook 'mysql'
variables :config => {
:name => "mysql",
:port => 3306,
:user => "mysql"
},
:pid_file => "/var/run/mysqld/mysqld.pid",
:socket_file => "/var/run/mysqld/mysqld.sock",
:include_dir => "/etc/mysql/conf.d/"
notifies :restart, 'mysql_service[default]'
action :create
end
which returns this error:
Chef::Mixin::Template::TemplateError (undefined method `name' for {:name=>"mysql", :port=>3306, :user=>"mysql"}:Hash) on line #1:
1: # Chef generated my.cnf for instance mysql-<%= @config.name %>
2:
3: [client]
4: <% if @config.charset %>
5: default-character-set = <%= @config.charset %>
I also placed my cookbooks for this minimal example up in github: https://github.com/rhuffstedtler/chef-mysql-database-example
Upvotes: 3
Views: 1990
Reputation: 63
Yes you can change the my.cnf, but you have to pass the variables to mysql_service and you don't have to use mysql_config. (See below)
After having the same problems I found this in the mysql cookbook folder mysql/libraris/rsource_mysql_sercie.rb
require 'chef/resource/lwrp_base'
class Chef
class Resource
class MysqlService < Chef::Resource::LWRPBase
self.resource_name = :mysql_service
actions :create, :delete, :start, :stop, :restart, :reload
default_action :create
attribute :charset, kind_of: String, default: 'utf8'
attribute :data_dir, kind_of: String, default: nil
attribute :initial_root_password, kind_of: String, default: 'ilikerandompasswords'
attribute :instance, kind_of: String, name_attribute: true
attribute :package_action, kind_of: Symbol, default: :install
attribute :package_name, kind_of: String, default: nil
attribute :package_version, kind_of: String, default: nil
attribute :bind_address, kind_of: String, default: nil
attribute :port, kind_of: String, default: '3306'
attribute :run_group, kind_of: String, default: 'mysql'
attribute :run_user, kind_of: String, default: 'mysql'
attribute :socket, kind_of: String, default: nil
attribute :version, kind_of: String, default: nil
end
end
end
-> So if you want to change things in the instance/my.cnf file, you have to use the parameters of the mysql_service definition for example like this:
mysql_service 'default' do
version '5.5'
port '3306'
socket '/run/mysqld/mysqld.sock'
initial_root_password "#{node['mysql']['server_root_password']}"
action [:create, :start]
end
Hope this helps.
Cheers, Roger
Upvotes: 1
Reputation: 4223
The template
resource has an attribute cookbook
which tells it which cookbook to look in. As for the @config
being initialized, it is also your responsibility to pass that value into the mysql_config
resource. Furthermore, if you look at the template, it requires a few other variables too. Several of those variables are optional, as you can see from the template code. So you'll want something like this:
mysql_config 'default' do
source 'my.cnf.erb'
cookbook 'mysql'
variables :config => <some config hash>,
:pid_file => <your pid file path>,
:socket_file => <which socket to use>,
.....
:temp_dir => <you get the idea>
notifies :restart, 'mysql_service[default]'
action :create
end
Upvotes: 0