Java连接MongoDB
2015-12-06 03:13
549 查看
1.MongoDB没有添加使用auth认证方式
2.MongoDB启动的时候添加--auth参数启动MongoDB(即安全认证模式下)
MongoClientURI uri = new MongoClientURI("mongodb://192.168.32.129:27017/local", MongoClientOptions.builder().cursorFinalizerEnabled(false)); MongoClient client = new MongoClient(uri); MongoDatabase db=client.getDatabase("local"); MongoCollection<Document> collection = db.getCollection("system.users"); List<Document> foundDocument = collection.find().into(new ArrayList<Document>()); System.out.println(foundDocument);
2.MongoDB启动的时候添加--auth参数启动MongoDB(即安全认证模式下)
private static MongoClient mongoClient = null; private static MongoDatabase database = null; private static ServerAddress serverAddress = null; private static MongoCredential credentials = null; private static List<ServerAddress> addressLists =new ArrayList<ServerAddress>(); private static List<MongoCredential> credentialsLists = new ArrayList<MongoCredential>(); try{ //mongoClient = new MongoClient("192.168.32.129",27017); serverAddress = new ServerAddress("192.168.32.129",27017); addressLists.add(serverAddress); credentials = MongoCredential.createMongoCRCredential("admin", "admin", "admin".toCharArray()); credentialsLists.add(credentials); mongoClient = new MongoClient(addressLists, credentialsLists); }catch (MongoException e) { System.out.println(e.toString()); } if(null != mongoClient){ database = mongoClient.getDatabase("admin"); /* collection = database.getCollection("system.users"); foundDocument = collection.find().into( new ArrayList<Document>()); System.out.println(foundDocument);*/ }
相关文章推荐
- MongoDB学习系列(二)mongo聚合查询实例
- MongoDB学习系列(一)mongo简介及相关操作
- MongoDB
- Day 25: 联合Tornado、MongoDB 和 AngularJS 进行应用开发
- Day 22: 使用Spring、MongoDB和AngularJS开发单页面应用
- mongoDB 3.0 安全权限访问控制
- mongodb
- mongodb格式化输出查询结果
- Mongodb mmapv1存储引擎解析
- 启动MongoDB的授权认证校验
- Mongodb如何使用wiredTiger?
- linux下安装mongodb
- mongodb查询部分字段方法
- mongodb3.0 无法运行问题
- Mongodb分布式分片集群安装和理解
- MongoDB分组统计的一个例子
- mongodb的基本操作(crud)
- 使用NodeJS+AngularJS+MongoDB实现一个Web数据扒取-分析-展示的系统
- mongodb3.0.6的下载和安装
- mongodb 数据库操作--备份 还原 导出 导入