1 Exception in thread "main" com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='user', source='userdb', password=<hidden>, mechanismProperties={}}
2 Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server 你的IP:你的端口. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }

出现以上springboot 2.x连接 mongdb 3.x的情况。

直面意思,就是指认证失败。

情况有4种,

1.你的认证方式有问题

2.认证信息有误(用户名密码等配错了)

3.mongodb数据库问题

4.springboot yml配置错误


1.你的认证方式有问题

SCRAM-SHA-1:就是mongodb 3.x之后默认的认证方式,springboot 2.x之后也是可以选择这种方式。
注意:mechanism=SCRAM-SHA-1。如果你不是这种方式,你就要升级你的springboot版本,或者去修改你的mongdb数据库的认证方式/降低mongodb版本。

你用纯javamongodb-driver包测试时,可见com.mongodb.MongoCredential 类下选取的认证方式来源于如下类。
public enum AuthenticationMechanism {
GSSAPI("GSSAPI"),
PLAIN("PLAIN"),
MONGODB_X509("MONGODB-X509"),
/** @deprecated */
@Deprecated
MONGODB_CR("MONGODB-CR"),
SCRAM_SHA_1("SCRAM-SHA-1"),
SCRAM_SHA_256("SCRAM-SHA-256"); private static final Map<String, AuthenticationMechanism> AUTH_MAP = new HashMap();
private final String mechanismName; private AuthenticationMechanism(String mechanismName) {
this.mechanismName = mechanismName;
}

2.认证信息有误(用户名密码等配错了)

  如果你手动,改错 数据库名或者用户名密码,你会发现也是会报这个错误。

3.mongdb数据库问题

  用户权限问题。正确的用户创建如下:

use mydb
#没有数据库会自动创建 #在此数据库下创建用户
db.createUser(
{
user: "user",
pwd: "123456",
roles: [ { role: "readWrite", db: "mydb" } ]
}
) #重新进入,开启auth参数,认证通过后才能访问数据库
./mongod -f mongdb.conf --auth db.auth("user","123456") 认证通过后可以操作数据库。

4.springboot yml配置错误

spring:
application:
name: test
data:
mongodb:
host: 你的Ip
username: user
password: 123456
database: user
port: 1234 #如果你是上面这种配置方式,那么恭喜你。他根本不会成功
#正确配置是如下这种方式
uri: mongodb://user:123456@你的Ip:1234/user

 

springboot 和 mongdb连接问题 Exception in thread "main" com.mongodb.MongoSecurityException:的更多相关文章

  1. windows下eclipse远程连接hadoop错误“Exception in thread"main"java.io.IOException: Call to Master.Hadoop/172.20.145.22:9000 failed ”

    在VMware虚拟机下搭建了hadoop集群,ubuntu-12.04,一台master,三台slave.hadoop-0.20.2版本.在 master机器上利用eclipse-3.3连接hadoo ...

  2. 使用sqljdbc连接mssql数据库,maven生成jar运行后报"Exception in thread "main" java.lang.SecurityException"错误

    错误信息如下: Exception in thread "main" java.lang.SecurityException: Invalid signature file dig ...

  3. 在运行Hibernate Hello World程序的时候,抛如下错误: view plain Exception in thread "main" org.hibernate.exception.LockAcquisitionException 解决方法

    在运行Hibernate Hello World程序的时候,抛如下错误: Exception in thread "main" org.hibernate.exception.Lo ...

  4. ReactNative问题随记1 Exception in thread "main" java.lang.RuntimeException: gradle-2.14.1-all.zip

    ReactNative问题随记 想运行在真机上,在运行命令react-native run-android遇到错误如下: Scanning 559 folders for symlinks in D: ...

  5. yml配置文件读取出错 Exception in thread "main" while scanning for the next token found character '\t(TAB)'

    这几天在学习springboot的微服务项目,在配置文件方面也想尝试下新的yml配置,就想把原来项目properties写的文件转换成yml试一下(老项目是之前检出在了eclipse里面),结果写好了 ...

  6. Exception in thread "main" java.lang.UnsupportedClassVersionError : unsupported major.minor version 52.0

    新建好springboot项目后,打开application运行main方法报如下错. 1.问题:Exception in thread "main" java.lang.Unsu ...

  7. Exception in thread "main" java.net.SocketTimeoutException: connect timed ou错误处理

    今天做了一个Jsoup解析网站的项目,使用Jsoup.connect(url).get()连接某网站时偶尔会出现java.net.SocketTimeoutException:Read timed o ...

  8. Exception in thread "main" java.nio.channels.NotYetConnectedException

    import java.nio.channels.AsynchronousServerSocketChannel; import java.nio.channels.AsynchronousSocke ...

  9. Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused (Connection refused)

    一.linux中配置redis,使用java连接测试时报错: Exception in thread "main" redis.clients.jedis.exceptions.J ...

随机推荐

  1. IO 的五种模型是什么

    目录 前言 用户空间和内核空间 IO 五种模型 阻塞型 IO 非阻塞 IO IO 多路复用 信号驱动 IO 异步 IO 总结 阻塞和非阻塞 同步与异步 前言 我们经常看到阻塞/非阻塞,同步/异步这两组 ...

  2. es6删除指定元素

    原数组: let arr =[{id:1},{id:2},{id:3},{id:8}] 待删除数据 obj = {id:1} 删除原数组指定元素 arr.splice(arr.findIndex(it ...

  3. I/O方式(本章最重要)

    目录 程序查询方式 程序查询方式接口结构 例题 本节回顾 程序中断方式 中断的基本概念 工作流程 中断请求 分类 中断请求标记 中断响应 判优实现 优先级设置 中断处理过程 中断隐指令 硬件向量法 中 ...

  4. 双端口RAM和多模块存储器

    目录 双端口RAM 存取周期 双端口RAM 多模块存储器 普通存储器 单体多字存储器 多体并行的存储器 高位交叉编址的多体存储器 低位交叉编址的多提存储器 为什么要这么弄? 高位 低位 流水线(考试常 ...

  5. 【ubantu下安装python3.6】

    Ubuntu16.04默认安装了Python2.7和3.5 请注意,系统自带的python千万不能卸载! 输入命令python

  6. Samba:error nt_status_host_unreachable

    安装Samba之后: smbclient -L server0.example.com 出现报错:error nt_status_host_unreachable     解决方法:server端把S ...

  7. 一步步分析:C语言如何面向对象编程

    这是道哥的第009篇原创 一.前言 在嵌入式开发中,C/C++语言是使用最普及的,在C++11版本之前,它们的语法是比较相似的,只不过C++提供了面向对象的编程方式. 虽然C++语言是从C语言发展而来 ...

  8. CGLIB(Code Generation Library)详解

    什么是CGLIB CGLIB是一个强大的.高性能的代码生成库.其被广泛应用于AOP框架(Spring.dynaop)中,用以提供方法拦截操作.Hibernate作为一个比较受欢迎的ORM框架,同样使用 ...

  9. jdbc编程学习(1)

    一,背景 1996年,sun公司发布了第一版本的数据库连接(JDBC)API,使编程人员可以通过这个API接口连接到数据库,并使用结构化查询语言(SQL)完成对数据库的查找与更新.JDBC自此成为Ja ...

  10. JS验证三种提示框

    1 <form> 2 <!-- 按钮选择点击 --> 3 <tr> 4 <td><input type="button" na ...