设为首页 收藏本站
查看: 1051|回复: 0

[经验分享] mongodb数据库添加权限及简单数据库命令操作笔记

[复制链接]

尚未签到

发表于 2017-12-16 06:49:02 | 显示全部楼层 |阅读模式
加固mongodb建议:修改数据库默认端口,添加数据库访问权限:


  • 启动数据库(裸奔):C:\mongodb\bin>mongod --dbpath C:\MongoDB\data(同时用--dbpath指定数据存放地点为“db”文件夹。)
  • 数据库管理:mongo.exe
  • 新版的MongoDB已经不支持addUser方法了,改成createUser了。
启动数据库的注意事项:   


  • 指定端口启动数据库(不需要认证):E:\mongodb\bin>mongod --dbpath E:\MongoDB\data --port=27017
  • 指定端口启动数据库(需要认证):E:\mongodb\bin>mongod --auth --dbpath E:\MongoDB\data  --port=27017
  登录数据库:(name:root;pwd:root)
本地登录:


  • 指定端口登录数据库:C:\mongodb\bin>mongo --port=27017
  • 用户名密码登录:C:\mongodb\bin>mongo -u root -p root --port=27017
  • 登陆到db1数据库:C:\mongodb\bin>mongo db1 -u root -p root --port=27017
远程登录:


  • 连接远程数据库:E:\mongodb\bin>mongo ip:27017/db -u root -p root
一些命令:


  • show dbs (或者使用show databases  查看当前数据库情况,默认在test下)
  • use test  如果test不存在则创建test数据库,show dbs查看不到,需要插入数据
  • db.test.insert({"aa":"11"})
  • db 查看当前连接在哪个数据库(db.test2.insert()会在当前数据库下创建test2数据表并插入数据)
  • db.test.find() 查看当前数据库的test数据表数据
创建一个用户名和密码为root的管理员(创建在当前db下)

roles角色,指定角色后就有相应权限,一般在admin里定义角色在其他地方用


  • db.createUser({ user: "root",pwd: "root",customData:{name:"root"},roles:[{ role: "userAdminAnyDatabase",db: "admin" }]})
  • db.createUser({ user: "root4",pwd: "root",customData:{name:"root"},roles:[]})
简版:


  • db.createUser({ user: "root5",pwd: "root",customData:{},roles:[]})
创建完后登陆

1.直接命令登录;

2.mongo后缀加用户名密码登录;

3.mongo远程登录;

4.robomongo可视化管理软件登录。


  • db.auth('root','root')
修改用户密码


  • use admin
  • db.changeUserPassword("username", "xxx")
查看用户信息


  • db.runCommand({usersInfo:"userName"})
修改密码和用户信息


  • db.runCommand( { updateUser:"username", pwd:"xxx", customData:{title:"xxx"} })
给数据库添加访问权限:(auth)

解决步骤:

1)不带--auth参数启动数据库,所以不需要帐号即可连上MongoDB。

2)新建一个角色,比如叫 sysadmin,需要先切换到admin库进行如下操作:

> use admin

switched to db admin

> db.createRole({role:'syadmin',roles:[],

privileges:[

{resource:{anyResource:true},actions:['anyAction']}

]})

3)然后,新建一个用户,使用这个角色,注意,这个角色的db是admin,操作如下:

> use woplus

switched to db woplus

> db.createUser({

user:'root',

pwd: 'root',

roles:[

{role:'syadmin',db:'admin'}

]})

好了现在重启启动数据库带上  --auth 就可以正常执行了

Node服务器端配置:

var mongoose = require('mongoose')

var opts = {  server: {  socketOptions: { keepAlive: 1 }    }   }

//  连接地址

mongoose.connect('mongodb://uname:pwd@ip:27017/db', opts);

var db = mongoose.connection;

//在控制台上输出

db.on('error',()=>{  console.error('连接数据库错误')})

db.once('open', () => {  console.log('连接成功!')})

db.help()

DB methods:

1)      db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]

2)      db.auth(username, password)

3)      db.cloneDatabase(fromhost)

4)      db.commandHelp(name) returns the help for the command

5)      db.copyDatabase(fromdb, todb, fromhost)


6)      db.createCollection(name, {>
7)      db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } )

8)      db.createUser(userDocument)

9)      db.currentOp() displays currently executing operations in the db

10)  db.dropDatabase()

11)  db.eval() - deprecated

12)  db.fsyncLock() flush data to disk and lock server for backups

13)  db.fsyncUnlock() unlocks server following a db.fsyncLock()

14)  db.getCollection(cname) same as db['cname'] or db.cname

15)  db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections

16)  db.getCollectionNames()

17)  db.getLastError() - just returns the err msg string

18)  db.getLastErrorObj() - return full status object

19)  db.getLogComponents()

20)  db.getMongo() get the server connection object

21)  db.getMongo().setSlaveOk() allow queries on a replication slave server

22)  db.getName()

23)  db.getPrevError()

24)  db.getProfilingLevel() - deprecated

25)  db.getProfilingStatus() - returns if profiling is on and slow threshold

26)  db.getReplicationInfo()

27)  db.getSiblingDB(name) get the db at the same server as this one

28)  db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set

29)  db.hostInfo() get details about the server's host

30)  db.isMaster() check replica primary status

31)  db.killOp(opid) kills the current operation in the db

32)  db.listCommands() lists all the db commands

33)  db.loadServerScripts() loads all the scripts in db.system.js

34)  db.logout()

35)  db.printCollectionStats()

36)  db.printReplicationInfo()

37)  db.printShardingStatus()

38)  db.printSlaveReplicationInfo()

39)  db.dropUser(username)

40)  db.repairDatabase()

41)  db.resetError()

42)  db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }

43)  db.serverStatus()

44)  db.setLogLevel(level,<component>)

45)  db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all

46)  db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db

47)  db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db

48)  db.setVerboseShell(flag) display extra information in shell output

49)  db.shutdownServer()

50)  db.stats()

51)  db.version() current version of the server



春雷原创转载请注明:http://www.cnblogs.com/chunlei36

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-424572-1-1.html 上篇帖子: 项目实战(连载):基于Angular2+Mongodb+Node技术实现的多用户博客系统教程(3) 下篇帖子: MongoDB中的数据类型
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表