Installing DBSlayer on Mac OS X Snow Leopard

February 2, 2010

DBSlayer is a tool that will wrap your MySQL database with an HTTP REST/JSON API. Here's how to get it installed on Snow Leopard using Macports. First, make sure you have all the dependencies install via Macports:

$ sudo port install db46
$ sudo port install apr
$ sudo port install apr-util
$ sudo port install mysql5-server

Then, if you try to download the source and install it:

$ cd ~/src
$ wget http://code.nytimes.com/downloads/dbslayer-beta-12.tgz
$ tar xzf
$ cd dbslayer
$ ./configure
$ make
$ make install

You'll run into this error:

ld: library not found for -ldb-4.6
collect2: ld returned 1 exit status
make[1]: *** [dbslayer] Error 1
make: *** [all-recursive] Error 1

Instead, pass these options to configure:

$ ./configure CFLAGS="-L/opt/local/lib/db46" \
--with-apr-1-config=/opt/local/bin/apr-1-config \
--with-apu-1-config=/opt/local/bin/apu-1-config \
--with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

Now try to install again:

$ make clean
$ make
$ sudo make install

Next, create the config file, which at it's most basic, should look something like this:

[my_db]
database=my_db
host=localhost
user=root

Now when you start dbslayer, make sure to give it the full path to the config file:

$ dbslayer -c ~/stuff/my_db.cnf -s my_db

dbslayer starts in the background, so to restart it, you have to find the process and kill it manually. It also doesn't say whether it worked or not, it just backgrounds with no output. Here's a little Ruby script to try it out:

require 'open-uri'
require 'rubygems'
require 'json'
require 'pp'

def query_url(sql)
  query_hash = { "SQL" => sql }
  url_args = URI.encode(query_hash.to_json)
  "http://localhost:9090/db?#{url_args}"
end

def exec_query(sql)
  url = query_url(sql)
  open(url) do |f|
    yield JSON.parse(f.read)
  end
end

exec_query "select * from stuff limit 1" do |res|
  pp res
end

Posted in Technology | Tags MacOSX, MySQL, REST, SnowLeopard, DBSlayer | 6 Comments