Skip to content

Instantly share code, notes, and snippets.

@JimmyPun610
Last active July 6, 2020 00:30
Show Gist options
  • Save JimmyPun610/61e65f7a0dacb57c0199b404eee1c7fa to your computer and use it in GitHub Desktop.
Save JimmyPun610/61e65f7a0dacb57c0199b404eee1c7fa to your computer and use it in GitHub Desktop.

1. Installation

Follow the guide with MongoDb site

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

2. Add User, use below command in mongoDB

use databaseName
db.createUser({
    user: 'username',
    pwd: 'secretPassword',
    roles: [{ role: 'readWrite', db:'databaseName'}]
})

3. Modify configuration /etc/mongod.conf

sudo nano /etc/mongod.conf

In the conf file modify as below :

  # network interfaces
  net:
    port: 27017
    bindIp: 0.0.0.0
  
  security:
  authorization: 'enabled'

4. Open port for mongoDb remote acccess (optional)

sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT  -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

5. Restart mongoDb service to take effect

sudo service mongod restart

Reference:

https://ianlondon.github.io/blog/mongodb-auth/

https://medium.com/founding-ithaka/setting-up-and-connecting-to-a-remote-mongodb-database-5df754a4da89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment