Fixing my first problem with MongoDB.

working with MongoDB seemed easy at first.

It isn’t available in the APT repository and compiling it on the Raspberry pi is a little different so I followed this very helpful guide.

That got me up and running so next I had to learn a little more about MongoDB. Fortunately, the MongoDB manual is fantastic so This section told me everything I needed to know to create my first database and collection. Note, collection is similar to a table in SQL world.

Next, I wanted to dive right in and connect my Python application that is taking data over serial from my Arduino. The idea was to add this data to a collection in MongoDB so that historical graphs could be generated. With a quick Google search, I found This quick tutorial that shows the basics of Python and MongoDB. Armed with a little knowledge I successfully learned how to add data to my new collection in MongoDB. Everything was working perfectly.

That was on Friday night. Saturday was a very busy day so I didn’t get near this stuff at all until late on Sunday. With a lot of frustration, I found that no data was shown in MongoDB any more and MongoDB was frequently hanging without providing me with a prompt. This seemed to be any time I tried to use the db operation. I was absolutely certain that data had been written to the database and collection so I set about trying to find out what was going wrong.

First, I checked the logs in /var/log/mongod/mongodb.log. All they were showing me were the connection attempts.

Then I looked at the ocasional time out error message that I was getting from within the MongoDB shell. Still nothing useful.

I even tried searching forums etc for some hint of what might be going wrong.

I found two useful pages that I’ll keep for future reference but unfortunately, they were of no help with this problem. This is a forum post that someone created when MongoDB seemed to be hung. and This one shows how to troubleshoot hanging issues.

I decided to try running MongoDB as a user run process instead of a system daemon. Of course, then, i was using the default settings so I thought that it might show me if anything was going wrong with the settings in /etc/mongo.conf. No. All I got was a message about the dBPath not set. This was of no use to me because I quickly found out that in /etc/mongod.conf, the DBPath was set to /var/lib/mongodb/. However, this pointed me toward looking at the permissions on that folder. I had set them while installing MongoDB but I thought I’d check again to be sure that the permissions were set recursively for child files and folders. Again, this wasn’t the problem, however I noticed a file called mongod.lock. I thought maybe this was a place holder to show that the database was locked so I tried moving that file out of the /var/lib/mongodb directory. Sure enough, I made a little more progress. The show dbs command was still causing the MongoDB shell to hang. I deleted the databases that I created while working with Python earlier from the /var/lib/mongodb/ directory and this let the show dbs file runn a little longer but it was still causing MongoDB to freeze. I deleted the MyDB database that was created when I was learning MongodB first as well and the command completed successfully. Of course, I was very aware that I had deleted files from the file system and the databases were still there but were probably now corrupt within MongodB so with another quick search on Google, I found the short command to delete databases from within MongoDB. I also found the command to drop mongoDB collections as well but I don’t particularly need that at the moment. Handy to have for future reference of course.

So, after deleting the files and deleting the databases within MongoDB, the system is working perfectly. However, I hear you say, “There’s nothing in the system, of course it’s running perfectly!” Your absolutely right, but for the moment, there’s no locking so I@m going to hope either one of the tutorials / forum posts were wrong or it was just something in one of the databases that became corrupt.