Here's how to get an instance of MongoDB up and running on your Opalstack server:
- Create a shell user and make a note of the shell username. I'll use
myuser
as the username in the remainder of this procedure.
- Create a "proxy port" application and make a note of the app name its port assignment. I'll use
mongoapp
and port 55555
in the remainder of this procedure.
- SSH to your shell user from step 1 and run the following commands:
cd ~/apps/mongoapp
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.1.tgz
tar zxf mongodb-linux-x86_64-rhel70-4.4.1.tgz
ln -s $PWD/mongodb-linux-x86_64-rhel70-4.4.1 mongodb
wget https://downloads.mongodb.com/compass/mongosh-1.8.0-linux-x64.tgz
tar zxf mongosh-1.8.0-linux-x64.tgz
ln -s $PWD/mongosh-1.8.0-linux-x64 mongosh
mkdir data
./mongodb/bin/mongod --dbpath $PWD/data --port 55555 --logpath $HOME/logs/apps/mongoapp/mongo.log --pidfilepath $PWD/pid --fork
- The MongoDB server is now running in the background. Connect to it and create your Mongo superuser with the following commands:
~/apps/mongoapp/mongosh/bin/mongosh localhost:55555/admin
# you're now in the mongo shell, next command creates your mongo superuser
# replace USERNAME and PASSWORD with your desired mongo username and pw
db.createUser({user: "USERNAME", pwd: "PASSWORD", roles: [ "root" ]})
# verify that it was created
show users
# then exit
exit
- Finally, stop the running mongodb process and restart it with auth enabled:
kill $( cat $HOME/apps/mongoapp/pid)
$HOME/apps/mongoapp/mongodb/bin/mongod --dbpath $HOME/apps/mongoapp/data --port 55555 --logpath $HOME/logs/apps/mongoapp/mongo.log --pidfilepath $HOME/apps/mongoapp/pid --fork --auth
Your MongoDB instance is now running in the background waiting for authenticated connections.
You'll probably want to reconnect as your superuser from step 4 above to create less-privileged users with the roles necessary for your application:
~/apps/mongoapp/mongodb/bin/mongo localhost:55555/admin --username USERNAME
Please refer to the MongoDB docs for how to create additional users: MongoDB: Add Users
Here are the commands to start and stop it:
- Start:
$HOME/apps/mongoapp/mongodb/bin/mongod --dbpath $HOME/apps/mongoapp/data --port 55555 --logpath $HOME/logs/apps/mongoapp/mongo.log --pidfilepath $HOME/apps/mongoapp/pid --fork --auth
- Stop:
kill $( cat $HOME/apps/mongoapp/pid)
That's it - now go forth and Mongo! 🍃