%w(mysql-server mysql mysql-devel).each do |pkg|
package pkg do
action :install
end
end
template '/etc/my.cnf' do
source 'my.cnf.erb'
owner 'root'
group 'root'
mode 644
notifies :restart, "service[mysqld]", :delayed
end
service 'mysqld' do
action [:start, :enable]
end
script "Secure_Install" do
interpreter 'bash'
user "root"
only_if "mysql -u root -e 'show databases'"
code <<-EOL
mysqladmin -u root password "your_password"
mysql -u root -pyour_password -e "DELETE FROM mysql.user WHERE User='';"
mysql -u root -pyour_password -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1');"
mysql -u root -pyour_password -e "DROP DATABASE test;"
mysql -u root -pyour_password -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -u root -pyour_password -e "FLUSH PRIVILEGES;"
EOL
end
$ mysql -u root -p -e "show databases"
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
# MySQL-client MySQL-shared-compat MySQL-shared-compat MySQL-devel をインストール
for number in 0..3
cookbook_file "/usr/local/src/#{node[:mysql56][:rpm][:filename][number]}" do
source node[:mysql56][:rpm][:filename][number]
end
package node[:mysql56][:rpm][:package][number] do
action :install
provider Chef::Provider::Package::Rpm
source "/usr/local/src/#{node[:mysql56][:rpm][:filename][number]}"
end
end
link '/etc/my.cnf' do
to '/usr/my.cnf'
end
template '/etc/my.cnf' do
source 'my.cnf.erb'
owner 'root'
group 'root'
notifies :restart, "service[mysql]", :delayed
end
# MySQL-server をインストール
cookbook_file "/usr/local/src/#{node[:mysql56][:rpm][:filename][4]}" do
source node[:mysql56][:rpm][:filename][4]
end
package node[:mysql56][:rpm][:package][4] do
action :install
provider Chef::Provider::Package::Rpm
source "/usr/local/src/#{node[:mysql56][:rpm][:filename][4]}"
end
directory '/var/run/mysqld' do
owner 'mysql'
group 'root'
mode 0755
action :create
end
service 'mysql' do
action [:start, :enable]
end
script "Secure_Install" do
interpreter 'bash'
user "root"
not_if "mysql -u root -pyour_password -e 'show databases'"
code <<-EOL
export Initial_PW=`head -n 1 /root/.mysql_secret |awk '{print $(NF - 0)}'`
mysql -u root -p${Initial_PW} --connect-expired-password -e "SET PASSWORD FOR root@localhost=PASSWORD('your_password');"
mysql -u root -pyour_password -e "SET PASSWORD FOR root@'127.0.0.1'=PASSWORD('your_password');"
mysql -u root -pyour_password -e "DELETE FROM mysql.user WHERE User='';"
mysql -u root -pyour_password -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1');"
mysql -u root -pyour_password -e "DROP DATABASE test;"
mysql -u root -pyour_password -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';"
mysql -u root -pyour_password -e "FLUSH PRIVILEGES;"
EOL
end