MongoDB 单实例部署 [ 非源码编译安装 ]

MongoDB 官方网址:
http://www.mongodb.org

本文,主要介绍 MongoDB 单实例的安装部署~

1.下载MongoDB的安装包并解压,安装相关依赖包 [ 这里不采用源码编译安装 ]
yum -y install net-snmp.x86_64 net-snmp-devel.x86_64 net-snmp-libs.x86_64
mkdir /usr/local/mongodb
tar -xf mongodb-linux-x86_64-enterprise-rhel62-3.0.2.tgz
mv mongodb-linux-x86_64-enterprise-rhel62-3.0.2 /usr/local/mongodb

2. 创建数据目录,日志目录,配置目录等相关目录
cd /usr/local/mongodb
mkdir data conf log

3.编辑MongoDB 的配置文件:
vim conf/mongodb.conf
port=27017
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
fork=true

4.启动/关闭MongoDB:
(1).启动MongoDB:
/usr/local/mongodb/bin/mongod –config /usr/local/mongodb/conf/mongodb.conf

(2).关闭MongoDB:
/usr/local/mongodb/bin/mongod –config /usr/local/mongodb/conf/mongodb.conf –shutdown

(3).主从启动的相关要点: [也可将其参数写入配置文件 master=true ]
A. 如果mongodb 做为 master 的话,只要在启动时,加 –master 参数即可:
ex:
/usr/local/mongodb/bin/mongod –config /usr/local/mongodb/conf/mongodb.conf –master
# 同时,查看日志,日志会有相关master 的信息

B. 如果mongodb 做为 SLAVE 的话,只要在启动时,加 –slave –source ip:端口 参数即可:
ex:
/usr/local/mongodb/bin/mongod –config /usr/local/mongodb/conf/mongodb.conf –slave –source 172.16.10.55:27017
# 同时,查看日志,日志会有相关slave 的信息

C.主从数据源信息的变更:
> db.printSlaveReplicationInfo();
source: 172.16.10.5627017
doing initial sync
> use local
> db.sources.find();
{ “_id” : ObjectId(“5549957a542399f1888879b2”), “host” : “172.16.10.5627017”, “source” : “main” }
> db.sources.insert({“host”: “172.16.10.56:27017”}); # 添加一个新的主数据源
WriteResult({ “nInserted” : 1 })
> db.sources.find();
{ “_id” : ObjectId(“5549957a542399f1888879b2”), “host” : “172.16.10.5627017”, “source” : “main” }
{ “_id” : ObjectId(“5549c25670656588c7058016”), “host” : “172.16.10.56:27017” }
> db.sources.remove({“host”: “172.16.10.5627017”}); # 删除一个已存在的数据源
WriteResult({ “nRemoved” : 1 })
> db.sources.find();
{ “_id” : ObjectId(“5549c25670656588c7058016”), “host” : “172.16.10.56:27017” }

D.查看主从复制信息:
> db.printReplicationInfo();
configured oplog size: 1550MB
log length start to end: 72993secs (20.28hrs)
oplog first event time: Tue May 05 2015 17:27:01 GMT+0800 (CST)
oplog last event time: Wed May 06 2015 13:43:34 GMT+0800 (CST)
now: Wed May 06 2015 14:24:21 GMT+0800 (CST)
> db.printSlaveReplicationInfo();
source: 172.16.10.55:27017
syncedTo: Wed May 06 2015 14:24:24 GMT+0800 (CST)
13 secs (0 hrs) behind the freshest member (no primary available at the moment)

5.注意事项:
启动后,日志信息如下,红色警告部分,是我们需要进行调整的地方:
2015-05-05T15:14:48.310+0800 I JOURNAL [initandlisten] journal dir=/usr/local/mongodb/data/journal
2015-05-05T15:14:48.311+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-05-05T15:14:48.368+0800 I JOURNAL [durability] Durability thread started
2015-05-05T15:14:48.368+0800 I JOURNAL [journal writer] Journal writer thread started
2015-05-05T15:14:48.369+0800 I CONTROL [initandlisten] MongoDB starting : pid=8874 port=27017 dbpath=/usr/local/mongodb/data 64-bit host=Templet
2015-05-05T15:14:48.369+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-05-05T15:14:48.369+0800 I CONTROL [initandlisten]
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten]
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is ‘always’.
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten]
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is ‘always’.
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] ** We suggest setting it to ‘never’
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten]
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] db version v3.0.2
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] git version: 6201872043ecbbc0a4cc169b5482dcf385fc464f modules: enterprise
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] build info: Linux ip-10-171-117-53 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 BOOST_LIB_VERSION=1_49
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] allocator: tcmalloc
2015-05-05T15:14:48.370+0800 I CONTROL [initandlisten] options: { config: “/usr/local/mongodb/conf/mongodb.conf”, net: { port: 27017 }, processManagement: { fork: true }, storage: { dbPath: “/usr/local/mongodb/data” }, systemLog: { destination: “file”, logAppend: true, path: “/usr/local/mongodb/log/mongodb.log” } }
2015-05-05T15:14:48.372+0800 I INDEX [initandlisten] allocating new ns file /usr/local/mongodb/data/local.ns, filling with zeroes…
2015-05-05T15:14:48.435+0800 I STORAGE [FileAllocator] allocating new datafile /usr/local/mongodb/data/local.0, filling with zeroes…
2015-05-05T15:14:48.435+0800 I STORAGE [FileAllocator] creating directory /usr/local/mongodb/data/_tmp
2015-05-05T15:14:48.439+0800 I STORAGE [FileAllocator] done allocating datafile /usr/local/mongodb/data/local.0, size: 64MB, took 0 secs
2015-05-05T15:14:48.450+0800 I NETWORK [initandlisten] waiting for connections on port 27017

下面,我们进行调整:
(1).创建mongodb用户
注:Mongodb不建议使用root用户来运行
# groupadd -g 1010 mongodb
# useradd -g 1010 -u 1010 mongodb
调整目录和文件权限
# cd /usr/local/mongodb
# chown -R mongodb. data/ log/

(2).内核参数调整
调整前:
# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] never
# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] never
调整方法:
# echo “never” > /sys/kernel/mm/transparent_hugepage/enabled
# echo “never” > /sys/kernel/mm/transparent_hugepage/defrag
调整后:
# cat /sys/kernel/mm/transparent_hugepage/enabled
always [never]
# cat /sys/kernel/mm/transparent_hugepage/defrag
always [never]

(3).调整限制参数
启动日志,提示启动mongodb的这个用户能打开的进程数不到能打开的文件数的二分之一;
建议调整该用户能打开进程数为能打开的文件数的二分之一以上;
在CentOS系统中,默认情况用户能打开的进程数和文件数都为1024,因此不会出现该提示;
至于限制参数可以通过ulimit命令和/etc/security/limits.conf来设置
设置完毕后重启服务(切换到 mongodb 用户,启动mongodb)

Author: Michael

关注数据分析,挖掘数据金矿。

Leave a Reply

Your email address will not be published. Required fields are marked *