woaichinaidiao
woaichinaidiao
采纳率0%
2017-07-22 07:22 阅读 3.4k

spring-data-mongodb,读写分离,对从库读取时,发生创建索引异常,如何解决!求帮忙!

Exception in thread "main" org.springframework.data.mongodb.UncategorizedMongoDbException: The server is not the primary and did not execute the operation; nested exception is com.mongodb.MongoNotPrimaryException: The server is not the primary and did not execute the operation
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:162)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForAndCreateIndexes(MongoPersistentEntityIndexCreator.java:133)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.checkForIndexes(MongoPersistentEntityIndexCreator.java:125)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:109)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.onApplicationEvent(MongoPersistentEntityIndexCreator.java:51)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:380)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)
at org.springframework.data.mapping.context.AbstractMappingContext.addPersistentEntity(AbstractMappingContext.java:336)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:180)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:140)
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentEntity(AbstractMappingContext.java:67)
at org.springframework.data.mongodb.core.MongoTemplate.determineCollectionName(MongoTemplate.java:2015)
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:581)
at cn.com.test.db.routing.BasicRoutingMongoOperations.findOne(BasicRoutingMongoOperations.java:161)
at cn.com.test.db.dao.impl.MongoBaseDaoImpl.findOne(MongoBaseDaoImpl.java:240)
at cn.com.test.rms.test.TestOrder.queryOne(TestOrder.java:138)
at cn.com.test.rms.test.TestOrder.main(TestOrder.java:54)
Caused by: com.mongodb.MongoNotPrimaryException: The server is not the primary and did not execute the operation
at com.mongodb.connection.ProtocolHelper.createSpecialException(ProtocolHelper.java:171)
at com.mongodb.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:111)
at com.mongodb.connection.CommandProtocol.execute(CommandProtocol.java:114)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
at com.mongodb.connection.DefaultServerConnection.command(DefaultServerConnection.java:176)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:216)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:207)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:146)
at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:139)
at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:150)
at com.mongodb.operation.CreateIndexesOperation$1.call(CreateIndexesOperation.java:144)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:422)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:413)
at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:144)
at com.mongodb.operation.CreateIndexesOperation.execute(CreateIndexesOperation.java:71)
at com.mongodb.Mongo.execute(Mongo.java:845)
at com.mongodb.Mongo$2.execute(Mongo.java:828)
at com.mongodb.DBCollection.createIndex(DBCollection.java:1618)
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:142)
... 18 more

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • oyljerry oyljerry 2017-07-22 07:46

    创建索引要在主库,从库只读取数据。

    点赞 评论 复制链接分享
  • qq_34730277 南宫文凯 2017-07-22 08:32

    创建索引主库异常,数据库删除该索引重新创建,注意命名规范

    点赞 评论 复制链接分享

相关推荐