遇到問題---mongodb--Usage of 'UserCredentials' with 'MongoClient' is no longer supported
阿新 • • 發佈:2019-01-09
遇到問題
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"
dbname="${mongo.log.db}"
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"
dbname="${mongo.log.db}"
mongo-ref="logMongoClient"/>
<bean id="logMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="logMongoDbFactory"/>
</bean>
注意事項
注意叢集下連線的mongodb的host為主伺服器的地址,而非從伺服器的地址。
然後mongo:db-factory需要帶 dbname引數,即使credentials="{mongo.log.password}@${mongo.log.db}"中已經帶了db的引數,但是這個是認證用的。
否則會報錯無法通過認證。