springcloudl新增模块moudle连接mongoDB数据库报错

连接报错,身份认证失败

Caused by: com.mongodb.MongoCommandException: Command failed with error 18: ‘Authentication failed.’ on server XXXXXXX:27017. The full response is { “ok” : 0.0, “errmsg” : “Authentication failed.”, “code” : 18, “codeName” : “AuthenticationFailed” }

1、可能用户名密码错误或者没加引号等

2、MongoDB中每个数据库之间是相互独立的,都有独立的权限,正确的做法是使用root账号在【将要操作的数据库】中创建一个【子账号】,在用这个子账号连接mongo

-- 使用admin数据库
use admin


-- 查看数据库
show dbs

-- 使用position数据库
use position

-- 创建用户,赋予权限

db.createUser({user:"position",pwd:"123456",roles:[{role:"dbOwner",db:"position"}]})

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐