com.mongodb.MongoQueryException: Query failed with error code 13

今天在用spring-data-mongo 配置mongodb的时候碰到个bug——com.mongodb.MongoQueryException: Query failed with error code 13。这个bug是说我鉴权没有通过,但我通过shell去访问是好的。
配置用的是spring官方的文档的配置:

<context:property-placeholder location="classpath:/com/myapp/mongodb/config/mongo.properties"/>

<mongo:mongo host="${mongo.host}" port="${mongo.port}">
  <mongo:options
     connections-per-host="${mongo.connectionsPerHost}"
     threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
     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="${mongo.slaveOk}"
     write-number="1"
     write-timeout="0"
     write-fsync="true"/>
</mongo:mongo>

<mongo:db-factory dbname="database" mongo-ref="mongo"/>

<bean id="anotherMongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
</bean>

这里要说一下mongodb3.0以后鉴权的方式就改变了,所以之前安装mongodb可视化工具的时候也碰到鉴权不通过的问题,也是因为mongodb版本问题。所以通过以上这段xml配置鉴权的方式只能在mongodb3.0版本以前,3.0版本以后要用下面的配置。

    property-placeholder location="classpath:mongo.properties" file-encoding="UTF-8"/>

    id="mongo-client"  host="${mongo.host}" port="${mongo.port}" credentials="${mongo.username}:${mongo.password}@${mongo.database}">
        write-concern="1"/>
    

    id="mongoDbFactory" dbname="${mongo.database}" mongo-ref="mongo-client" />

    id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">    
        name="mongoDbFactory" ref="mongoDbFactory"/>    
    

参考:http://stackoverflow.com/questions/30634987/com-mongodb-mongoqueryexception-query-failed-with-error-code-13

你可能感兴趣的:(java,mongodb,bug)