Usage of 'UserCredentials' with 'MongoClient' is no longer supported(1)
- UID
- 1066743
|
Usage of 'UserCredentials' with 'MongoClient' is no longer supported(1)
遇到问题
SpringMVC的项目中使用spring-data-mongodb驱动包进行读写mongodb。
包的pom.xml如下
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.9.1.RELEASE</version>
</dependency>
mongdb.xml配置如下:
<mongo:mongo-client id="logMongoClient" host="${mongo.log.server}" port="${mongo.log.port}" >
<!-- com.mongodb.MongoClientOptions -->
<mongo:client-options
connections-per-host="${mongo.log.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.log.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.log.connectTimeout}"
max-wait-time="${mongo.log.maxWaitTime}"
socket-keep-alive="${mongo.log.socketKeepAlive}"
socket-timeout="${mongo.log.socketTimeout}"
max-connection-life-time="${mongo.log.connectLifeTime}"/>
</mongo:mongo-client>
<mongo:db-factory id="logMongoDbFactory"
dbname="${mongo.log.db}"
username="${mongo.log.user}"
password="${mongo.log.password}"
mongo-ref="logMongoClient"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
参数配置如下:
#log mongodb
mongo.log.user=test
mongo.log.password=
mongo.log.server=192.168.30.11
mongo.log.port=28717
mongo.log.db=db_log
mongo.log.connectionsPerHost=100
mongo.log.threadsAllowedToBlockForConnectionMultiplier=5
mongo.log.connectTimeout=10000
mongo.log.maxWaitTime=120000
mongo.log.socketKeepAlive=false
mongo.log.socketTimeout=0
#1 hour
mongo.log.connectLifeTime=3600000
mongodb服务更改为副本集,增加账号密码后使用原配置连接报错如下:
Constructor threw exception; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Usage of 'UserCredentials' with 'MongoClient' is no longer supported. Please use 'MongoCredential' for 'MongoClient' or just 'Mongo'.
原因
配置过时,当前配置不符合集群副本集的认证机制。
旧的配置在单机模式下带账号密码认证是ok的,可以正常访问。
但是修改成副本集模式之后,这种配置无效。根据提示,应该使用MongoCredential的认证配置。
或者只使用mongo模式。 |
|
|
|
|
|