Configuring Fedora and MongoDB
In my last post, I covered installing MongoDB on Fedora. This is another post as much for my reference as for anything else, and I’ll go over here getting set up so that my application successfully uses the MongoDB server.
When I left off last time, I had successfully configured the server to allow me to create documents using the mongo command line utility. So, I created a collection and a document and was ready to access it from my web application. Following the examples in the MongoDB java tutorial, I created the following implementation of my HouseService interface that I had previously hard coded room values into:
public class HouseServiceMongoImpl implements HouseService {
private Mongo _mongoConnection;
private DB _database;
public HouseServiceMongoImpl() {
try {
_mongoConnection = new Mongo( "192.168.2.191" , 27017 );
_database = _mongoConnection.getDB("daedalus");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public House getHouse() {
DBCollection myCollection = _database.getCollection("house");
DBCursor myCursor = myCollection.find();
List myStrings = new ArrayList();
while(myCursor.hasNext()) {
myStrings.add(myCursor.next().get("room").toString());
}
return new HouseModel(myStrings);
}
}
I felt pretty good about that, and so I fired up the local process and promptly got an error:
java.io.IOException: couldn't connect to [/192.168.2.191:27017] bc:java.net.NoRouteToHostException: No route to host: connect
IP address was resolving correctly, but a cryptic “NoRouteToHoseException”. I deployed my app to the server, “daedalus” and it ran fine, as-is. I was glad to see that my code was working as I expected with Mongo, but I didn’t want to have to deploy to the server every time I wanted to run the application, so I started poking around for a fix. I had no luck connection through the browser either. Something was strange.
At this point, I remembered a similar annoyance in dealing with MySQL, and for some reason “iptables” popped into my head. After poking around a little for syntax particulars, I ran the following commands:
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 28017 -j ACCEPT
Sure enough, that did the trick. Accessing MongoDB through the browser now yielded:
I had been struggling to figure out what I needed to do to the mongo installation, but the problem was with the Linux firewall (or, more specifically, the fact that I hadn’t set it up to allow connections on these ports). To make my changes permanent, I added the following to /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 28017 -j ACCEPT
I felt good about this, since I saw my handiwork in the same file opening up ports for MySQL and Tomcat. To verify that this bit of goodness stuck around, I rebooted and re-ran the web app on my client desktop…