spring-data-mongodb mongo2.X驱动连接mongodb3.X服务器用户认证失败的解决办法
spring-data-mongodb mongo2.X驱动连接mongodb3.X服务器用户认证失败的解决办法
流年_浮华诺言 发表于10个月前
spring-data-mongodb mongo2.X驱动连接mongodb3.X服务器用户认证失败的解决办法
  • 发表于 10个月前
  • 阅读 115
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

由于mongodb3.X后版本改变了用户认证机制,由原先默认的"MONGODB-CR"认证方式改为了"SCRAM_SHA_1",mongo2.X驱动只支持"MONGODB-CR"方式的认证,所以程序连接时会报认证失败错误

解决办法:

首先关闭认证,修改system.version文档里面的authSchema版本为3,初始安装时候应该是5,命令行如下: 

> use admin 
switched to db admin 
> var schema = db.system.version.findOne({"_id" : "authSchema"}) 
> schema.currentVersion = 3 
3 
> db.system.version.save(schema) 
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 

不过如果你现在开启认证,仍然会提示AuthenticationFailed MONGODB-CR credentials missing in the user document 原因是原来创建的用户已经使用了SCRAM-SHA-1认证方式

> use admin
switched to db admin
> db.system.users.find()
[...]
{ "_id" : "userdb.myuser", "user" : "myuser", "db" : "userdb", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "XXXXXXXXXXXXXXXXXXXXXXXX", "storedKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXX", "serverKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXX" } }, "roles" : [ { "role" : "dbOwner", "db" : "userdb" } ] }

解决方式就是删除刚刚创建的用户,重新重建即可:

> use userdb
switched to db userdb
> db.dropUser("myuser")
true
>db.createUser({user:'myuser',pwd:'123456',roles:[{role:'dbOwner',db:'userdb'}]})

 

附上spring整合mongodb2.X驱动的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载mongodb的属性配置文件 -->
    <context:property-placeholder location="classpath:mongodb.properties"/>
    <mongo:mongo id="mongo" replica-set="${mongo.relicatSet.host}">
        <mongo:options connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
                       auto-connect-retry="${mongo.autoConnectRetry}" socket-keep-alive="${mongo.socketKeepAlive}"
                       socket-timeout="${mongo.socketTimeout}" slave-ok="false"/>
    </mongo:mongo>
    <mongo:db-factory id="mongoDbFactory" dbname="o2o_store" mongo-ref="mongo" username="${mongo.username}"
                      password="${mongo.password}"/>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
    </bean>
</beans>

spring整合mongodb3.X驱动的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载mongodb的属性配置文件 -->
    <context:property-placeholder
            location="classpath:mongodb.properties"/> <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo, --> <!-- MongoClient -->
    <bean id="mongoClient" class="com.mongodb.MongoClient">
        <constructor-arg name="credentialsList">
            <list> <!-- Credentials -->
                <bean class="com.mongodb.MongoCredential">
                    <constructor-arg name="mechanism" value="#{T(com.mongodb.AuthenticationMechanism).SCRAM_SHA_1}"/>
                    <constructor-arg type="java.lang.String" name="userName" value="${mongo.username}"/>
                    <constructor-arg type="java.lang.String" name="source" value="${mongo.dbname}"/>
                    <constructor-arg type="char[]" name="password" value="${mongo.password}"/>
                </bean>
            </list>
        </constructor-arg>
        <constructor-arg name="seeds">
            <list>
                <bean class="com.mongodb.ServerAddress">
                    <constructor-arg type="java.lang.String" name="host" value="${mongo.sharded_clusters1.host}"/>
                    <constructor-arg type="int" name="port" value="${mongo.sharded_clusters1.port}"/>
                </bean>
                <bean class="com.mongodb.ServerAddress">
                    <constructor-arg type="java.lang.String" name="host" value="${mongo.sharded_clusters2.host}"/>
                    <constructor-arg type="int" name="port" value="${mongo.sharded_clusters2.port}"/>
                </bean>
                <bean class="com.mongodb.ServerAddress">
                    <constructor-arg type="java.lang.String" name="host" value="${mongo.sharded_clusters2.host}"/>
                    <constructor-arg type="int" name="port" value="${mongo.sharded_clusters3.port}"/>
                </bean>
            </list>
        </constructor-arg>
    </bean> <!-- MongoDbFactory -->
    <bean id="simpleMongoDbFactoryID" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
        <constructor-arg ref="mongoClient"/>
        <constructor-arg name="databaseName" value="${mongo.dbname}"/>
    </bean> <!-- MongoTemplate -->
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="simpleMongoDbFactoryID"/>
    </bean>
</beans>

 

共有 人打赏支持
粉丝 0
博文 7
码字总数 3196
×
流年_浮华诺言
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: