查看系统版本
cat /etc/issue
下载合适的安装包
https://www.mongodb.com/download-center/community?jmp=nav
服务器配置
下载
cd /usr/local/src
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-4.0.5.tgz
tar -zxvf mongodb-linux-x86_64-ubuntu1404-4.0.5.tgz
cp -R mongodb-linux-x86_64-ubuntu1404-4.0.5 /usr/local/mongodb
配置
cd /usr/local/mongodb/bin
vim mongo.conf
#添加
dbpath=/usr/local/mongodb/db #数据库文件
logpath=/usr/local/mongodb/logs/mongodb.log #日志
fork = true #以守护程序的方式启用,即在后台运行
#auth=true #需要认证。如果放开注释,就必须创建MongoDB的账号,使用账号与密码才可远程访问,第一次安装建议注释
bind_ip=0.0.0.0 #允许远程访问,或者直接注释,127.0.0.1是只允许本地访问
测试
./mongod -f mongo.conf
mongo
添加到服务
vim /etc/init.d/mongod
#!/bin/sh
. /lib/lsb/init-functions
CONFIG="/usr/local/mongodb/mongodb.conf"
PROGRAM="/usr/local/mongodb/bin/mongod"
MONGOPID=`ps -ef | grep 'mongod' | grep -v grep | awk '{print $2}'`
test -x $PROGRAM || exit 0
case "$1" in
start)
ulimit -n 3000
echo "Starting MongoDB Server"
$PROGRAM -f $CONFIG &
;;
stop)
ulimit -n 3000
echo "Stopping MongoDB Server"
if [ ! -z "$MONGOPID" ]; then
kill -15 $MONGOPID
fi
;;
*)
log_success_msg "Usage:/etc/init.d/mongod {start|stop}"
exit 1
esac
exit 0
service mongod start #启动
service mongod sttop #关闭
开机启动
sudo sysv-rc-conf mongod on(off)
后续
use admin
db.createUser({user:"admin",pwd:"123456",roles:["root"]}); #创建用户
db.grantRolesToUser("admin", [ { role: "read", db: "dbname" } ]);
db.auth('admin,'~123456');