Tuesday, 24 April 2018

mongodb create user security




I want to set up user name & password authentication for my MongoDB instance, so that any remote access will ask for the user name & password. I tried the tutorial from the MongoDB site and did following:

use admin
db.addUser('theadmin', '12345');
db.auth('theadmin','12345');


After that, I exited and ran mongo again. And I don't need password to access it. Even if I connect to the database remotely, I am not prompted for user name & password.

UPDATE Here is the solution I ended up using

1) At the mongo command line, set the administrator:
 
    use admin;
    db.addUser('admin','123456');
 
2) Shutdown the server and exit
 
    db.shutdownServer();
    exit
 
3) Restart mongod with --auth
 
  $ sudo ./mongodb/bin/mongod --auth --dbpath /mnt/db/
 
4) Run mongo again in 2 ways:
 
   i) run mongo first then login:
 
        $ ./mongodb/bin/mongo localhost:27017
        use admin
        db.auth('admin','123456');
 
  ii) run & login to mongo in command line.
 

        $ ./mongodb/bin/mongo localhost:27017/admin -u admin -p 123456



The username & password will work the same way for mongodump and mongoexport.



You need to start mongod with the --auth option after setting up the user.
From the MongoDB Site:
Run the database (mongod process) with the --auth option to enable security. You must either have added a user to the admin db before starting the server with --auth, or add the first user from the localhost interface.



First, un-comment the line that starts with #auth=true in your mongod configuration file (default path /etc/mongo.conf). This will enable authentication for mongodb.
Then, restart mongodb : sudo service mongod restart


This answer is for Mongo 3.2.1 Reference
Terminal 1:
$ mongod --auth

Terminal 2:
db.createUser({user:"admin_name", pwd:"1234",roles:["readWrite","dbAdmin"]})


if you want to add without roles (optional):

db.createUser({user:"admin_name", pwd:"1234", roles:[]})
to check if authenticated or not:
db.auth("admin_name", "1234")
it should give you:
1

else :
Error: Authentication failed.

0



older versions such as 2.4 would use db.addUser
I had to type use admin before createUser otherwise it gave an error.


This is as of v3.4.
1)    Start MongoDB without access control.

mongod --dbpath /data/db

2)    Connect to the instance.
mongo

3)    Create the user.
use some_db
db.createUser(
  {
    user: "myNormalUser",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "some_db" },
             { role: "read", db: "some_other_db" } ]
  }
)
 
 
 

4)    Stop the MongoDB instance and start it again with access control.
        mongod --auth --dbpath /data/db

5)    Connect and authenticate as the user.

use some_db
db.auth("myNormalUser", "xyz123")
db.foo.insert({x:1})
use some_other_db
db.foo.find({})


Long answer: Read this if you want to properly understand.

It's really simple. I'll dumb the following down https://docs.mongodb.com/manual/tutorial/enable-authentication/
If you want to learn more about what the roles actually do read more here: https://docs.mongodb.com/manual/reference/built-in-roles/
1) Start MongoDB without access control.

mongod --dbpath /data/db



2) Connect to the instance.
mongo
3) Create the user administrator. The following creates a user administrator in the adminauthentication database. The user is a dbOwner over the some_db database and NOT over the admin database, this is important to remember.
use admin
db.createUser(
  {
    user: "myDbOwner",
    pwd: "abc123",
    roles: [ { role: "dbOwner", db: "some_db" } ]
  }
)
Or if you want to create an admin which is admin over any database:
use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
4) Stop the MongoDB instance and start it again with access control.
mongod --auth --dbpath /data/db
5) Connect and authenticate as the user administrator towards the admin authentication database, NOT towards the some_db authentication database. The user administrator was created in the admin authentication database, the user does not exist in the some_db authentication database.
use admin
db.auth("myDbOwner", "abc123")
You are now authenticated as a dbOwner over the some_db database. So now if you wish to read/write/do stuff directly towards the some_db database you can change to it.
use some_db
//...do stuff like db.foo.insert({x:1})
// remember that the user administrator had dbOwner rights so the user may write/read, if you create a user with userAdmin they will not be able to read/write for example.
If you wish to make additional users which aren't user administrators and which are just normal users continue reading below.
6) Create a normal user. This user will be created in the some_db authentication database down below.
use some_db
db.createUser(
  {
    user: "myNormalUser",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "some_db" },
             { role: "read", db: "some_other_db" } ]
  }

)



7) Exit the mongo shell, re-connect, authenticate as the user.
use some_db
db.auth("myNormalUser", "xyz123")
db.foo.insert({x:1})
use some_other_db
db.foo.find({})



Edit the mongo settings file;
sudo nano /etc/mongod.conf
Add the line:
security.authorization : enabled
Restart the service
sudo service mongod restart



First run mongoDB on terminal using
mongod
now run mongo shell use following commands
    use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
Re-start the MongoDB instance with access control.
mongod --auth
Now authenticate yourself from the command line using
mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
I read it from


db.system.users.find()



db.changeUserPassword()
db.auth('admin', 'my pass')


You'll need to switch to the database you want the user on (not the admin db) ...
use mydatabase



use dbName
db.createUser(
   {
     user: "dbUser",
     pwd: "dbPassword",
     roles: [ "readWrite", "dbAdmin" ]
   }
)


These steps worked on me:
1.      write mongod --port 27017 on cmd
2.      then connect to mongo shell : mongo --port 27017
3.      create the user admin : use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )
4.      disconnect mongo shell
5.      restart the mongodb : mongod --auth --port 27017
6.      start mongo shell : mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"
7.      To authenticate after connecting, Connect the mongo shell to the mongod: mongo --port 27017
8.      switch to the authentication database : use admin db.auth("myUserAdmin", "abc123"


//connection show in mongo db by give command
grep 2019-01-26 mongod.log | grep "end connection"  | awk -F "end connection" '{print $2}' | awk '{print $2}' | sort | tail



2 . db.serverStatus().connections




====================================



Url: http://adminapp.nexg.tv/admin/default/login
Username: admin@nexgtv.com
Password: NexGTV_123
admin@Digivive1

Password: Digi_1234
password: Digi@Nexg123


CRM0924TTOFO4

URL - adminapp.nexg.tv/admin/default/login
Admin User Name : admin@nexgtv.com
Password : Digi@Nexg123
PK482

http://dev-app.nexg.tv/



172.31.22.238
http://dev-app-digivive.nexg.tv/admin/default/login
admin@nexgtv.com
Digi@Nexg123



vodafone : 9643467350
vodafone 8587916376


alok no 7290947826




================================================

 "hosts" : [
                "Appinv-db:27017(172.31.22.235)",
                "Appinv-db3:27017(172.31.22.160)",
                "INWAP-06:27017(172.31.22.55)",
                "Appinv-db2:27017(172.31.22.236)"
        ],

rs.printSlaveReplicationInfo()



http://dev-app-digivive.nexg.tv/admin/channel/index

User Name :admin@nexgtv.com
Password : Digi@Nexg123


====================================================
Private IP(172.31.22.91)


http://dev-app-digivive.nexg.tv/(172.31.22.91)

Username : admin@nexgtv.com
Password :   Digi_1234


find / -type f -name "*.php" -exec grep -il tapan {} \;
=========================================



mongodb

db folder create 1st


mongod --storageEngine=mmapv1 --dbpath [your-path] 



mongod --storageEngine=mmapv1 --dbpath C:/data/db

//database backup and restore command
mongodump --db wowza --out /root/mong_nexgtv_9aug16

mongodump --db nexgtv_16 --out /root/mong_nexgtv_9aug16
mongorestore --db nexgtv_16 /home/mongodump/mong_nexgtv_9aug16/nexgtv_16

mongoimport --db dbName --collection collectionName --file fileName.json --jsonArray



=============================================



//export file in mongodb


mongoexport -d mydb -c mycollection -o mybackup.json
connected to: 127.0.0.1
exported 9438 records

mongoexport -d nexgtv_16 -c ADMIN -o /home/mybackup.json




//Import file in push file in mongodb
 mongoimport -d mydb -c mycollection --file mybackup.json
connected to: 127.0.0.1
Sat Jan 16 12:06:10 imported 19878 objects

mongoimport -d nexgtv_16  -c VOUCHERS --file /var/www/vouchecollive/VOUCHERS_28sep_16_backup.json

mongoimport -d nexgtv_16  -c VOUCHER_CODES --file /var/www/vouchecollive/VOUCHER_CODES_28sep_16backup.json

===================================

vendor/yiisoft/yii2-mongodb

==================================


521F4D84-F6BD-4445-821E-06A6DE28F632_DEN



9910058197(VIDDTH Registred Mobile number)


rm -rf DUMMY*
rm -rf Launch_User*
rm -rf USER_HISTORY*
rm -rf NOTIFICATION_HISTORY_*



bulksms@infotelconnect.com


mongorestore -d nexgtv_bcup -c SESSION /var/www/nexgtv-2017-01-12-0000/nexgtv_16/SEASON.bson

tar -xvzf /var/www/nexgtv-2017-01-12-0000.tgz
mongorestore -d db_name -c collection_name path/file.bson


============================================================


mongodump --db=<old_db_name> --collection=<collection_name> --out=data/

mongorestore --db=<new_db_name> --collection=<collection_name> data/<db_name>/<collection_name>.bson


 netstat -anp | awk '{print $6}' | sort | uniq -c
=====================================================

Multiple coloum update using mongo query

db.ASSETS.update({categories:{$in:['CTG513','CTG514']}},{$set: {content_id:"5682cd15cc95b319a65da89b"}},{multi:true})


860848034403882
9722782295
863819036347945




832  pm2 start scheduleworker.js -n cathcup
  833  pm2 stop cathcup
  834  ls -ltr
  835  vim config.js
  836  yum install redis











 785  pm2 start app.js -n digiapis -i 0
  786  pm2 save
  787  pm2 list



db.getReplicationInfo()

 rs.printSlaveReplicationInfo()

rs.printSlaveReplicationInfo()

db.currentOp()

db.stats();
db.serverStatus().connections
mongostat

grep 2019-01-26 mongod.log | grep "end connection"  | awk -F "end connection" '{print $2}' | awk '{print $2}' | sort | tail


2 . db.serverStatus().connections


7055394939
==========================================

mongorestore --db wowza --drop /var/www/analyticsdump/home/wowza_analytics_bcup/wowza_analytics/wowza_analytics/wowza_analytics-2018-07-23-1634/wowza/

===================================

http://dev-nexgtv.nexg.tv/#/




































No comments:

Post a Comment