加固mongodb建议:修改数据库默认端口,添加数据库访问权限:mongodb
启动数据库的注意事项: shell
登陆数据库:(name:root;pwd:root)数据库
本地登陆:服务器
远程登陆: app
一些命令:socket
建立一个用户名和密码为root的管理员(建立在当前db下)mongoose
roles角色,指定角色后就有相应权限,通常在admin里定义角色在其余地方用ui
简版:this
建立完后登录spa
1.直接命令登陆;
2.mongo后缀加用户名密码登陆;
3.mongo远程登陆;
4.robomongo可视化管理软件登陆。
修改用户密码
查看用户信息
修改密码和用户信息
给数据库添加访问权限:(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