Failed to connecto to primary node

If you try to connect to MongoDB Replica Set via MognoID and you get this error message here:

/opt/local/lib/ruby1.9/gems/1.9.1/gems/mongo-1.5.2/lib/mongo/repl_set_connection.rb:165:inconnect': Failed to connect to primary node. (Mongo::ConnectionFailure) from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongo-1.5.2/lib/mongo/repl_set_connection.rb:500:insetup'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongo-1.5.2/lib/mongo/repl_set_connection.rb:144:in initialize' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/config/replset_database.rb:24:innew'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/config/replset_database.rb:24:in configure' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/config.rb:316:inconfigure_databases'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/config.rb:119:in from_hash' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid/config.rb:136:inload!'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/mongoid-2.3.4/lib/mongoid.rb:147:in load!' from /Users/reiz/workspace/versioneye/versioneye/config/application.rb:33:inclass:Application'
from /Users/reiz/workspace/versioneye/versioneye/config/application.rb:18:in <module:Versioneye>' from /Users/reiz/workspace/versioneye/versioneye/config/application.rb:17:in'
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:52:in require' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:52:inblock in '
from /opt/local/lib/ruby1.9/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:49:in tap' from /opt/local/lib/ruby1.9/gems/1.9.1/gems/railties-3.1.0/lib/rails/commands.rb:49:in'
from script/rails 
in require' from script/rails:6:in'

Than you should check out this link here: https://github.com/mongoid/mongoid/issues/1783#issuecomment-4319677Durran Jordan helped me to solve problem. Thx for that!

Rails + MongoDB ReplicaSet Configuration

This article shows how to configure a Ruby on Rails app for a MongoDB ReplicaSet. How to set up a MongoDB ReplicaSet I described here: MongoDB ReplicaSet Tutorial. And how to configure Rails to work together with MongoDB is described here: Rails + MongoDB Quickstart Tutorial.

If you want to connect to a ReplicaSet you just have to change your configuration a little bit. This is how your mongoid.yml file should look like:

production:
  database: mydb_prod
  hosts:
    - - mongonode1:1222
    - - mongonode2:1222
    - - mongonode3:1222
  read: :secondary

“mongonode1”, “mongonode2” and “mongonode3” should be mapped to a real ip address, of course! Usually you do that in “/etc/hosts”.

You don’t have to define a PRIMARY! The driver will figure out which of them is the PRIMARY. I like this! You also don’t need a load balancer. Because all nodes are known by the driver. And so the driver will figure it out.

The property “read” can have 2 values: [“:secondary”, “:primary”]. “:secondary” means that read operations can also be routed to secondary nodes. “:primary” means that all read operations will be routed to the PRIMARY node.

Rails + MongoDB Tutorial. Quickstart.

This is a Quickstart Tutorial for Rails + MongoDB. Ruby on Rails and MongoDB, a document based database, is a pretty good fit. It is very easy to integrate MongoDB with Rails. If you want to know how to install MongoDB, check out this article here: Installing MongoDB on Mac OS X Lion. Or this here: Installing MongoDB on Linux.

I assume you are familiar with Ruby on Rails 🙂

There are several GEMs to interact with MongoDB. I used the MongoID GEM. That worked pretty good for me. Just add this to your Gemfile:

gem 'bson_ext', '1.6.0'  
gem 'mongo', '1.6.0'
gem 'mongoid', '2.4.6'

And run “bundle install” on your project, to load the GEM from the internet. And add the file mongoid.yml to your config folder:

development:
    host: localhost
    database: myapp_dev

test:
    host: localhost
    database: myapp_test

production:
    host: localhost
    database: myapp_prod

This is the file where you are configuring the access to the database. If your mongodb instance is running locale you don’t need any username or password settings. Now you have to load the mongoid.yml file. You can do that by adding this line to your application.rb inside of the “class Application”.

Mongoid.load!("config/mongoid.yml")

Than just add a new Model to your project. For example “User”. That could look like that:

class User
include Mongoid::Document
include Mongoid::Timestamps
field :username, type: String
field :firstname, type: String
field :llastname, type: String
field :email, type: String
filed :age, type: Integer

end

That’s it. You just have to include the Mongoid:Document.

include Mongoid::Document

And define your fields! Now you can create a new user and save it like that:

user = User.new
user.username = "mike"
user.firstname = "Mike"
user.lastname = "Boby"
user.age = 44
user.save

Or make a query like that:

user = User.where( username: "mike")

Check out the Criteria API for MongoID. That is pretty straight forward. Worked pretty well for me.

ie.css isn’t precompiled

If you get this Error Message:

ActionView::Template::Error (blueprint/ie.css isn't precompiled)

Than that means that your ie.css isn’t precompiled 🙂
No. Seriously. If you get this message, probably you are running on production and your css files are not precompiled. Take a deeper look to the Assets Pipeline in Rails 3.1.X. If your css and js files are in the right place, you can fix this error message by executing this command here:

rake assets:precompile

Or with JRuby:

jruby -S rake assets:precompile

That should generate all precompiled css and js files and fix the problem.

ActiveRDF + DBPedia + Ruby on Rails

This article show how to use ActiveRDF in Ruby on Rails to write queries for DBPedia. DBPedia is the semantic version of WikiPedia. Basically the whole knowledge in Wikipedia is available in DBPedia over RDF (Resource Definition Framework). There is a query language for RDF called SPARQL. With SPARQL and RDF you can write queries and fetch data from DBPedia/Wikipedia.

That is pretty awesome. You can see DBPedia/Wikipedia as a big Database or Knowledge base.

ActiveRDF is a Ruby GEM to use RDF in Ruby. To use it in Rails you have to add this two lines to your Gemfile

gem 'activerdf'
gem 'activerdf_sparql'

In you application.rb file you have to add this line pretty much in the top.

require 'active_rdf'

In the same file, in the class part you have to configure the connection pool for RDF.


module TrophicWeb
  class Application < Rails::Application

  adapter = ConnectionPool.add_data_source(:type => :sparql, :results => :sparql_xml, :engine => :virtuoso, :url ="http://dbpedia.org/sparql")
  adapter.enabled = true
  Namespace.register(:dbpedia, "http://dbpedia.org/ontology/")

That’s it for the initialization. Now you can use it in your code. Here is an example how to fetch a species and the corresponding thumbnail uri.

species = RDFS::Resource.new("http://dbpedia.org/resource/Symphurus")
thumbnail = species.dbpedia::thumbnail.uri

Thats it.

Mizuno

Mizuno is Webserver for JRuby on Rails.

I looked for different options to run my JRuby App. After some googleing I found Mizumo. It was very easy to install:

jruby -S gem install mizuno

and very easy to start. Just go into the root directory of your rails app and exec.

mizuno

or if you want to start on Port 80.

sudo mizuno --port 80

Mizuno is the fastest option for Reck applications on JRuby:

Mizuno: 6106.66 req/s (mean) 
Jetty (via jruby-rack): 2011.67 req/s (mean) 
Mongrel: 1479.15 req/sec (mean)