The following actions are based on the Windows platform
1. Start MongoDB Service
Mongod.exe--dbpath d:\mongodb-win32-x86_64-2.2.2\data--logpath d:\mongodb-win32-x86_64-2.2.2\log\mongo.log--auth
"--auth": Represents the need to access according to permissions, you must start the service with this parameter, the client can be a user name password to establish a connection
2. Start MONGO console MONGO
- D:\mongodb-win32-x86_64-2.2.2\bin>mongo
- MongoDB Shell version:2.2.2
- Connecting To:test
- >
3. Switch to the database you want to set up, create a user
- > Use Caotian
- Switched to DB Caotian
- > Db.adduser ("Ctadmin", "111111");
- {
- "User": "Ctadmin",
- "ReadOnly": false,
- "pwd": "790457ca1e19341d5043766e9192c5b9",
- "_id": ObjectId ("549943e6cbd0bdf00f57783b")
- }
- >
4. Establishing a connection in Java
- try {
- ServerAddress sa = new ServerAddress ("localhost", "27017");
- Mongooptions options = new Mongooptions ();
- Options.connectionsperhost = 200;//pool size (the drive's link pool size). /* Mongod--dbpath. \db--maxconns 20 (concurrent number) */default is 10
- This.mongo = new MONGO (SA, options);
- This.db = This.mongo.getDB ("Caotian");
- char[] Pwd_char = "111111". ToCharArray ();
- Boolean auth = db.authenticate ("Ctadmin", Pwd_char);
- if (!auth) {
- Logger.info ("======mongodb Initialization of permission validation error ======");
- throw new RuntimeException ();
- }
- Logger.info ("======mongodb initialization completed ======");
- } catch (Unknownhostexception e) {
- E.printstacktrace ();
- } catch (Mongoexception e) {
- E.printstacktrace ();
- }
MongoDB set access permissions, set user, user name password access in Java