ポイントは以下の3つでした。
1. Vagrantfileで":forwarded_port"の設定をします。ホストの3366がゲストの3306を指すようになります。
config.vm.network :forwarded_port, guest: 3306, host: 33662. "/etc/mysql/my.cnf"の"bind-address"の設定をします。ゲストのMySQLにリモートからのアクセスを許可させます。
bind-address = 0.0.0.03. SQLを実行します。rootユーザがリモートからアクセスできるようになります。
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY 'xxxxxxxxxx' WITH GRANT OPTION2と3を反映したChefのレシピは、以下のようになりました。
* node['mysql']['password'] はVagrantfileの"chef.json"に書きます。レシピに直書きでもOKです。
# MySQLのインストール
package 'mysql-server' do
action :install
notifies :run, 'execute[mysqladmin]'
notifies :run, 'execute[mysql]'
end
# パスワードの変更(初回のみ実行される)
execute 'mysqladmin' do
action :nothing
command 'mysqladmin password -u root ' + node['mysql']['password']
end
# リモート(ホスト)からの接続を許可(初回のみ実行される)
execute 'mysql' do
action :nothing
command "mysql -u root -p#{node['mysql']['password']} -e \"GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '#{node['mysql']['password']}' WITH GRANT OPTION\""
end
# サービスを有効にしてスタート
service 'mysql' do
supports :status => true, :restart => true, :reload => true
action [:enable, :start]
end
# 前述の"bind-address"の設定を反映する
template '/etc/mysql/my.cnf' do
notifies :restart, 'service[mysql]'
end
適当なGUIクライアントソフトからlocalhost:3366にrootユーザで接続できればOKです。(ほんとは専用のユーザを用意した方が良いんだろうけど。)
No comments:
Post a Comment