MongoDB database user name and password settings

Source: Internet
Author: User

The first is the MongoDB user and permissions settings, if you do not set the user, the direct use of the MONGO command can enter the client shell interface to operate, but if not set up the user, the total feeling of something less, so after a half-day search and practice, almost the user and permissions to understand. Summarized as follows:

If you follow this command, install the following:

Mongod--install--dbpath "C:\Program files\mongodb\data\db"--logpath "C:\Program Files\mongodb\data\log\mongodb.log "

As follows:

C:\Program files\mongodb\bin>mongod--install--dbpath "C:\Program files\mongodb\data\db"--logpath "C:\Program Files\mongodb\data\log\mongodb.log "

Fri APR 05 13:47:43.164

Fri APR 13:47:43.168 warning:32-bit servers don ' t has journaling enabled by default. Please use the--journal if you want durability.

Fri APR 05 13:47:43.169

Fri APR 13:47:43.169 Trying to install Windows service ' MongoDB '

Fri APR 13:47:43.170 There is already a service named ' MongoDB ', aborting

C:\Program files\mongodb\bin>net Start MongoDB

Mongo DB Service has been successfully activated by the start-up.

C:\Program Files\mongodb\bin>mongo

MongoDB Shell version:2.4.1

Connecting To:test

Server has startup warnings:

Fri APR 13:48:02.516 [Initandlisten]

Fri APR 13:48:02.516 [initandlisten] * * Note:this is a A-bit MongoDB binary.

Fri APR 13:48:02.516 [Initandlisten] * * + builds is limited to less than 2GB of data (or less with--journa L).

Fri APR 13:48:02.516 [initandlisten] * * Note that journaling defaults to off for the + is currently off.

Fri APR 13:48:02.516 [Initandlisten] * * See Http://dochub.mongodb.org/core/32bit

Fri APR 13:48:02.516 [Initandlisten]

> Show DBS

Admin (empty)

Local 0.03125GB

>

You can see that there are two default database admin and Local

Example 1: Create a user named root, password for admin, as follows:

> Use admin

Switched to DB admin

> Db.adduser ("Root", "admin")

{

"User": "Root",

"ReadOnly": false,

"pwd": "bde0d84f6749c235a6b4e36d945eb666",

"_id": ObjectId ("515e662430d89f61f6991c91")

}

> Show Collections

Fri Apr 13:50:36.685 JavaScript execution Failed:error: {

"$err": "Not authorized for query on Admin.system.namespaces",

"Code": 16550

} at src/mongo/shell/query.js:l128

>

Description: Use the above command show collections, found an error. is because there is no permission. Do these things:

> Db.auth ("Root", "admin");

1

Description: Returning 1 indicates that the validation was successful, and returning 0 indicates that the validation failed.

At this point, enter the following command: Show collections can see the collection under Admin.

> Show Collections

System.indexes

System.users

> Db.system.users.find ()

{"_id": ObjectId ("515e662430d89f61f6991c91"), "user": "Root", "readOnly": false, "pwd": "bde0d84f6749c235a6b4e36d945e B666 "}

>

Example 2: Create a student database under User name root, password admin, and create a Stu collection in the student database and insert a document as follows:

> Use Student

Switched to DB student

> Db.stu.insert ({"Name": "Maoyuanjun", "age": +, "sex": "Male"})

> Db.stu.find ()

{"_id": ObjectId ("515e676630d89f61f6991c92"), "name": "Maoyuanjun", "age": +, "sex": "Male"}

To exit the server, re-login as follows:

C:\Program Files\mongodb\bin>mongo

MongoDB Shell version:2.4.1

Connecting To:test

> Show DBS

Fri Apr 13:58:05.420 JavaScript execution failed:listdatabases failed:{"OK": 0, "errmsg": "Unauthorized"}

At src/mongo/shell/mongo.js:l46

cause of Error: no permissions. The first is to determine whether there is permission, as follows:

> Use admin

Switched to DB admin

> Db.auth ("Root", "admin")

1

> Show DBS

Admin 0.0625GB

Local 0.03125GB

Student 0.0625GB

>

At this point, we can see the user root, password Admin user under a student database.

Example 3: Creating a user

C:\Program Files\mongodb\bin>mongo

MongoDB Shell version:2.4.1

Connecting To:test

> Use admin

Switched to DB admin

Adding users directly will error:

> Db.adduser ("Test", "123456")

Fri Apr 14:01:31.404 JavaScript execution failed:error: {"$err": "Not authorized for query on Admin.system.users", " Code ": 16550} at src/mongo/shell/query.js:l128

To do the right to judge:

> Db.auth ("Root", "admin");

1

> Use admin

Switched to DB admin

> Db.adduser ("Test", 123456)

{

"User": "Test",

"ReadOnly": false,

"pwd": "c8ef9e7ab00406e84cfa807ec082f59e",

"_id": ObjectId ("515e68e2be252e81c5dee198")

}

> Show Collections

System.indexes

System.users

> Db.system.users.find ()

{"_id": ObjectId ("515e662430d89f61f6991c91"), "user": "Root", "readOnly": FA

LSE, "PWD": "bde0d84f6749c235a6b4e36d945eb666"}

{"_id": ObjectId ("515e68e2be252e81c5dee198"), "User": "Test", "readOnly": FA

LSE, "PWD": "c8ef9e7ab00406e84cfa807ec082f59e"}

>

MongoDB database user name and password settings

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.