SHELL脚本执行mongodb 添加用户权限

fit_mongo.sh和fit_mongo.sh在同一级目录下:

MongoDB添加用户:

1、设置权限:mongodb.conf中添加

auth =true

2、启动mongoDB:

cd mongo/mongodb-linux-x86_64-rhel62-4.2.3/bin/
./mongod -f ../../mongodb.conf

3、执行脚本

fit_mongo.sh

#!/bin/bash
cd mongo/mongodb-linux-x86_64-rhel62-4.2.3/bin/
./mongo --host 127.0.0.1:27017 data ../../../fit_mongo.js >> fit_mongo.log

fit_mongo.js

try{
var database = connect('admin')
database.createUser({user:'root',pwd:'root',roles:['root']});
print('Ready to addRoot');
}catch(err){
print('error message:'+err);
}

 

现在我们为mongodb的admin数据库添加一个用户root,密码也是root,mongodb可以为每个数据库都建立权限认证,也就是你可以指定某个用户可以登录到哪个数据库。上面的代码,我们为admin数据库添加了一个root用户,在mongodb中admin数据库是一个特别的数据库,这个数据库的用户,可以访问mongodb中的所有数据库。

 

 

 

 

springboot:连接mongo

 

 

mongodb.port=27017
mongodb.host=127.0.0.1
mongodb.dbName=admin
mongodb.userName=root
mongodb.password=123456
org.quartz.jobStore.class=cn.com.rivercloud.report.scheduler.CustomMongoQuartzSchedulerJobStore
org.quartz.jobStore.mongoUri=mongodb://root:123456@127.0.0.1:27017/admin
org.quartz.jobStore.collectionPrefix=quartz
org.quartz.jobStore.isClustered=false
org.quartz.scheduler.skipUpdateCheck=true
org.quartz.scheduler.instanceId=AUTO
org.quartz.scheduler.instanceName=quartzMongoInstance
org.quartz.quartz.jobStore.dbName=data
org.quartz.server.name=quarts-intro
org.quartz.threadPool.threadCount=1

 

原文地址:https://www.cnblogs.com/notchangeworld/p/13479682.html