mongoDB-3.x安装配置(单机版)
官方文档:
环境:
CentOS6.5 x64
mongoDB-3.2.0
方式一 : yum安装
请参看yum安装mongoDB
方式二
: 二进制包
1.安装
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.0.tgz --no-check-certificate
tar -xvf mongodb-linux-x86_64-rhel62-3.2.0.tgz -C /opt/
ln -s /opt/mongodb-linux-x86_64-rhel62-3.2.0/
/opt/mongodb
cat >>/etc/profile <<HERE
PATH=$PATH:/opt/mongodb/bin
HERE
source /etc/profile
mkdir -p /opt/mongodb/{log,db,conf}
2.启动
mongod --fork --httpinterface --rest --jsonp --setParameter
enableLocalhostAuthBypass=0 --pidfilepath /opt/mongodb/mongod.pid
--dbpath /opt/mongodb/db --logpath /opt/mongodb/log/mongod.log
--logappend --logRotate
rename --timeStampFormat ctime
可以加入/etc/rc.d/rc.local,以随操作系统重启时自启动
部分参数解释
--fork #后台daemon运行
--bind_ip #监听IP地址列表,以逗号分隔
--port #监听端口,默认27017
--setParameter
enableLocalhostAuthBypass=0 #所有接口都需要认证
--pidfilepath
#pid文件
--dbpath #db存放路径
--logpath #日志文件
--config #配置文件
--auth #启用认证
--httpinterface #启用web接口
--rest #rest api
--jsonp #json api
如果闲命令参数太多,可以指定配置文件
WARNING
Ensure that the HTTP status interface, the REST API, and the JSON API are all disabled in production environments to prevent potential data exposure and vulnerability to attackers
root@master:~#netstat -tunlp|grep mongod
tcp
0
0
0.0.0.0:28017
0.0.0.0:*
LISTEN
11896/mongod
tcp
0
0
0.0.0.0:27017
0.0.0.0:*
LISTEN
11896/mongod
默认监听端口,db端口为27017, web端口为28017
3.关闭
mongod --shutdown --dbpath
/opt/mongodb/db
还可以
命令行模式执行shutdown
use admin
db.shutdownServer()
或
mongo admin --port 27017 --eval "db.shutdownServer()"
4.指定配置文件
Changed in version 2.6: MongoDB
2.6 introduces a YAML-based configuration file format.
The 2.4
configuration file format remains
for backward compatibility.
默认的二进制包没有配置文件模板,可以从源码包内提取
cp /usr/local/src/mongodb-src-r3.2.0/rpm/mongod.conf
/opt/mongodb/conf
以下是一个简单的对应以上命令行参数的配置文件
# mongod.conf
# for documentation of all
options, see:
#
http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging
data.
systemLog:
# Where and how to store
data.
storage:
#
engine:
#
mmapv1:
#
wiredTiger:
# how the process
runs
processManagement:
# network
interfaces
net:
setParameter:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only
Options
#auditLog:
#snmp:
更多参数和解释请参看官方文档
5.init管控脚本
useradd -s /sbin/nologin -r mongod
chown -R mongod: /opt/mongodb-linux-x86_64-rhel62-3.2.0
cp /usr/local/src/mongodb-src-r3.2.0/rpm/init.d-mongod
/etc/init.d/mongod
chmod +x /etc/init.d/mongod
sed -i '/CONFIGFILE=/i MONGOD="/opt/mongodb/bin/mongod"'
/etc/init.d/mongod
sed -i
'/CONFIGFILE=/s:/etc/mongod.conf:/opt/mongodb/conf/mongod.conf:g'
/etc/init.d/mongod

或systemd
cat >/lib/system/systemd/mongod.service <<HERE
[Unit]
Description=mongoDB
After=network-online.target
[Service]
Type=forking
User=mongod
Group=mongod
LimitNOFILE=65536
ExecStart=/opt/mongodb/bin/mongod -f /opt/mongodb/conf/mongod.conf
KillMode=control-group
Restart=on-failure
[Install]
WantedBy=multi-user.target
HERE
systemctl daemon-reload
6.修改内核参数
cat >>/etc/rc.d/rc.local <<HERE
echo never >
/sys/kernel/mm/transparent_hugepage/enabled
echo never >
/sys/kernel/mm/transparent_hugepage/defrag
HERE
或者加入/etc/rc.sysinit末尾
7.优化系统资源限制
Recommended ulimit Settings¶
Every deployment may have unique requirements and settings;
however, the following thresholds and settings are particularly
important for
-
-f
(file size): unlimited -
-t
(cpu time): unlimited -
-v
(virtual memory): unlimited [1] -
-n
(open files): 64000 -
-m
(memory size): unlimited [1] [2] -
-u
(processes/threads): 64000
Always remember to restart your
cat >/etc/security/limits.d/99-mongodb-nproc.conf
<<HERE
mongod soft fsize unlimited
mongod soft cpu unlimited
mongod soft as unlimited
mongod soft nofile 64000
mongod soft nproc 64000
mongod hard fsize unlimited
mongod hard cpu unlimited
mongod hard as unlimited
mongod hard nofile 64000
mongod hard nproc 64000
HERE
方式三 :
源码安装
1.升级gcc,g++(>=4.8.2)
2.升级scons
rpm -ivh scons-2.4.1-1.noarch.rpm
3.安装
tar -xvf /mnt/samba/mongodb-src-r3.2.0.gz -C
/usr/local/src
cd /usr/local/src/mongodb-src-r3.2.0
scons --prefix=/opt/mongodb --full install