时间:2021-07-01 10:21:17 帮助过:48人阅读
1.无验证启动mongodb /usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -fork 2.验证启动mongodb /usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -auth -for
1.无验证启动mongodb/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -fork
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -auth -fork
登录的例子:
/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
在这样启动前,需要先通过无验证启动mongodb,然后增加账户,增加账户的方法为
首先增加超级账户,然后通过超级账户增加对应db的相对应账户
增加的超级账户,在auth之前,必须use admin
步骤:
先用无验证方式启动mongodb
use admin; db.createUser({user:"onfancy",pwd:"onfancy!2014", roles:[{role:"root",db:"admin"}]})
登录:
/usr/local/mongodb/bin/mongomongodb的超级账户启动后,必须先use admin,在admin数据库中验证超级账户,然后才能操作其他,创建账户等
use admin; db.auth('onfancy','onfancy!2014'); use market; db.createUser( { user: "market", pwd: "market123", roles: ["readWrite"] } );
创建账户成功后,就可以使用这个test账户连接mongodb了,连接的方法为
use market db.auth('market','market123');
删除用户:
use market db.dropUser('market')
show dbs; show collections
4 备份数据库:
4.备份数据库:
首先把数据库关闭,然后通过无密码验证的方式启动
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -fork /usr/local/mongodb/bin/mongo use admin db.auth('onfancy','onfancy!2014'); show dbs;
缓冲区写入磁盘,并且上锁
db.runCommand({"fsync":1,"lock":1});
备份:
/usr/local/mongodb/bin/mongodump -d test -o /backup/dump
恢复:
/usr/local/mongodb/bin/mongorestore -d test /backup/dump/*
解锁
$db.$cmd.sys.unlock.findOne();