Setting up a MySQL database with a user with Vagrant and chef-solo

Thomas picture Thomas · Jun 21, 2012 · Viewed 17.5k times · Source

I'm trying to automate the developer setup on a project, such that a new developer can simply issue vagrant up to get a VM with MySQL and Tomcat with a custom war-file deployed up and running. The deployed-war assumes that a particular database and user are present on the MySQL instance.

Now, I'm using chef-solo, and everything is booting up and installing correctly. However, I can't seem to figure out how to write a cookbook for setting up the database and the user. I've looked into the database cookbook, but that does not seem to fit my use case with chef-solo. I've been unable to find an example setup online that sets up a database for a chef-solo instance, but maybe I'm searching for the wrong terms?

I could write a bash script for setting up the database and user (conditional on them not existing), but that does not seem to fit into the cookbook philosophy. How would you solve this problem?

Answer

Matthias B picture Matthias B · Dec 13, 2012

Yeah you can do it the Chef way just like this with the database cookbook:

include_recipe "database::mysql"

# create a mysql database
mysql_database 'sonar' do
  connection ({:host => "localhost", :username => 'root', :password => node['mysql']['server_root_password']})
  action :create
end

more about this cookbook can be found here: http://community.opscode.com/cookbooks/database