Mongo database operations

Source: Internet
Author: User
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();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.