MongoDB with Mysql and ActiveRecord.

I’ve been playing with mongodb for a little while now and here some basic issues that I faced and some setup help.

Setting up mongo is really straight forward in OS X. You may either download it from here or use homebrew to install it. If your using brew

brew install mongodb

should do it.

Skip over to the bin folder and launch it with

./mongod

You’ll now see that the console is available for access. It can be launched by

./mongo

and you should see a irb like shell.

Using MongoDB with Mysql while continuing to use Mysql as the primary datastore.

I have been looking into Mongo purely because I was told its highly performant but I still need my primary datastore to be mysql because I see the need for transactions in the future. I decided to go with mongoid (was recommended because its maintained and is unlikely to vanish soon) which only require you to add the gems to the Gemfile.

gem "mongoid", "~> 2.3"
gem "bson_ext", "~> 1.4"

# Run bundle install

If you decide to use Mongoid with MongoDB as your primary datastore you would have to follow these steps completely. However, since I needed to still retain my mysql configuration I only ran this step

 rails g mongoid:config

This should generate a mongoid.yml file similar to your database.yml file in the config folder. A very well written blog post on how to setup users on mongodb and maintain it with mongoid is provided here and I strongly recommend reading it.

Running the generator now makes mongo your default datastore and using the generator would build models using mogoid and not ActiveRecord. To ensure that active record remains your primary orm add the following config to your application.rb

config.generators do |gen|
        gen.orm :active_record
    end

Source :http://stackoverflow.com/questions/6372626/using-active-record-generators-after-mongoid-installation

Now you have ActiveRecord as your primary ORM and you may generate models with mongoid when you need it.

Errors when installing MongoDb on Ubuntu

As before you can find the tar ball at the Mongodb downloads page but on installing and running mongo, I encountered this error.

 exception in initAndListen std::exception: dbpath (/data/db/) does not exist, terminating
  dbexit:
 	 shutdown: going to close listening sockets...
 	 shutdown: going to flush oplog...
 	 shutdown: going to close sockets...
 	 shutdown: waiting for fs preallocator...
 	 shutdown: closing all files...
     closeAllFiles() finished
  dbexit: really exiting now

The error says that it can’t find the /data/db folder. So just create with the right user permissions and you should be good to go

Source : http://ronaldbradford.com/blog/mongodb-experience-getting-started-2010-06-09/

Couldn’t create database for {”username”=>”root”, “adapter”=>”mysql”, “database”=>”some_db”, “password”=>”testpassword”, “socket”=>”/tmp/mysql.sock”}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)

I purchased a Macbook pro this week and have been trying to move my stuff from my old Ubuntu machine which completely died on me. Now those who work on linux machines know that setting up your development environment hardly takes any time and after you’ve done it about 50 times its more about going through the commands on your console.

I am not sure if this is the general opinion but I find the key bindings very inconsistent and frankly a pain to keep track of.

Anways getting back to the issue at hand I hit a roadblock while installing Mysql. I chose the 32 bit package installer for OSX and everything seemed smooth and I was able to start the server and connect, but rake db:create through this error, which drove me nuts for almost a day.

Couldn’t create database for {”username”=>”root”, “adapter”=>”mysql”, “database”=>”some_db”, “password”=>”testpassword”,  “socket”=>”/tmp/mysql.sock”}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)

The error speaks very little about what the actual problem is.

Now if your using OS X 10.6 and your current architecture is the 32 bit you are still advised to install the 64 bit OS X installation, to avoid “weird issues”.

To get rid of this issue

  • uninstall your mysql gem
  • stop your mysql server
  • install the 64 bit mysql server
  • start you mysql server
  • reinstall the gem with the 64 bit architecture configurations
  • sudo env ARCHFLAGS=”-arch x86_64″ gem install mysql — –with-mysql-config=/usr/local/mysql/bin/mysql_config

This is should pretty much help you get around it.

More help here  http://weblog.rubyonrails.org/2009/8/30/upgrading-to-snow-leopard and there is a lot of information about this on the rails forum too so check this out too http://railsforum.com/viewtopic.php?id=20371

Hope this helps OSX newbies like me.