MongoD单节点环境安装(Linux)
安装包
下载地址: (https://www.mongodb.com/download-center)
用户权限/目录
1.创建 dbuser用户
useradd mongod
2.部署目录
mkdir -p /opt/local
chown -R mongod.root /opt/local
cd /opt/local
3.解压安装包
tar -xzvf mongodb-linux-x86_64-enterprise-SUSE11-3.2.7.tgz
mv mongodb-linux-x86_64-enterprise-suse11-3.2.7 mongodb
cd mongodb
mkdir conf data log
conf 作为配置文件目录 data 作为数据文件目录 log 作为日志文件目录
配置文件
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /mongo/data/mongod.log
# Where and how to store data.
storage:
dbPath: /mongo/data
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /mongo/mongod.pid # location of pidfile
# network interfaces
net:
port: 27017
bindIp: 172.16.6.65 # Listen to local interface only, comment to listen on all interfaces.
#security:
#operationProfiling:
replication:
replSetName: rs0
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
其中启动端口为27017,将http管理界面关闭; slowOpThredsholdMs是慢操作检测的阈值,可以根据需要调整;
环境变量 vi /etc /profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
初始化
1.启动mongod,缺省状态下可匿名访问
mongod -f conf/mongodb.conf
2.连接 mongo
mongo --port 27017
3.初始化管理员
use admin
db.createUser({user:'admin',pwd:'admin@2016',roles:[{role:'clusterAdmin',db:'admin'},{role:'userAdminAnyDatabase',db:'admin'}]})
use appdb
db.createUser({user:'appuser',pwd:'appuser@2016',roles:[{role:'dbOwner',db:'appdb'}]})
分别创建了管理员用户和应用账户,之后将
4.重启 mongo,启用鉴权
pkill mongod
mongod -f conf/mongodb.conf -auth
此后连接mongo 则需要提供身份鉴权
常用命令
启动命令
mongod -f conf/mongodb.conf
停止命令
kill -2 `cat mongod.pid`
rm mongod.pid
检测端口
netstat -nlp |grep 27071
查看日志
tail -n200 -f log/mongodb.log
连接端口
./bin/mongo --port 10001 -u admin -p admin@2016 --authenticationDatabase=admin
监控命令
./bin/mongostat --port 27071 -u admin -p admin@2016 --authenticationDatabase=admin --discover -n 30 3