Setting Up and Connecting To A Remote MongoDB Database - by Mithilesh Said - Founding Ithaka - Medium
Setting Up and Connecting To A Remote MongoDB Database - by Mithilesh Said - Founding Ithaka - Medium
At Ithaka quite a few of our microservices use MongoDB 3.6 for data persistence. If you
have used MongoDB you probably already know that starting from version 3.4
MongoDB’s WiredTiger Storage engine takes up a considerable amount of RAM. To be
specific it will take either 50% of (RAM minus 1GB) OR 256 MB, whichever is higher.
You can read more about how and why in their manual.
This, along with the common understanding that it is never a good idea to run your
database service on the same server instance as your production services, makes it clear
that you need to establish remote MongoDB server/s which your services then connect
to.
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89 1/6
11/18/2020 Setting up and connecting to a remote MongoDB database | by Mithilesh Said | Founding Ithaka | Medium
Please note that even though at Ithaka we use a Linux distro on top of EC2 instances, the
points mentioned in this article are generic enough to work on most cloud+OS
combinations. With that out of the way, let’s get to setting things up.
ubuntu:~$ mongo
Inside mongo shell access the admin database. Create a new admin user.
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89 2/6
11/18/2020 Setting up and connecting to a remote MongoDB database | by Mithilesh Said | Founding Ithaka | Medium
You can assign multiple roles to a single user in one go. These roles grant the admin user
all privileges required to create users, modify users, read and write to any database.
Now create db users and grant them roles on their respective databases. If your system
consists of multiple databases, is a good idea to have a separate user for each database.
> db.createUser({
user: "user1",
pwd: "user1password",
roles: [
{ role: "userAdmin", db: "sampledb" },
{ role: "dbAdmin", db: "sampledb" },
{ role: "readWrite", db: "sampledb" }
]
});
Note how the roles have changed here. If you try to assign one of the admin roles like
“userAdminAnyDatabase” to a user on any non admin database, mongo will throw an
error indicating that such a role is not defined on that database. Admin roles work only
on the admin database.
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89 3/6
11/18/2020 Setting up and connecting to a remote MongoDB database | by Mithilesh Said | Founding Ithaka | Medium
security:
authorization: 'enabled'
This will tell mongodb that whenever it starts up next, it needs to enforce database
access control using the roles we created in the previous step.
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 #default value is 127.0.0.1
Now save and exit the config file and restart mongodb server.
Now if you try to access the mongo shell by simply typing mongo in the terminal, you’ll
get through but won’t be able to access any databases. You need to use your created
users to access the databases.
However we are still accessing the mongo shell from within the EC2 instance. We
haven’t tried connecting remotely. To be able to do that, change the network settings of
your EC2 instance.
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89 4/6
11/18/2020 Setting up and connecting to a remote MongoDB database | by Mithilesh Said | Founding Ithaka | Medium
Note: Despite following these instructions, you might still run into issues when
connecting to the remote database within your code. And those issues are mostly
specific to the library that you use in your code. So far we have solved issues with
pymongo for python, mgo for golang and mongoose for node.js. If you are using any of
these, ping me on twitter @MithileshSaid and I’ll be glad to help.
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89 5/6
11/18/2020 Setting up and connecting to a remote MongoDB database | by Mithilesh Said | Founding Ithaka | Medium
https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89 6/6