Simple use of Mongodb

Source: Internet
Author: User
Tags auth mongodb server

First, environment and start-up

MongoDB Direct download back decompression can be used. For example, I unzipped to the App/mongo directory. And in the Appmongo directory, the data/db directory is used to present a folder for MongoDB database data.


The root user starts MongoDB, and if you do not use the root user, the second boot will not start again. The specific reason was not found.

CD App/mongo/bin/mongod--dbpath data/db


Enter the shell of the MONGO:

./bin/mongo

Shut down:
A secure way to stop the MongoDB service is to use the shutdown command, {"Shutdown": 1}, which is an administrative command to be used under the admin database. The shell provides auxiliary functions, as follows:


Use admin

Db.shutdownserver ();

If the MongoDB server is the most foreground process running at the terminal, then you can close the command line window directly.


II. Security and Certification

The database in each MongoDB instance can have many users, and if security checks are turned on, only database authenticated users can perform read or write operations. Add users to the database as follows:

Use Testdb.adduser ("Test_user", "1234")


The third parameter in the AddUser () function is optional true or false, indicating whether the user is a read-only user.


Note: AddUser can not only add users, but also modify the user password or read-only status.


To turn on security checks, restart the server and add--auth command-line options. Then reconnect the database through the shell by doing the following:

Use Testdb.auth ("Test_user", "1234")


The user is then able to operate within their own purview.
The user accounts of the database are stored as documents in the System.users collection. The structure of the document is as follows:
{"User": Username, "readOnly": true, "pwd": password hash}


Where the password hash is a hash generated from the user name and password.


When a user authenticates, the server authenticates and binds the connection to track the authentication. So if a driver or tool uses a connection pool or fails over to another node, all authenticated users must re-authenticate each new connection. Some drivers can make this step transparent, but if not, it has to be done manually.


In addition to authentication there are many options worth considering to lock the MongoDB instance. It is recommended that the MongoDB server be placed behind a firewall or in a network that only the application server can access. However, if MongoDB must be accessible outside, it is recommended to use the-BINDIP option to specify the local IP address to which the Mongod is bound.

For example, you can only access from a native application server:

Mongod–bindip localhost

Third, simple use of 1, the use of CRM database
Use CRM



2. Add a piece of data to the user collection in the database (similar to a table in SQL)
Db.user.insert ({"username": "admin", "password": "1234",  


3. Query User Collection
Db.user.find (). Pretty ()


4. Add more data for user collection
Stuff = [{"username": "test1", "Password": "1234", "email": "[email protected]"}, {"username": "test2", "Password": "1234", "email": "[email protected]"}] db.user.insert (stuff);  


The result of the above operation is as follows:
[email protected]:~/app/mongo/bin$./mongomongodb Shell version:2.6.4connecting to:test> use crmswitched to DB Crm> Db.user.insert ({"username": "admin", "password": "1234", "email": "[email protected]"}) Writeresult ({"N Inserted ": 1}" > Db.user.find (). Pretty () {"_id": ObjectId ("53fc73bf784eb7aa025aeb31"), "username": "Admon", " Password ":" 1234 "," email ":" [email protected] "}> stuff = [{" username ":" test1 "," Password ":" 1234 "," email ":  "[Email protected]"}, {"username": "test2", "Password": "1234", "email": "[email protected]"}][{"username" : "Test1", "Password": "1234", "email": "[email protected]"},{"username": "test2", "Password": "1234", "email": "[E  Mail protected] "}]> db.user.insert (stuff);  Bulkwriteresult ({"Writeerrors": [], "writeconcernerrors": [], "ninserted": 2, "nupserted": 0, "nmatched": 0, "nmodified" : 0, "nremoved": 0, "upserted": []}) > Db.user.find (). Pretty () {"_id": ObjectId ("53fc73bf784eb7aa025Aeb31 ")," username ":" Admon "," Password ":" 1234 "," email ":" [email protected] "} {" _id ": ObjectId (" 53fc741f784eb7aa025aeb32 ")," username ":" test1 "," Password ":" 1234 "," email ":" [email protected] "} {" _id ": ObjectId ("53fc741f784eb7aa025aeb33"), "username": "test2", "Password": "1234", "email": "[email protected]"}  >


Simple use of Mongodb

Related Article

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.