mongodb數據庫添加權限及簡單數據庫命令操做筆記

加固mongodb建議:修改數據庫默認端口,添加數據庫訪問權限:mongodb

  • 啓動數據庫(裸奔):C:\mongodb\bin>mongod --dbpath C:\MongoDB\data(同時用--dbpath指定數據存放地點爲「db」文件夾。)
  • 數據庫管理:mongo.exe
  • 新版的MongoDB已經不支持addUser方法了,改爲createUser了。

啓動數據庫的注意事項:   shell

  • 指定端口啓動數據庫(不須要認證):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

遠程登陸: app

  • 鏈接遠程數據庫:E:\mongodb\bin>mongo ip:27017/db -u root -p root

一些命令:socket

  •  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下)mongoose

roles角色,指定角色後就有相應權限,通常在admin裏定義角色在其餘地方用ui

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

簡版:this

  • db.createUser({ user: "root5",pwd: "root",customData:{},roles:[]})

建立完後登錄spa

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

相關文章
相關標籤/搜索