Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use vagrant to provision simple mysql setup (with shell provisioner)

It's a breeze to provision Vagrant to echo "hello world". Heck, even I can handle that!

But when it comes to doing things a user would actually like to do when provisioning a box, such as installing mysql, creating a database, and creating a mysql user, well... those things just don't seem to work. ARRG.

I'm working with a virtual CentOS server and installing MySQL goes off without a hitch, but that's about where I meet the proverbial brick wall. Bear in mind, I can do all these things easily by SSH'ing into the box (but of course that defeats the purpose of provisioning):

echo "hello world"; <-- good

sudo yum install mysql-server -y; <-- good

sudo /sbin/service mysqld start; <-- good

sudo mysql create user 'mycooluser'@'%' identified by 'mypassword'; <-- BAD

I've been Googling all over the place and the closest I've come is this article, but since it is based on Ubuntu rather than CentOS, I got stuck right away since apparently yum doesn't know what debconf-utils is, while apt does.

Can anyone shed some light on this for me? I don't think mine is an outrageous use-case, all I want to do is have the shell provisioner

  1. install mysql
  2. create database
  3. create user and grant him all privileges
  4. run db_setup.sql to add table structure and data to new database

Please don't tell me to "use chef or puppet". I've already tried and they had their own sets of issues and honestly feel like overkill for these simple requirements. Surely these items can be accomplished with a shell provisioner?

Thanks a lot for any insight. You may be able to tell I'm at my wit's end.

like image 291
Brian FitzGerald Avatar asked Jan 09 '23 23:01

Brian FitzGerald


1 Answers

Here is my Vagrantfile:

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

VAGRANTFILE_API_VERSION = "2"

$script = <<SCRIPT
    set -x
    echo I am provisioning...
    date > /etc/vagrant_provisioned_at
    echo "hello world"
    yum install mysql-server -y
    /sbin/service mysqld start
    mysql -e "create user 'mycooluser'@'%' identified by 'mypassword'"

SCRIPT
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "chef/centos-6.5"
  config.vm.provision :shell, :inline => $script

end

vagrant up should get you a centos-6.5 OS with mysql and the user 'mycooluser'.

To verify:

vagrant ssh to the box after it has finished provisioning and perform the following within the centos guest OS.

mysql -uroot

mysql> SELECT User FROM mysql.user;
+------------+
| User       |
+------------+
| mycooluser |
| root       |
|            |
| root       |
|            |
| root       |
+------------+
6 rows in set (0.00 sec)

Note: You will probably want to grant privileges for 'mycooluser' in order to login using the mysql shell etc.

More details can be found here.

like image 100
nikhil Avatar answered Jan 20 '23 00:01

nikhil