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 }

 

成功!

轉發注明出處: http://www.cnblogs.com/jycboy/p/8331019.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM