加固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, { 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

为mongodb数据库增加用户名密码权限的更多相关文章

  1. mongo安全:增加用户名密码

    0.简述:在非auth下创建账户,然后重启 1.以不需要用户名密码的方式启动mongodb 2.运行客户端mongo,输入以下指令 show dbs;use admin;db.createRole({ ...

  2. 你的MongoDB Redis设置用户名密码了吗?看看shodan这款邪恶的搜索引擎吧!~

    早上看新闻的时候看到了个醒目的新闻 开源中国:MongoDB 赎金事件持续发酵,究竟是谁之过? 博客园:MongoDB数据库勒索,中国受害者数量超乎你的想象,SOS! 1. 由于自己之前做过的项目,R ...

  3. springMVC web项目 对访问数据库的用户名密码进行加密解密

    在使用springMVC开发web项目中,数据库的用户名,密码一般都是配置在.properties文件中 然后在通过.xml配置文件引入.properties的变量,例如 在config.proper ...

  4. MongoDB启动及用户名密码设置

    1.服务启动 下载后的安装步骤,请参见mongoDB安装详细教程 启动服务NET START MongoDB 关闭服务NET STOP MongoDB 启动客户端mongo MongoDB shell ...

  5. Oracle数据库忘记用户名密码的解决方案

    1.windows+r输入sqlplus 2.依次输入: sys/manager as sysdba #创建新用户 SQL> create user c##username(自己的用户名) id ...

  6. elasticsearch中head插件中的定制增加用户名密码范例

    在head插件目录下一般 在 elasticsearch目录下的 plugins\head目录 下 在 或 plugins\head\site目录下 有 一个index.html文件.把这个文件用下面 ...

  7. Java连接MySQL数据库实现用户名密码的验证方法 Java语句中sql查询语句'' ""作用

    //方法一,可以验证登录,但方法不实用.package com.swift; import java.sql.Connection; import java.sql.DriverManager; im ...

  8. TypeError: db.addUser is not a function : @(shell):1:1 ——mongoDB创建新用户名密码的方法

    不多说,旧版本使用 db.addUser("root","root") 新版本使用这句会出现这个错误提示 TypeError: db.addUser is no ...

  9. mongodb数据库禁止外网访问以及添加账号

    未曾料到被黑客勒索比特币的戏码竟然降临到我的身上,几个月的技术积累付之一炬.怪只怪自己学艺不精,心存侥幸和无知,不过经此一役,方知网络安全防护的重要性. 一直未给自己的mongodb数据库设置账号密码 ...

随机推荐

  1. bzoj千题计划171:bzoj2456: mode

    http://www.lydsy.com/JudgeOnline/problem.php?id=2456 任意删除序列中两个不同的数,众数仍然是众数 不停的删,剩下的最后的数一定是众数 具体实现: 记 ...

  2. ZeroMQ API(八) 异常&属性

    1.错误处理 1.1 zmq_errno() 1.1.1 名称 zmq_errno - 为调用线程检索errno的值 1.1.2 概要 int zmq_errno(void); 1.1.3 描述 zm ...

  3. github pages 正确访问方式

    ❌ http://username.github.io ✅ http://username.github.io. github pages 绑定域名后,由于浏览器dns缓存,需要重启一下

  4. 【CodeForces】600 E. Lomsat gelral (dsu on tree)

    [题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σc ...

  5. 33、Map简介

    Map接口概述 除了Collection之外,常用的集合还有Map接口,里面常用的实现类图如下: map中的元素是以键-值的方式存在的,通过键可以获取到值,键是不可以重复的,跟地图比较像,通过一个坐标 ...

  6. UNIX网络编程 第1章:简介和TCP/IP

    1.1 按1.9节未尾的步骤找出你自己的网络拓扑的信息. 1.2 获取本书示例的源代码(见前言),编译并测试图1-5所示的TCP时间获取客户程序.运行这个程序若干次,每次以不同IP地址作为命令行参数. ...

  7. 工具推荐:Backdoor-apk,安卓APK文件后门测试工具

    工具推荐:Backdoor-apk,安卓APK文件后门测试工具 Backdoor-apk可以看成是一个shell脚本程序,它简化了在Android APK文件中添加后门的过程.安全研究人员在使用该工具 ...

  8. Python标准库笔记(10) — itertools模块

    itertools 用于更高效地创建迭代器的函数工具. itertools 提供的功能受Clojure,Haskell,APL和SML等函数式编程语言的类似功能的启发.它们的目的是快速有效地使用内存, ...

  9. NTC热敏电阻认识及功率型热敏电阻选型

    -------------------记公司一款已经量产的产品,在最新加工的批次上出现:刚上电就炸毁功率型热敏电阻的问题------------------------- ************写在 ...

  10. ARM Linux 3.x的设备树(Device Tree)【转】

    转自:http://blog.csdn.net/21cnbao/article/details/8457546 宋宝华 Barry Song <21cnbao@gmail.com> 1.  ...