Java Mongo驱动程序无意中连接到本地主机
我下载了用于java的mongo驱动程序(mongo-java-driver-3.12.4.jar),并将MongoClient连接到远程主机 与远程主机的连接已成功建立。 但除此之外,日志中还有错误消息,表明驱动程序试图连接到没有运行mongo db的本地主机:Java Mongo驱动程序无意中连接到本地主机,java,mongodb,mongodb-java,Java,Mongodb,Mongodb Java,我下载了用于java的mongo驱动程序(mongo-java-driver-3.12.4.jar),并将MongoClient连接到远程主机 与远程主机的连接已成功建立。 但除此之外,日志中还有错误消息,表明驱动程序试图连接到没有运行mongo db的本地主机: 09:52:52,082 INFO [org.mongodb.driver.cluster] (default task-1) Cluster created with settings {hosts=[127.0.0.1:2701
09:52:52,082 INFO [org.mongodb.driver.cluster] (default task-1) Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
09:52:52,843 INFO [org.mongodb.driver.cluster] (default task-1) Cluster created with settings {hosts=[remoteHost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
09:52:52,879 INFO [org.mongodb.driver.cluster] (default task-1) Cluster description not yet available. Waiting for 30000 ms before timing out
09:52:53,159 INFO [org.mongodb.driver.connection] (cluster-ClusterId{value='5ef457d443933341aa40d5f3', description='null'}-remoteHost:27017) Opened connection [connectionId{localValue:2, serverValue:373}] to remoteHost:27017
09:52:53,219 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ef457d443933341aa40d5f3', description='null'}-remoteHost:27017) Monitor thread successfully connected to server with description ServerDescription{address=remoteHost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 18]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=54329200}
09:52:53,707 INFO [org.mongodb.driver.connection] (default task-1) Opened connection [connectionId{localValue:3, serverValue:374}] to remoteHost:27017
09:52:54,198 INFO [org.mongodb.driver.cluster] (cluster-ClusterId{value='5ef457d443933341aa40d5f2', description='null'}-127.0.0.1:27017) Exception in monitor thread while connecting to server 127.0.0.1:27017: com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:128)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:64)
at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79)
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65)
... 3 more
我使用MongoClient(服务器地址地址、MongoCredential凭证、MongoClientOptions)
设置到远程主机的连接设置。我从不使用localhost或127.0.0.1
为什么驱动程序会尝试连接到本地主机,更重要的是,如何禁用此行为?:)
编辑:
以下是初始化MongoClient的方式:
{
...
String connectionString = "mongodb://user:password@remoteHost:27017/";
MongoClientURI clientURI = new MongoClientURI(connectionString);
mongoClient = new MongoClient(createServerAddress(clientURI.getHosts().get(0)), clientURI.getCredentials(), addOptions(clientURI.getOptions()));
MongoDatabase db = mongoClient.getDatabase(dbName);
...
}
private ServerAddress createServerAddress(String host) {
if (host.contains(":")) {
String[] parts = host.split(":");
return new ServerAddress(parts[0], Integer.parseInt(parts[1]));
} else {
return new ServerAddress(host);
}
}
private MongoClientOptions addOptions(MongoClientOptions options) {
Builder builder = new Builder(options);
File ksFile = new File(config.getKeystorePath()));
char[] password = config.getKeystorePassword()).toCharArray();
if (ksFile.exists()) {
try {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try (FileInputStream fis = new FileInputStream(ksFile)) {
ks.load(fis, password);
}
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
builder.sslEnabled(true);
builder.sslContext(sslContext);
} catch (Exception e) {
logger.error("TLS initialization failed: ", e.getCause());
}
}
return builder.build();
}
演示如何将
MongoClient()
与参数一起使用。是否需要自己解析该服务器地址?MongoClient不适用于MongoClient吗?从日志中可以看出,您正在创建两个MongoClient实例,一个到远程主机(使用您显示的代码),另一个到其他地方。从MongoClient
类文档:“/database是要登录的数据库的名称,因此只有在使用username:password@语法时才相关。如果未指定,默认情况下将使用“admin”数据库。“。假设用户/pwd已通过管理员数据库的身份验证。如果我想添加MongoClient,我不能仅使用MongoClient。没有其他代码可以让我创建第二个实例。用户根据管理数据库进行身份验证。所需的连接按预期工作,但我不知道为什么驱动程序尝试连接到localhost。向我们展示如何使用带有参数的MongoClient()
。您需要自己解析该服务器地址吗?MongoClient不适用于MongoClient吗?从日志中可以看出,您正在创建两个MongoClient实例,一个到远程主机(使用您显示的代码),另一个到其他地方。从MongoClient
类文档:“/database是要登录的数据库的名称,因此只有在使用username:password@语法时才相关。如果未指定,默认情况下将使用“admin”数据库。“。假设用户/pwd已通过管理员数据库的身份验证。如果我想添加MongoClient,我不能仅使用MongoClient。没有其他代码可以让我创建第二个实例。用户根据管理数据库进行身份验证。想要的连接按预期工作,但我不知道为什么驱动程序尝试连接到localhost。