遇到问题---mongodb--Usage of 'UserCredentials' with 'MongoClient' is no longer supported
程序员文章站
2022-06-15 15:00:19
...
转载请注明出处:遇到问题—mongodb–Usage of ‘UserCredentials’ with ‘MongoClient’ is no longer supported
遇到问题
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模式。
解决方式
方式一monogo模式
使用简单的mongo模式配置如下:
<mongo:db-factory id="logMongoDbFactory"
host="${mongo.log.server}"
port="${mongo.log.port}"
dbname="${mongo.log.db}"
username="${mongo.log.user}"
password="${mongo.log.password}"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
方式二 mongoClient模式
mongoClient模式是比较新的配置方式,可以带更多的参数,在原配置基础上只需要调整一下账号密码的配置方式如下:
<mongo:mongo-client id="logMongoClient" host="${mongo.log.server}" port="${mongo.log.port}" credentials="${mongo.log.user}:${mongo.log.password}@${mongo.log.db}">
<!-- 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"
mongo-ref="logMongoClient"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
如果是副本集,则使用如下配置:
<mongo:mongo-client id="logMongoClient" replica-set="${mongo.log.host}:${mongo.log.port}" credentials="${mongo.log.user}:${mongo.log.password}@${mongo.log.db}">
<!-- 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"
mongo-ref="logMongoClient"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
注意集群下连接的mongodb的host为主服务器的地址,而非从服务器的地址。
转载请注明出处:遇到问题—mongodb–Usage of ‘UserCredentials’ with ‘MongoClient’ is no longer supported
上一篇: npm源管理