MongoDB
大约 2 分钟约 590 字
MongoDB
下载MongoDB
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-5.0.1.tgz
解压安装
tar xf mongodb-linux-x86_64-rhel70-5.0.1.tgz -C /usr/local
cd /usr/local
mv mongodb-linux-x86_64-rhel70-5.0.1 mongodb
cd mongodb
mkdir data log conf
配置文件
vi mongod.conf
# 内容
# 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: /usr/local/mongodb/log/mongod.log
# Where and how to store data.
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
# engine:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
### 添加授权
security:
authorization: enabled
启动mongod服务器
cd /usr/local/mongodb/bin
# 基于配置参数启动mongodb
./mongod --dbpath ../data/ --logpath ../log/mongod.log --bind_ip 0.0.0.0 --fork --auth
# 基于配置文件启动
cd /usr/local/mongodb/bin
./mongod -f /usr/local/mongodb/mongod.conf
[root@node1 ~]# ps -ef | grep mongod
mongod 1574 1 4 20:39 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf
root 1610 1399 0 20:40 pts/0 00:00:00 grep --color=auto mongod
启动报错
about to fork child process, waiting until server is ready for connections.
forked process: 3988
ERROR: child process failed, exited with 1
To see additional information in this output, start without the "--fork" option.
进入/usr/local/mongodb/data删除mongod.lock
开放端口,并重启防火墙端口
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload
添加PATH
#对于交互式会话,请使用以下命令在 ~/.bash_profile 文件中修改 PATH 环境变量:
echo 'export PATH="$PATH:/usr/local/mongodb/bin"' >> ~/.bash_profile
#对于非交互式会话,请使用以下命令在 ~/.bashrc 文件中修改 PATH 环境变量:
echo 'export PATH="$PATH:/usr/local/mongodb/bin"' >> ~/.bashrc
source ~/.bashrc
关闭mongodb
mongod --shutdown --dbpath=/usr/local/mongodb/data
启动mongodb
mongod -f /usr/local/mongodb/mongod.conf
注意
一定要正常关闭,切记不要使用kill的形式,容易损坏
给需要的数据库授权
mongo
use sunnyfandb
db.createUser({user:"sunnyfan",pwd:"sunnyfan",roles:[{role:"dbOwner",db:"sunnyfandb"}]})
重启验证授权是否正常
shell验证
mongo
use sunnyfandb
db.auth("sunnyfan","sunnyfan")
开机自动启动mongodb
cd /etc/rc.d/init.d
vi mongod
# 内容
#!/bin/bash
#
#chkconfig: 2345 80 90
#description: mongod
start() {
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb//mongod.conf
}
stop() {
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb//mongod.conf --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
# 保存完成之后,添加脚本执行权限
chmod +x /etc/rc.d/init.d/mongod
chkconfig --add mongod
chkconfig --level 345 mongod on
chkconfig --list mongod
# MongoDB服务的启动与关闭
service mongod start
service mongod stop