springboot启动时候报错mongodb问题
作者:xyw10000
springboot启动时候报错mongodb
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:?]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:?]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) [mongodb-driver-core-3.4.2.jar:?]
at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[?:1.7.0_79]
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[?:1.7.0_79]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) ~[?:1.7.0_79]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) ~[?:1.7.0_79]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) ~[?:1.7.0_79]
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[?:1.7.0_79]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.7.0_79]
at java.net.Socket.connect(Socket.java:579) ~[?:1.7.0_79]
启动spring boot 结果直接报mongodb错误,挺奇怪的工程还没有配置mongodb 。
猜想是不是工程有mongodb jar包
然后 boot 自然配置了
查了资料原来
springboot自动配置了支持mongodb。在启动springboot时会自动实例化一个mongo实例。
现在要禁用自动配置
@SpringBootApplication(exclude = MongoAutoConfiguration.class):
这个注解可以禁用springboot自带的配置。
在SpringBoot使用MongoDB时出现的bug和解决
在springboot整合MongoDB时,报错
INFO 67135 — [ main] org.mongodb.driver.connection : Closed connection [connectionId{localValue:2}] to 127.0.0.1:27017 because there was a socket exception raised by this connection.
org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘root’, source=‘demo’, password=, mechanismProperties=}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName=‘root’, source=‘demo’, password=, mechanismProperties=}
报错的话说是权限什么的,可以把properties文件中mongodb.uri改一下:
spring.data.mongodb.uri=mongodb://root:123456@127.0.0.1:27017/demo?authSource=admin&authMechanism=SCRAM-SHA-1
root是账号,123456是密码,demo是数据库名字,改为自己的
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。