Home  >  Article  >  Database  >  mongo数据库的操作

mongo数据库的操作

WBOY
WBOYOriginal
2016-06-07 15:36:131014browse

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

2.验证启动mongodb
/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

3 使用:

在这样启动前,需要先通过无验证启动mongodb,然后增加账户,增加账户的方法为
首先增加超级账户,然后通过超级账户增加对应db的相对应账户
增加的超级账户,在auth之前,必须use admin
步骤:
先用无验证方式启动mongodb

use admin;
db.createUser({user:"onfancy",pwd:"onfancy!2014", roles:[{role:"root",db:"admin"}]})

然后关闭mongodb
然后用第二种需要验证的方式启动mongodb

登录:

/usr/local/mongodb/bin/mongo
mongodb的超级账户启动后,必须先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')

显示dbs和collection
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});

备份:<br>

/usr/local/mongodb/bin/mongodump -d test -o /backup/dump


恢复:

/usr/local/mongodb/bin/mongorestore -d test /backup/dump/*

解锁

$db.$cmd.sys.unlock.findOne();
























Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn