Skip to content

How to install multiple versions of MongoDB on Linux

Last updated on December 17, 2023

1. Choose version:

2. Installation and Configuration

I use Ubuntu 20.04 and I want to install mongodb Community ver 2.6.12

Installation:

$ cd ~/Downloads
$ curl -O http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.12.tgz
$ tar -zxvf mongodb-linux-x86_64-2.6.12.tgz
$ mv mongodb-linux-x86_64-2.6.12 2.6.12
$ cd /opt && mkdir -p mongodb
$ cd ~/Downloads
$ cp -R 2.6.12 /opt/mongodb
$ cp /mongod.conf /opt/mongodb/2.6.12/

Configuration:

Configuration options on version <3.0 is different with version >3.0 so I googled it. This is what I had.

systemLog:
   destination: file
   path: "/var/log/mongodb/mongodb.log"
   logAppend: true
storage:
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: 127.0.0.1
   port: 27018
setParameter:
   enableLocalhostAuthBypass: false

On this file, you need change systemLog.path, net.port and net.bindIp to your own. One important thing is we cannot run 2 versions at same time with same port. So here I set 27018 for older version.

Put it wherever you want, I did:

cp ~/mongod.conf /opt/mongodb/2.6.12/

DON’T RUN THIS because it will overwrite current mongodb version commands

$ export PATH=/opt/mongodb/2.6.12/bin:$PATH

Create data and log directories and set permission.

$ mkdir -p /data/db
$ mkdir -p /var/log/mongodb

Running:

Now you can start 2.6 version with command.

$ /opt/mongodb/2.6.12/bin/mongod --fork --dbpath /data/db --config
$ /opt/mongodb/2.6.12/mongod.conf

It is too long, isn’t it? So make it shorter with alias.

$ sudo nano ~/.bash_profile
or
$ sudo nano ~/.bashrc

and add this to file

alias mongo26="/opt/mongodb/2.6.12/bin/mongo --port 27018"
alias mongoRestore26="/opt/mongodb/2.6.12/bin/mongorestore --port 27018"
alias mongoDump26="/opt/mongodb/2.6.12/bin/mongodump --port 27018"
alias startMongo26="/opt/mongodb/2.6.12/bin/mongod --fork --dbpath /data/db --config /opt/mongodb/2.6.12/mongod.conf"
alias stopMongo26="/opt/mongodb/2.6.12/bin/mongod --dbpath /data/db --shutdown"

alias nanoMongo="nano /opt/mongodb/2.6.12/mongod.conf"
alias tailMongo="tail -f /var/log/mongodb/mongod.log"
alias tailMongo26="tail -f /var/log/mongodb/mongodb.log"

Apply these alias

$ source ~/.bash_profile
or
$ source ~/.bashrc

Now we can simply start mongodb version 2.6 by

$ startMongo26
about to fork child process, waiting until server is ready for connections.
forked process: 1049481
child process started successfully, parent exiting

Run shell version 2.6

$ mongo26
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27018/test
>

Run shell current version

$ mongo
MongoDB shell version v4.4.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3ae6094f-86dc-4741-aa72-1e48efc5273a") }
MongoDB server version: 4.4.3
>

3. Troubleshooting

  • Cannot start by startMongo26 please check log at /var/log/mongodb/mongod.log
about to fork child process, waiting until server is ready for connections.
forked process: 1049890
ERROR: child process failed, exited with error number 100

Check log at /var/log/mongodb/mongod.log

  • If log has this line:
2021-02-26T15:51:13.493-0700 [initandlisten] exception in initAndListen: 10309 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating

This is because your current user doesn’t have permission to run and write into this directory. You can set permission to /data/db or change directory. I changed to ~/datamongo

$ /opt/mongodb/2.6.12/bin/mongod --fork --dbpath ~/datamongo --config
$ /opt/mongodb/2.6.12/mongod.conf
  • Do the same with error on /var/log/mongodb/mongodb.log

Cannot shutdown by stopMongo26

There doesn't seem to be a server running with dbpath: /data/db

Do this:

$ mongo26
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1:27018/test
> use admin
switched to db admin
> db.shutdownServer()
2021-02-26T15:45:19.738-0700 DBClientCursor::init call() failed
server should be down...
2021-02-26T15:45:19.742-0700 trying reconnect to 127.0.0.1:27018 (127.0.0.1) failed
2021-02-26T15:45:19.742-0700 warning: Failed to connect to 127.0.0.1:27018, reason: errno:111 Connection refused
2021-02-26T15:45:19.742-0700 reconnect 127.0.0.1:27018 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27018 (127.0.0.1), connection attempt failed

Do step by step with my instruction you gonna be ok. Good luck and enjoy!!!

*My answer on stackoverflow: https://stackoverflow.com/a/66393820/3349931

Published in⌨️ Programming TipsBlogs

Comments are closed.