Install multiple versions of MongoDB on linux
To do that, you must have both versions of MongoDB available on your system.
For instance, let's say you have downloaded the 2.6 version binaries to /opt/mongo/26/
and the other version's at /opt/mongo/34/
, you could run both versions of the database daemon on different ports:
/opt/mongo/26/mongod --dbpath /data/26/ --port 27017/opt/mongo/34/mongod --dbpath /data/34/ --port 28018
This is what I do.
1. Choose version:
- Community: https://www.mongodb.com/download-center/community/releases/archive
- Enterprise: https://www.mongodb.com/download-center/enterprise/releases/archive
2. Install and Configuration
I use Ubuntu 20.04 and I want to install mongodb Community ver 2.6.12
Installing:
$ 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: truestorage: journal: enabled: trueprocessManagement: fork: truenet: bindIp: 127.0.0.1 port: 27018setParameter: 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_profileor$ 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_profileor$ source ~/.bashrc
Now we can simply start mongodb version 2.6 by
$ startMongo26about to fork child process, waiting until server is ready for connections.forked process: 1049481child process started successfully, parent exiting
Run shell version 2.6
$ mongo26MongoDB shell version: 2.6.12connecting to: 127.0.0.1:27018/test>
Run shell current version
$ mongoMongoDB shell version v4.4.3connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodbImplicit 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: 1049890ERROR: 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:
$ mongo26MongoDB shell version: 2.6.12connecting to: 127.0.0.1:27018/test> use adminswitched to db admin> db.shutdownServer()2021-02-26T15:45:19.738-0700 DBClientCursor::init call() failedserver should be down...2021-02-26T15:45:19.742-0700 trying reconnect to 127.0.0.1:27018 (127.0.0.1) failed2021-02-26T15:45:19.742-0700 warning: Failed to connect to 127.0.0.1:27018, reason: errno:111 Connection refused2021-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!!!