how to upgrade MongoDB 2.6 to 3.x on Ubuntu

sudo mv /etc/apt/sources.list.d/mongodb* /tmp/
echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
sudo apt-get update && sudo apt-get install -y mongodb-org

And I also had to do fix my replica set in the MongoDB shell (necessary for Meteor oplog tailing):

var a = {"_id" : "rs0", "version" : 1,"members" : [{"_id" : 1, "host" : "localhost:27017"}]};
rs.reconfig(a, {force:true});

UPDATE 2015-10-01: Alas, one of my coworkers found even all the above wasn’t enough–he had to blow away his old MongoDB install.

sudo mv /var/lib/mongodb /tmp
sudo apt-get purge mongodb-org-server
sudo apt-get install -y mongodb-org-server

We also use one-member replica sets in dev (Meteor uses the oplog), so edit /etc/mongodb.conf and include something like replSet=rs0, then restart mongo (sudo service mongodb restart). Finally, initialize the replica set:

var a = {"_id" : "rs0", "version" : 1,"members" : [{"_id" : 1, "host" : "localhost:27017"}]};
rs.initiate(a);

It appears that collections can be restored by simply copying files like blah.0, blah.1, blah.2 and blah.ns from /tmp/mongodb to /var/lib/mongodb while the MongoDB server is stopped.

Published by adam

Adam is a kind and savvy free and open source software enthusiast. He is an adept coder, builder, producer, debugger, architect, leader, manager, lecturer, author, administrator, CISO, and CTO. He has logged professional successes in markets of all maturities, sizes, and scales, from startups to big enterprise. He's most proud of his family, growing Mifos (mifos.org), founding SeaGL (seagl.org), selling C-SATS (csats.com), and self-publishing his book about self-hosting (selfhostbook.com).

Leave a comment

Your email address will not be published. Required fields are marked *