MongoDB的安装配置

参考:

mac上

安装MongoDB

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

brew tap mongodb/brew
brew install mongodb-community@4.0
brew services start mongodb-community@4.0
# brew services list
# launchctl list | grep mongo
# launchctl stop homebrew.mxcl.mongodb-community # 停止

Bobo Studio 3T MongoDB管理工具
https://robomongo.org/

MongoDB配置
cat /usr/local/etc/mongod.conf

重启MongoDB
brew services restart mongodb-community

命令行连接
mongo --port 27017 -u "luowei" -p "xxxxxx" --authenticationDatabase "admin"


ubuntu 14.04 上

安装MongoDB

  1. 导入公钥
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

  2. 为mongodb创建source list
    echo "deb [ arch=amd64 ] http://repo.mongodb.com/apt/ubuntu trusty/mongodb-enterprise/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

  3. 更新本地源
    sudo apt-get update

  4. 安装mongodb-enterprise
    sudo apt-get install -y mongodb-enterprise

  5. 固定mongodb版本以防意外升级
    echo "mongodb-enterprise hold" | sudo dpkg --set-selections
    echo "mongodb-enterprise-server hold" | sudo dpkg --set-selections
    echo "mongodb-enterprise-shell hold" | sudo dpkg --set-selections
    echo "mongodb-enterprise-mongos hold" | sudo dpkg --set-selections
    echo "mongodb-enterprise-tools hold" | sudo dpkg --set-selections
    
  6. 其他
    默认日志与数据目录
    its data files in /var/lib/mongodb
    its log files in /var/log/mongodb
    

默认用户

By default, MongoDB runs using the mongodb user account

启动/重启

sudo service mongod start
sudo service mongod stop
sudo service mongod restart

安装admin-mongo

zip 安装方案

  1. Download Zip file from: https://github.com/mrvautin/adminMongo/archive/master.zip
  2. Unzip to chosen directory
  3. Enter directory: cd adminMongo
  4. Install dependencies: npm install
  5. Start application: npm start
  6. Visit http://127.0.0.1:1234 in your browser

启动adminMongo

cd /var/www/mongo.wodedata.com 
npm start
npm restart

让adminMongodb后台运行

cd  /var/mongo.wodedata.com
nohup npm start & 

其他启动方式
npm start --prefix /var/www/mongo.wodedata.com


实战案例配置

查看mongodb配置文件:
sudo cat /etc/mongod.conf

dbPath: /var/lib/mongodb
logPath: /var/log/mongodb/mongod.log

添加的用户
命令行执行: mongo

使用admin数据库 use admin

不受访问限制的超级用户root

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

admin超级管理员用户

db.createUser(
  {
    user: "luowei",
    pwd: "xxxxxx",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

业务数据库管理员用户

db.createUser({
    user:"mongo",
    pwd:"mg.wodedata_com",
    customData:{
        name:"mongo",
        email:"luowei@wodedata.com",
        age:28,
    },
    roles:[
        {role:"read",db:"admin"}
    ]
})

开启权限控制

  1. 采用命令行启动
    /usr/bin/mongod --config /etc/mongod.conf --auth

  2. 在/etc/mongod.conf 中添加

    security:
      #开启访问权限控制
      authorization: enabled
    

版权所有,转载请注明出处 luowei.github.io.