1. Start mongodbusrlocalmongodbbinmongod -- dbpathvarmongodbdata -- logpathvarmongodblogslog. log-fork2. Verify start mongodbusrlocalmongodbbinmongod -- dbpathvarmongodbdata -- logpathvarmongodblogslog. log-auth-
1. start mongodb/usr/local/mongodb/bin/mongod -- dbpath =/var/mongodb/data -- logpath/var/mongodb/logs/log without verification. log-fork 2. verify that mongodb/usr/local/mongodb/bin/mongod -- dbpath =/var/mongodb/data -- logpath/var/mongodb/logs/log is started. log-auth-
1. Start mongodb without verification
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -fork
2. Verify that mongodb is started.
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -auth -fork
Logon example:
/usr/local/mongodb/bin/mongo -umarket -pmarket123 127.0.0.1:27017/market/usr/local/mongodb/bin/mongo -umarket -pmarket123 127.0.0.1:27017/market/usr/local/mongodb/bin/mongo -uonfancy -ponfancy!2014 127.0.0.1:27017/market
3 usage:
Before starting the mongodb instance in this way, you must first start mongodb without verification, and then add an account.
First, add a super account, and then add the corresponding account of the corresponding database through the super account
The added super account must use admin before auth
Steps:
Start mongodb with no authentication
use admin;db.createUser({user:"onfancy",pwd:"onfancy!2014", roles:[{role:"root",db:"admin"}]})
Disable mongodb
Then start mongodb In the second way that requires verification.
Logon:
/usr/local/mongodb/bin/mongo
After the mongodb super account is started, you must first use admin, verify the super account in the admin database, and then perform operations on other accounts and create accounts.
use admin;db.auth('onfancy','onfancy!2014');use market;db.createUser( { user: "market", pwd: "market123", roles: ["readWrite"] });
After the account is successfully created, you can use the test account to connect to mongodb. The connection method is
use marketdb.auth('market','market123');
Delete A User:
use marketdb.dropUser('market')
Display dbs and collections
show dbs;show collections
4. Back up the database:
4. Back up the database:
First, shut down the database and start the database without password verification.
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -fork/usr/local/mongodb/bin/mongouse admindb.auth('onfancy','onfancy!2014');show dbs;
The buffer is written to the disk and locked.
db.runCommand({"fsync":1,"lock":1});
Backup:
/usr/local/mongodb/bin/mongodump -d test -o /backup/dump
Recovery:
/usr/local/mongodb/bin/mongorestore -d test /backup/dump/*
Unlock
$db.$cmd.sys.unlock.findOne();