Skip to content

Instantly share code, notes, and snippets.

@shivampip
Last active October 18, 2022 01:30
Show Gist options
  • Save shivampip/f9623c55d8676c7f10a9b5cc702e6e20 to your computer and use it in GitHub Desktop.
Save shivampip/f9623c55d8676c7f10a9b5cc702e6e20 to your computer and use it in GitHub Desktop.
MongoDB on Ubuntu 18.04 (nginx) SSH

Just follow these steps and teke ref of Refernce

  • Uninstall previous installation
sudo service mongod stop

sudo apt-get purge mongodb-org*

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
  • Now Install it
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

sudo apt-get update

sudo apt-get install -y mongodb-org=4.2.0 mongodb-org-server=4.2.0 mongodb-org-shell=4.2.0 mongodb-org-mongos=4.2.0 mongodb-org-tools=4.2.0

(optional)
echo "mongodb-org hold" | sudo dpkg --set-selections
echo "mongodb-org-server hold" | sudo dpkg --set-selections
echo "mongodb-org-shell hold" | sudo dpkg --set-selections
echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
echo "mongodb-org-tools hold" | sudo dpkg --set-selections
  • Run and check
sudo service mongod start
sudo service mongod status

If error just do it

sudo systemctl enable mongod

or try this if (code=exited, status=14)

chown -R mongodb:mongodb /var/lib/mongodb
chown mongodb:mongodb /tmp/mongodb-27017.sock

Now run

  • Adding user and roles
mongo

Now in mongo shell

use admin
db.createUser(
  {
    user: "shivam",
    pwd: "shivpass",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
  • Now enable Authenticaton
nano /etc/mongod.conf

In #security section, add this

security:
  authorization: "enabled"
  • Restart and check
sudo systemctl restart mongod
sudo systemctl status mongod
  • Connect to local mongodb
mongo -u shivam -p --authenticationDatabase admin

Enter password when asked and connect

Remote access

  • Modify mongod.conf
sudo nano /etc/mongod.conf

in #net section, do it

net:
  port: 27017
  bindIp: 127.0.0.1,153.92.4.175

where 153.92.4.175 is vps static ip

  • Restart mongod and check status
sudo systemctl restart mongod
sudo systemctl status mongod
  • Connect to this remote mongo from anywhere on the planet by
mongo -u shivam -p --authenticationDatabase admin --host 153.92.4.175

Create New database

  • Open mongo shell (local or remote)
  • create db by
>use shivaDB
  • Add permission
> use admin
> db.updateUser("shivam",
{role:[{ role: "userAdminAnyDatabase", db: "admin" }, { role: "readWrite", db: "shivaDB" } ]}
)
  • Done, now check by creating collection
> use shivaDB
> db.createCollection("dddd")

Connection with django by djongo

pip install djongo

In settings.py edit

DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': "shivaDB",
        'HOST': "153.92.4.175",
        'USER': "shivam",
        'PASSWORD': "shivam123"
    }
}

Now migrate (to create collection in db)

python manage.py makemigrations
python manage.py migrate

Enjoy

Note:

You don't need to worry about Nginx It won't intureept

Refernce

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