我设置了2个mongo容器,其端口为27017:27017和27018:27017。 mongo1设置为主,mongo2设置次要,并使用副本集。

mongo1作为主要对象同时具有写,读访问权限,而mongo2仅具有读访问权限

spring.data.mongodb.uri = mongodb://abc.xyz:27017/user_demo
//monog1, abc.xyz is the hostname

spring.data.mongodb.uri = mongodb://abc.xyz:27018/user_demo
//mongo2

如果我在正常的Spring Boot程序中仅连接monogo1,则它可以工作(读,写),如果我设置mongo2则可以工作(仅读),但是当我在一行中同时设置这两个时,它将出错
spring.data.mongodb.uri = mongodb://abc.xyz:27017,abc.xyz:27018/user_demo?replicaSet=idea-mongo-set

我通过存储在mongo1中来检查副本集是否正常工作,在mongo2中我可以读取

因此,最后一行出了什么问题。还有其他方法可以为副本集存储uri

在官方spring site中给出
spring.data.mongodb.uri=mongodb://user:[email protected]:12345,mongo2.example.com:23456/test

我得到的错误是
2019-01-08 15:56:35.245  INFO 29804 --- [           main] c.j.bootifulmongodb.BootMongoDBApp       : Started BootMongoDBApp in 3.603 seconds (JVM running for 4.373)
2019-01-08 15:56:54.466  INFO 29804 --- [azure.com:27018] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server abc.xyz:27018
com.mongodb.MongoSocketOpenException: Exception opening socket
        at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar:na]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.net.SocketTimeoutException: connect timed out
        at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_181]
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_181]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_181]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_181]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_181]
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_181]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
        at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
        at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.3.jar:na]
        at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.3.jar:na]
        ... 3 common frames omitted

最佳答案

您的uri似乎在连接副本集的正确轨道上。

spring.data.mongodb.uri = mongodb://abc.xyz:27017,abc.xyz:27018/user_demo?replicaSet=idea-mongo-set

此处省略凭据(用户,密码)只是错字吗?

这是我们用于副本集连接uri的确切语法:
mongodb://user:pwd@server1:27017,server2:27017,server3:27017/our_collection?replicaSet=replica_set_name&authSource=authagainstthiscollection&authMechanism=SCRAM-SHA-1

关于mongodb - Spring Boot连接到Mongo容器运行的MongoDB副本集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54086832/

10-12 07:39