Home > Database > Mysql Tutorial > body text

mongo数据库的操作

WBOY
Release: 2016-06-07 15:36:13
Original
1019 people have browsed it

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
Copy after login

2.验证启动mongodb
/usr/local/mongodb/bin/mongod --dbpath=/var/mongodb/data --logpath /var/mongodb/logs/log.log -auth -fork
Copy after login

登录的例子:

/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
Copy after login

3 使用:

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

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

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

登录:

/usr/local/mongodb/bin/mongo
Copy after login
mongodb的超级账户启动后,必须先use admin,在admin数据库中验证超级账户,然后才能操作其他,创建账户等


use admin;
db.auth('onfancy','onfancy!2014');
use market;
db.createUser(
    {
      user: "market",
      pwd: "market123",
      roles: ["readWrite"]
    }
);
Copy after login


创建账户成功后,就可以使用这个test账户连接mongodb了,连接的方法为

use market

db.auth('market','market123');
Copy after login


删除用户:

use market
db.dropUser('market')
Copy after login

显示dbs和collection
show dbs;

show collections
Copy after login

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;
Copy after login


缓冲区写入磁盘,并且上锁

db.runCommand({"fsync":1,"lock":1});
Copy after login

备份:<br>

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


恢复:

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

解锁

$db.$cmd.sys.unlock.findOne();
Copy after login
























source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template