Mongodb

From Halfface
Jump to navigation Jump to search

connect

mongo

run command(list cluster information)

mongo --ssl --host mongo.inter.net --quiet -u admin -p P@ssW0rd --authenticationDatabase admin --eval 'rs.status();'

list databases

show dbs

Via script

rs.slaveOk();db.adminCommand('listDatabases')

mongodump

Dump database

mongodump --ssl --host "database/mongo.inter.net" -u username -p P@ssw0rd --authenticationDatabase admin --db "database" --out=/tmp/database_2020-10-20"

restore database

mongo -ssl --host database/mongo.inter.net -u username -p P@ssw0rd --authenticationDatabase admin
db.copyDatabase("dbname", "dbname-oldprod-date");> use dbname;
db.dropDatabase();
mongorestore --ssl --host database/mongo.inter.net -u username -p P@ssw0rd --authenticationDatabase admin --gzip  --db dbname /<path>/dbname

Login as ldap user

mongo --ssl --sslCAFile /etc/ssl/mongodb-ca.crt -u username --host mongodb.inter.net --port 27017 --authenticationDatabase '$external' --authenticationMechanism 'PLAIN' -pP@ssW0rd --eval 'rs.status();'
mongo --ssl -u $username --host $(hostname -f) --port 27017 --authenticationDatabase '$external' --authenticationMechanism 'PLAIN' -p $PASSWORD

Look at database status

mongo --ssl --host mongodb.inter.net --quiet -u username -p P@ssW0rd --authenticationDatabase admin --eval 'rs.status();' | head

Put db i state 2

rs.stepDown(120);

Look at connections

'db.currentOp()'

List databases.

'db.getMongo().getDBNames();'

List users

echo -e 'use $external \n db.getUsers();' | mongo --ssl --host mongodb.inter.net --quiet -u username -p P@ssW0rd --authenticationDatabase admin --eval 'rs.status();'

Create user

mongo << END
/// setup authentication
use admin;
db.addUser({"pwd" : "yO9HUQiuLIMfzHK6sBJF0DdwZKiwAq9X", "roles" : [  "userAdminAnyDatabase",  "readWrite" ], "user" : "admin" });
use mathem
//db.addUser("mathem", "Q0Nay1xeY9zq");
db.addUser({"pwd" : "Q0Nay1xeY9zq", "roles" : [  "userAdminAnyDatabase",  "readWrite" ], "user" : "admin" })
END

Which version

db.version()

show process list

Show all operations

db.currentOp( { "$all": true } )