mongoDB的简单配置

数据库配置

  1. 安装mongoDB,不在赘述,不明白的goole

  2. 创建数据库日志文件夹:
    在终端输入: sudo mkdir -p /data/db

  3. 给予数据库日志文件夹操作权限
    在终端输入: sudo chown -R 用户名 /data/db

  4. 进入mongodb 的 “bin”目录,使用命令“./mongod”启动mongoDB server,启动成功后最后一行应该是端口号,如配图,出现配图就能肯定你的Mongodb已经安装成了

    《mongoDB的简单配置》 m1.jpg

tips: 查找bin路径的时候,有时候会忘记具体路径,因为我是用brew安装的,所以我用
brew info mongodb 查找安装信息的时候能找到其配置文件的路径,进而找到bin路径为/usr/local/bin

tip2017nov22:这里最好在.bash_profile中配置一下,具体可以看我的博客

  1. 新建终端标签,并输入./mongo 登陆到数据库

    《mongoDB的简单配置》 m2.jpg

  2. 下面你可以任意操纵这个数据库了是不是很简单啊,如配图

    《mongoDB的简单配置》 m3.png

关于用户的一些设置

  1. Start MongoDB without access control. (No need, if service already running)
$ mongod --port 27017 --dbpath /data/db1
  1. Connect to the instance
$ mongo --port 27017
  1. Create the user administrator.
    Add a user with the root role. For example, the following creates the user superAdmin on the admin database:
$ use admin
$ db.createUser(
  {
    user: "superAdmin",
    pwd: "admin123",
    roles: [ { role: "root", db: "admin" } ]
  })
  1. Re-start the MongoDB instance with access control
    Add the security.authorization setting to the config file
    ubuntu: $ sudo vi /etc/mongod.conf
    osx with brew version: $ sudo vi /usr/local/etc/mongod.conf
    It may look like this
systemLog:
 destination: file
 path: /usr/local/var/log/mongodb/mongo.log
 logAppend: true
storage:
 dbPath: /usr/local/var/mongodb
net:
 bindIp: 127.0.0.1
security:
 authorization: enabled

Restart mongodb

ubuntu: $ sudo service mongod restart

osx with brew version: $ brew services restart mongodb

  1. Connect to database instance with superAdmin access
$ mongo --port 27017 -u "superAdmin" -p "admin123" --
authenticationDatabase "admin"
  1. Create user access (readWrite) for specific database
$ mongo --port 27017 -u "superAdmin" -p "admin123" --authenticationDatabase "admin"
$ use myAppDb
$ db.createUser(
  {
   user: "myAppDbUser",
   pwd: "myApp123",
   roles: [ "readWrite"]
  })
  1. Try Connecting to the specific database with limited access
$ mongo --port 27017 -u "myAppDbUser" -p "myApp123" --authenticationDatabase "myAppDb"

More user roles here Build in Roles
Source Mongodb Enable Client Access Control
Your comments and suggestions are welcome :)

文章参考自
百度经验
国外博客

    原文作者:小白猿
    原文地址: https://www.jianshu.com/p/f79b759988d3
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞