Millard
Millard

Reputation: 1157

Chef-solo and vagrant to automatically run an MySql script after install?

Im pretty new to vagrant and chef so this may very well be an easy question.

I have managed to get chef to install MySql, PHP etc

After this I need the VM to run about 3 SQL scripts, is this possible using chef or is it something I should move into a shell script and run after initiation?

My Vagrant File -

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

Vagrant.configure("2") do |config|
  config.vm.box = "raring32.box"
  config.vm.box_url = "http://[local network location]/raring32.box"
  config.vm.network :forwarded_port, guest: 80, host: 8888

    config.vm.provision :chef_solo do |chef|
        chef.cookbooks_path = "cookbooks"
        chef.add_recipe "openssl"
        chef.add_recipe "apache2"
        chef.add_recipe "mysql"
        chef.add_recipe "mysql::server"
        chef.add_recipe "php"
        chef.add_recipe "php::module_apc"
        chef.add_recipe "php::module_curl"
        chef.add_recipe "php::module_mysql"
        chef.add_recipe "apache2::mod_php5"
        chef.add_recipe "apache2::mod_rewrite"
        chef.json = {
            "mysql" => {
                "server_root_password" => "hello",
                "server_repl_password" => "hello",
                "server_debian_password" => "hello"
                }
        }   
        chef.add_recipe "Database:Mysql"    
    end
end

Thanks for any help.

Upvotes: 2

Views: 4179

Answers (1)

Adam
Adam

Reputation: 1982

For this, I would recommend using Chef since you are already in the Chef/Vagrant env. No need to create a shell script and trigger it from within Chef - it would go against the intended flow of Chef/Vagrant.

You can do something along the lines of (taken and simplified from the MySQL cookbook by OpsCode in the server.rb recipe):

execute "mysql-install-privileges" do
  command "mysql -u root -p#{node['mysql']['server_root_password']} < /vagrant/command.sql"      
end

You can determine on your own how you want to store the mysql path, username and password. The general idea is that you define your MySQL commands in a .sql file (saved in your templates or file directory within your cookbook) and then run it with plain MySQL commands.

Hope that gets you on the right path.

Upvotes: 7

Related Questions