mongodb数据库添加权限及简单数据库命令操作笔记
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodb数据库添加权限及简单数据库命令操作笔记,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5370字,纯文字阅读大概需要8分钟。
内容图文
![mongodb数据库添加权限及简单数据库命令操作笔记](/upload/InfoBanner/zyjiaocheng/476/8827c9cfbec84a21a80d350cc4a4b23e.jpg)
登录数据库:(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, { size : ..., capped : ..., max : ... } )
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
mongodb数据库添加权限及简单数据库命令操作笔记
标签:output man cal down any gdb 角色 pat 管理软件
本文系统来源:http://www.cnblogs.com/chunlei36/p/6506048.html
内容总结
以上是互联网集市为您收集整理的mongodb数据库添加权限及简单数据库命令操作笔记全部内容,希望文章能够帮你解决mongodb数据库添加权限及简单数据库命令操作笔记所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。