docker run --name mongodb -p 27017:27017 -v /opt/mongodb/data:/data/db -v /opt/mongodb/backup:/data/backup -d mongo --auth

进入容器:

docker -it exec 容器id /bin/bash

进入mongo的控制台

mongosh

设置用户名及密码

use admin

db.createUser({

user: "admin",

pwd: "666",

roles: [ { role: "root", db: "admin" } ],

mechanisms : ["SCRAM-SHA-1"]

})

说明:

#db.createUser({

# user: 'admin', // 用户名

# pwd: 'root123', // 密码

# roles:[{

# role: 'root', // 角色---超级管理员才可以使用该角色

# db: 'yygh_hosp' // 数据库

# }],

# mechanisms : ["SCRAM-SHA-1"]//加密类型

#})

查看密码是否设置成功: use admin show dbs 说明密码设置成功 授权登录

use admin

db.auth("admin","666")

输入show dbs 查询出数据及登录成功

设置mongodb的连接方式

#更新源

apt-get update

#安装 vim

apt-get install vim

#修改 mongo 配置文件

vim /etc/mongod.conf.orig

bindIp: 127.0.0.1注释掉 权限打开 security: authorization: enabled

springboot整合mongodb时报错

Command failed with error 13 (Unauthorized): ‘not authorized on xxx to execute command

仍使用uri的方式配置,格式如下:uri: mongodb://用户名:密码@106.12.111.157:27017/datasource名称?authSource=admin&authMechanism=SCRAM-SHA-1

uri: mongodb://username:password@106.12.111.157:27017/IntelligentGuidance?authSource=xxx&authMechanism=SCRAM-SHA-1

另外也可以使用属性配置的方式,要注意每个属性都需要配置(建议使用上一种)

spring:

data:

mongodb:

database: test

database: IntelligentGuidance

host: 106.12.111.157

port: 27017

username: root

password: 111111

database: IntelligentGuidance

推荐文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: