Last Updated: February 25, 2016
·
1.159K
· zshamrock

Install different versions of mongo locally linux

This script helps you to configure mongodb alternatives for different version of mongodb installed on your local linux machine. The trick here is to use --slave which allows you to switch between mongo groups (in my case 2.2.2 and 2.4.0 versions), so after alternatives are added you can switch all you mongo binaries easily using sudo update-alternatives --config mongo:

#!/bin/bash
# run: ./install-mongo.sh 0|1 <priority>    
files=(bsondump mongod  mongodump  mongoexport  mongofiles  mongoimport  mongooplog  mongoperf  mongorestore  mongos  mongosniff  mongostat  mongotop)
locations=("$HOME/tools/nosql/mongodb/mongodb-linux-i686-2.2.2/bin" "$HOME/tools/nosql/mongodb/mongodb-linux-i686-2.4.0/bin")
link="/usr/local/bin/mongo"
mkdir -p "${link}"
priority="$2"
location="${locations[$1]}"
install_command="update-alternatives --install ${link}/mongo mongo ${location}/mongo ${priority}"
for file in "${files[@]}"
do
    install_command="${install_command} --slave ${link}/${file} ${file} ${location}/${file}"
done
echo on
`sudo ${install_command}`

You should edit your mongodb locations first. After done, you can run:

  • ./install-mongo.sh 0 10
  • ./install-mongo.sh 1 5

where the 1st parameter is the index of you location in locations array, and the 2nd parameter is the priority.