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个回答

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

woaichinaidiao
woaichinaidiao 我的是在Documents上注解配置的索引@CompoundIndexes,主库里已经建过了,从库里也能查到索引,但就是读取数据时,控制台报那个错又在从库中创建索引
大约 2 年之前 回复

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!