MongoDB username and password settings

Source: Internet
Author: User

Today, I started my study and summary on MongoDB. I feel that MongoDB is not as difficult as I think. However, if I am familiar with JavaScript, it is easier to learn about MongoDB.

The first step is to set MongoDB users and permissions. If you do not set users, you can directly use the Mongo command to enter the client Shell Interface for operations, but if you have not set users, I always felt something was missing, so after half a day of searching and practice, I almost figured out the users and permissions. Summary:

Run the following command to install the program: mongod -- install -- dbpath "C: \ Program Files \ MongoDB \ data \ DB" -- logpath "C: \ Program
Files \ MongoDB \ data \ log \ MongoDB. log"
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. 164fri APR 05 13:47:43. 168 warning: 32-bit servers don't have journaling enabled by default. please use -- journal if you want durability. fri APR 05 13:47:43. 169fri APR 05 13:47:43. 169 trying to install Windows Service 'mongodb 'Fri APR 05 13:47:43. 170 there is already a service named 'mongodb ', abortingc: \ Program Files \ MongoDB \ bin> Net start mongodbmongo dB service has been started successfully .. C: \ Program Files \ MongoDB \ bin> mongomongodb shell version: 2.4.1connecting to: testserver has startup Warnings: Fri APR 05 13:48:02. 516 [initandlisten] Fri APR 05 13:48:02. 516 [initandlisten] ** Note: This is a 32-bit MongoDB binary. fri APR 05 13:48:02. 516 [initandlisten] ** 32 bit builds are limited to less than 2 GB of data (or less with -- journal ). fri APR 05 13:48:02. 516 [initandlisten] ** note that journaling ults to off for 32 bit and is currently off. fri APR 05 13:48:02. 516 [initandlisten] ** see http://dochub.mongodb.org/core/32bitFri APR 05 13:48:02. 516 [initandlisten]> show dbsadmin (empty) Local 0.03125 GB> We can see that there are two default databases: Admin and local. Instance 1:Create a user with the username "root" and password "admin" as follows:> Use adminswitched to DB admin> dB. adduser ("root", "admin") {"user": "root", "readonly": false, "PWD": "bde0d84f6749c235a6b4e36d945eb666", "_ id ": objectid ("515e662430d89f61f6991c91")}> show collectionsfri APR 05 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> Note:When you use the show collections command above, an error is returned. Because you do not have the permission. Do the following:> dB. Auth ("root", "admin"); 1 Note:If 1 is returned, the verification is successful. If 0 is returned, the verification fails. Enter the following command: Show collections to view the collection under Admin.> Show collectionssystem. indexessystem. users> dB. system. users. find () {"_ id": objectid ("515e662430d89f61f6991c91"), "user": "root", "readonly": false, "PWD": "success"}> Instance 2:Create a student database under a user with the username "root" and password "admin", create a stu set in the student database, and insert a document as follows:> use studentswitched to DB student> dB. stu. insert ({"name": "maoyuanjun", "Age": 25, "sex": "male"})> dB. stu. find () {"_ id": objectid ("515e676630d89f61f6991c92"), "name": "maoyuanjun", "Age": 25, "sex": "male "} Log out of the server and log on again as follows:C: \ Program Files \ MongoDB \ bin> mongomongodb shell version: 2.4.1connecting to: Test> show dbsfri APR 05 13:58:05. 420 JavaScript Execution failed: listdatabases failed: {"OK": 0, "errmsg": "unauthorized"} At src/Mongo/Shell/Mongo. JS: l46 Error cause:Yes. First, determine whether you have the permission, as follows:> Use adminswitched to DB admin> dB. auth ("root", "admin") 1> show dbsadmin 0.0625 gblocal 0.03125 gbstudent 0.0625 GB> at this time, we can see that the user root, password admin, has an additional student database. Instance 3:Create a user c: \ Program Files \ MongoDB \ bin> mongomongodb shell version: 2.4.1connecting to: Test> Use adminswitched to DB Admin If you add a user directly, an error is returned:> DB. adduser ("test", "123456") Fri APR 05 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 Determine permissions:> DB. auth ("root", "admin"); 1> Use adminswitched to DB admin> dB. adduser ("test", 123456) {"user": "test", "readonly": false, "PWD": "c8ef9e7ab00406e84cfa807ec082f59e", "_ id ": objectid ("515e68e2be252e81c5dee198")}> show collectionssystem. indexessystem. users> dB. system. users. find () {"_ id": objectid ("515e662430d89f61f6991c91"), "user": "root", "readonly": false, "PWD": "success 5eb666 "} {" _ id ": objectid (" 515e68e2be252e81c5dee198 ")," user ":" test "," readonly ": false," PWD ": "c8ef9e7ab00406e84cfa807ec082f59e"}> you can see that you already have two accounts. After the above user is created, you can use Java code to determine whether the connection is successful:
Package COM. mongodbdemo; import java.net. unknownhostexception; import COM. mongoDB. DB; import COM. mongoDB. mongo; import COM. mongoDB. except exception;/** MongoDB connection and shutdown */public class mongodbutil {public static void main (string [] ARGs) {Mongo M = NULL; DB = NULL; try {// Mongo (param1, param2): param1 is the IP address param2 is the port M = new Mongo ("127.0.0.1", 27017 ); // obtain the MongoDB object DB = m Based on the MongoDB database name. getdb ("admin"); // check whether the user password is Correct if (! DB. Authenticate ("root", "admin". tochararray () {system. Out. println ("connecting to MongoDB database, Verification Failed! ");} Else {system. Out. println (" connected to the MongoDB database. Verification Successful! ") ;}} Catch (unknownhostexception e) {e. printstacktrace ();} catch (except exception e) {e. printstacktrace ();} finally {// disable try {M. close ();} catch (exception ee) {ee. printstacktrace () ;}m = NULL ;}}}

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.