Authentication 설정


1. mongoDB 실행

2012:01:30 12:46:13



2. cmd창을 하나 더  열어서 쉘 접속

2012:01:30 12:48:43



3.  Database에 사용자 생성

2012:01:30 12:39:08



4. 테스트

2012:01:30 12:51:46


 5. mongoDB Secure Mode로 재시작

2012:01:30 12:33:50

 

6. JAVA 샘플 코드

package com.jeonguk.core;

import java.net.UnknownHostException;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

public class AuthTest {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("jeongukdb");
boolean auth = db.authenticate("jk", "1111".toCharArray());
System.out.println("auth : "+auth);
DBCollection collection = db.getCollection("jeongukCollection");
            // search query
            BasicDBObject searchQuery = new BasicDBObject();
            searchQuery.put("id", 1001);
   
           // query it
           DBCursor cursor = collection.find(searchQuery);
   
           // loop over the cursor and display the retrieved result
           while (cursor.hasNext()) {
            System.out.println(cursor.next());
           }
   
System.out.println("Done");
} catch(UnknownHostException e) {
e.printStackTrace();
} catch(MongoException e) {
e.printStackTrace();
}

}

}
 

신고
Posted by jeonguk