Java驱动远程连接mongoDB(简明易懂版)

栏目: Java · 发布时间: 8年前

内容简介:Java驱动远程连接mongoDB(简明易懂版)

mongodb默认是不能远程连接的,而且在 linux 安装完你会发现,它的目录极其简单,连个配置文件都没有. 我的 mongodb 的版本是3.6,目前最新的. https://www.mongodb.com/mongodb-3.6

百度了一下看是有配置文件: mongodb.conf. 又检查了下目录确实没有.然后只能自己创建了.

在官方文档找了关于配置文件的介绍:  https://docs.mongodb.com/manual/reference/configuration-options/

文档很长:

Configuration File 

File Format 

Use the Configuration File 

Core Options 

 systemLog Options 

processManagement Options 

net Options 

security Options 

setParameter Option 

storage Options 

operationProfiling Options 

replication Options 

sharding Options 

auditLog Options 

snmp Options 

Text Search Options 

mongos-only Options 

Windows Service Options

我把与远程连接及log文件有关的标红了, 如果只是修改远程连接, 这几个重点看下就可以.

文件格式

配置文件使用  YAML 格式, 应该都见过,没见过自行baidu..

一个简单的配置文件:

systemLog:
   destination: file
   #mongod或mongos应发送所有诊断日志信息的日志文件的路径,而不是标准输出或主机的syslog.MongoDB在指定的路径上创建日志文件。
   path: "/home/xxx/mongod.log"
   #追加在文件后边
   logAppend: false
storage:
   #数据库文件存放位置
   dbPath:"/home/xxx/db"
processManagement:
   fork: true
net:
   #绑定所有的ip地址:0.0.0.0
   bindIp: 0.0.0.0
   port: 27017
security:
   authorization: disabled

path dbPath   换成你自己的路径,其中 mongod.log 是存放控制台日志的地方.

启动

使用配置文件启动

mongod --config ./mongod.conf​

mongod -f /etc/mongod.conf​

Java程序

一个超级简单的 java 程序

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import org.bson.Document;

public class QuickTour {
	MongoClient mongoClient;
	
	public void connect(){
		mongoClient = new MongoClient("10.80.18.1");
		MongoDatabase database = mongoClient.getDatabase("firstDB");
		MongoCollection<Document> collection = database.getCollection("firstCollection1");
		Document myDoc = collection.find().first();
		System.out.println(myDoc.toJson());
	}
	
	public static void main(String[] args) {
		QuickTour quickTour = new QuickTour();
		quickTour.connect();
	}
}

输出:

一月 22, 2018 8:15:35 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Cluster created with settings {hosts=[10.80.18.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
一月 22, 2018 8:15:36 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=10.80.18.1:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
一月 22, 2018 8:15:36 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Opened connection [connectionId{localValue:1, serverValue:1}] to 10.80.18.1:27017
一月 22, 2018 8:15:36 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Monitor thread successfully connected to server with description ServerDescription{address=10.80.18.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 2]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, roundTripTimeNanos=758049}
一月 22, 2018 8:15:36 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Opened connection [connectionId{localValue:2, serverValue:2}] to 10.80.18.1:27017
{ "_id" : { "$oid" : "5a631d80070db90c43a3477d" }, "x" : 1.0 }

成功!


以上所述就是小编给大家介绍的《Java驱动远程连接mongoDB(简明易懂版)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

上瘾

上瘾

[美] 尼尔·埃亚尔、[美] 瑞安·胡佛 / 钟莉婷、杨晓红 / 中信出版集团 / 2017-5 / 49.00元

——为什么我们会习惯性地点开某个App? ——这种使用习惯到底是如何养成的? ——为什么有些产品能让我们戒不掉,而其他的产品却不行? ——是否有什么秘诀能让用户对你的产品形成使用习惯,欲罢不能? 《上瘾》揭示了很多让用户形成使用习惯,甚至“上瘾”的互联网产品服务背后的基 本设计原理,告诉你怎样打造一款让用户欲罢不能的产品。作者根据自己多年的研究、咨询及实际经验,提出了新颖而......一起来看看 《上瘾》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具