mongoDB-3.x安装配置(单机版)

本文详细介绍了如何在 CentOS 6.5 环境下通过 yum 和二进制包两种方式安装配置 MongoDB 3.2.0 单机版,包括安装步骤、启动关闭方法、指定配置文件、内核参数优化、系统资源限制调整等关键操作。
mongoDB-3.x 安装配置(单机版)
官方文档:

环境:
CentOS6.5 x64
mongoDB-3.2.0

方式一 : yum安装
请参看 yum安装mongoDB


方式二 : 二进制包
1.安装
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
mongoDB-3.x安装配置(单机版)

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:
  destination: file
  logAppend: true
  logRotate: rename
  timeStampFormat: ctime
  path: /opt/mongodb/log/mongod.log

# Where and how to store data.
storage:
  dbPath: /opt/mongodb/db
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongodb/mongod.pid  # location of pidfile

# network interfaces
net:
  port: 27017
  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
  http:
    enabled: true
    JSONPEnabled: true
    RESTInterfaceEnabled: true
setParameter:
   enableLocalhostAuthBypass: false

#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
mongoDB-3.x安装配置(单机版)
或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 mongod and mongos deployments:

  • -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 mongod and mongos instances after changing the ulimit settings to ensure that the changes take effect.

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

转载于:https://www.cnblogs.com/lixuebin/p/10814273.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值