Skip to content

Instantly share code, notes, and snippets.

@duyhenryer
Forked from shivampip/MongoDB_ubuntu_nginx_ssh.md
Last active October 27, 2020 05:51
Show Gist options
  • Save duyhenryer/4c3730b88d00b468b26ff1218a726e52 to your computer and use it in GitHub Desktop.
Save duyhenryer/4c3730b88d00b468b26ff1218a726e52 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 xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

sudo apt-get update -y && sudo apt-get install -y mongodb-org && sudo service mongod start && systemctl enable mongod
  • 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
rm -rf /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: 0.0.0.0
  • 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