mongodb commands

install only mongodb community  version 6 and above
mongo commands,
sudo systemctl start mongod

sudo systemctl enable mongod

sudo systemctl restart mongod

for backup:-

$ mongodump --db healyosdb --out /home/ec2-user/mongobackup/`date +"%m-%d-%y"`


to restore database

mongorestore  --host localhost -d SAMPLE_VMS --port 27017 /home/vms.krishit/web/vms.krishit.com/public_html/VMS

here SAMPLE_VMS is dbname creating  and VMS folder contains the db



creating user with pass

 use hrms_prod               
db.createUser(
 {
   user: "hrms_prod",
   pwd: passwordPrompt(),  
   roles: [
      { role: "readWrite", db: "hrms_prod" }
   ]
 }
)

//use cmd is important

Enter password: 
Successfully added user: {
"user" : "hrms_prod",
"roles" : [
{
"role" : "readWrite",
"db" : "hrms_prod"
}
]
}


username is mongoAdmin

password is h44qEBAXqs2

role readWrite

and db is SAMPLE_VMS

mongo --version

install

wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/3.0/x86_64/RPMS/mongodb-org-server-3.0.15-1.el7.x86_64.rpm

rpm -ivh mongodb-org-server-3.0.15-1.el7.x86_64.rpm


systemctl start mongod


SyntaxError: Missing semicolon. (1:12)

do not try to restore from mongosh directly run the command from shell



how can i give username and password in thie url


mongodb://192.227.112.108:27017/KrishitDB_VMS_Dev 


mongodb://admin:password@localhost:27017/db


use this working

mongodb://hrms:h44qEBAXqs2@192.227.112.108:27017/hrms


> show databases;
KrishitDB_VMS_Dev  0.027GB
admin              0.000GB
config             0.000GB
local              0.000GB

to create db

use DATABASE_NAME

Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it.

>db.movie.insert({"name":"tutorials point"})
>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB



Next, make sure that your administrative user is able to authenticate properly by running the following mongo command to connect as this user. This command includes the -u flag, which precedes the name of the user you want to connect as. Be sure to replace AdminSammy with your own administrative user’s username. It also includes the -p flag, which will prompt you for the user’s password, and specifies admin as the authentication database where the specified username was created:

  1. mongo -u AdminSammy -p --authenticationDatabase admin

Adding the User

Use the db.createUser() method to add the user to the specified database. Adding a user with options is easy, it’s essentially inserting a user document into a NoSQL database!

use reporting               // specify the DB
db.createUser(
 {
   user: "reportsUser",
   pwd: passwordPrompt(),  // or cleartext password if you wish
   roles: [
      { role: "read", db: "reporting" },
      { role: "read", db: "products" },
      { role: "read", db: "sales" },
      { role: "readWrite", db: "accounts" }
   ]
 }

on ubuntu 

//only works in mongo 6

mongosh

//here use admin is important to create user

test> use admin
switched to db admin
admin> db.createUser(
...   {
...     user: "admin-user",
...     pwd: passwordPrompt(),
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
...  }
... )
Enter password
***{ ok: 1 }


i typed pass bum

single line cmd below
use admin

db.createUser({user:"admin",pwd: passwordPrompt(),roles:[{role: "userAdminAnyDatabase", db: "admin"}], readWriteAnyDatabase})


mongosh -u admin-user -p --authenticationDatabase bum

bum is password set in above cmd




So, I've finally figured this out. Note, this fix only works for Mongo 2.4.

In 2.4 you can specify a database to authenticate yourself with in the connection string or on the command line

So to create the initial user


root@vmi1115207:/var/log/mongodb# mongo --version

MongoDB shell version v3.6.8



use admin
db.createUser({user: "foo" , pwd: "bar", roles: [  "userAdminAnyDatabase","readWriteAnyDatabase" ]})


Comments

Popular posts from this blog

how to install internet download manger in linux

premium url shortener

convert direct traffic to organic