MongoDB的安装配置
参考:
- mongodb用户权限管理最全攻略:用户的创建、查看、删除与修改,mongodb入坑之旅:https://segmentfault.com/a/1190000015603831
- mongodb用户权限管理最全攻略:https://segmentfault.com/a/1190000015603831
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
-
导入公钥
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
-
为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
-
更新本地源
sudo apt-get update
-
安装mongodb-enterprise
sudo apt-get install -y mongodb-enterprise
- 固定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
- 其他
默认日志与数据目录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 安装方案
- Download Zip file from: https://github.com/mrvautin/adminMongo/archive/master.zip
- Unzip to chosen directory
- Enter directory: cd adminMongo
- Install dependencies: npm install
- Start application: npm start
- 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"}
]
})
开启权限控制
-
采用命令行启动
/usr/bin/mongod --config /etc/mongod.conf --auth
-
在/etc/mongod.conf 中添加
security: #开启访问权限控制 authorization: enabled