Question

I have 2 rackspace servers.

I'm trying to connect to my database with mongoose like (in my app.js on [Server 1]):

// DB
var mongoose = require('mongoose');
var MongoStore = require('connect-mongo')(express);
var dbSession = 'mongodb://root:password@[Server 2]:27017'
mongoose.createConnection(dbSession);

On [Server 2] in /etc/mongo.conf, the only things that are turned on are:

dbpath=/var/lib/mongodb
logpath=/var/log/mongodb/mongodb.log
logappend=true
# Bind ip for our app server
bind_ip=[Server 1]

How can I: A) Check to see if I'm able to connect to [Server 2] from [Server 1] and actually get it working?

Was it helpful?

Solution

Not sure about your firewall configuration, but you may need to open port 27017 on the mongodb server (Server 2 in your setup). Something like the following:

sudo iptables -A INPUT -p tcp --dport 27017 -s [APP_SERVER IP ADDRESS HERE] -j ACCEPT

If you haven't setup firewall rules yet, I suggest reading the Ubuntu guide for Iptables: https://help.ubuntu.com/community/IptablesHowTo

Also, if you aren't already, you'll probably want to use the private IP address communicate between the servers:

http://www.rackspace.com/knowledge_center/article/using-the-private-ip-address-on-your-cloud-server

UPDATE -- 09/22/2013

I spun up 2 Rackspace servers and performed the steps below to successfully communicate between them.

tl;dr The problem might be the bind_ip parameter in your /etc/mongo.conf file.

If you copy and paste these commands, remove the '[remove this]' piece in the links when installing mongodb.

Server 1:

  • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  • echo 'deb http://[remove this]downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  • apt-get install mongodb-10gen
  • apt-get update
  • apt-get install mongodb-10gen
  • mongo [Server 2 public ip]:27017 // to test connection to Server 2
  • sudo apt-get install python-software-properties python g++ make
  • sudo add-apt-repository ppa:chris-lea/node.js
  • apt-get update
  • apt-get install nodejs=0.10.18-1chl1~precise1
  • npm install mongoose
  • npm install connect-mongo
  • npm install express
  • vim server.js
  • node server.js

Here is the server.js file I used:

var express = require('express')
var mongoose = require('mongoose');
var MongoStore = require('connect-mongo')(express);
var dbSession = 'mongodb://[Server 2]:27017';
mongoose.connect(dbSession);

var Dog = mongoose.model('Dog', {name: String});

var dog = new Dog({name: 'Fido'});
dog.save(function(err){
  if(err){
    console.log(err);
  }
  else{
    console.log('success');
  }
});

Server 2:

  • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  • echo 'deb http://[remove this]downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  • apt-get update
  • apt-get install mongodb-10gen
  • iptables -A INPUT -p tcp --dport 27017 -s [Server 1 private ip] -j ACCEPT
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top