springboot连接mongodb的一些问题

前言

今天用springboot集成mongodb的时候报了一些错误

第一个

org.springframework.data.mongodb.UncategorizedMongoDbException: 
Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='zh',
source='main', password=<hidden>, mechanismProperties={}}; nested exception is
com.mongodb.MongoSecurityException: Exception authenticating
MongoCredential{mechanism=SCRAM-SHA-1, userName='zh', source='main',
password=<hidden>, mechanismProperties={}}
复制代码

这是我的配置文件

原因很简单,我用idea连接mongodb的插件上发现的,需要指定授权库以及授权机制

解决办法

在uri后面加上 ?authSource=admin&authMechanism=SCRAM-SHA-1

mongodb://user:pwd@host:port/database?main?authSource=admin&authMechanism=SCRAM-SHA-1
复制代码

第二个

类型转换问题

我在这里使用了@Id注解 然后报了这个错

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.bson.types.ObjectId] to type [long]
复制代码

这个问题吗,我就懒得想了,直接用json接收了

太懒了!呜呜呜

发布了136 篇原创文章 · 获赞 43 · 访问量 18万+
展开阅读全文
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符

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

07-22

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 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览