本文使用的MongoDB版本是2.4.6,采用如下部署架构用于测试使用,不可用于线上环境。以下测试都是在本机上完成,线上环境需要各个组件分开部署。

1.添加主机列表
vim /etc/hosts 添加如下列表
127.0.0.1 mongo-sharding-router1 127.0.0.1 mongo-sharding-router2 127.0.0.1 mongo-sharding-config1 127.0.0.1 mongo-sharding-config2 127.0.0.1 mongo-sharding-config3 127.0.0.1 mongo-sharding-replica1 127.0.0.1 mongo-sharding-replica2 127.0.0.1 mongo-sharding-replica3 127.0.0.1 mongo-sharding-replica4 127.0.0.1 mongo-sharding-replica5 127.0.0.1 mongo-sharding-replica6
2.配置两个Shard,各自拥有三个成员的Replica Set
相应的主机列表
shard1
127.0.0.1 mongo-sharding-replica1 2811 Primary 127.0.0.1 mongo-sharding-replica2 2822 Secondary 127.0.0.1 mongo-sharding-replica3 2833 Secondary
shard2
127.0.0.1 mongo-sharding-replica4 3811 Primary 127.0.0.1 mongo-sharding-replica5 3822 Secondary 127.0.0.1 mongo-sharding-replica6 3833 Secondary
部署架构如下:

A.分别配置mongo-sharding-replica1,mongo-sharding-replica2和mongo-sharding-replica3的MongoDB的配置文件和启动脚本并启动MongoDB
如mongo-sharding-replica1相关配置
# cat /etc/mongod2811.conf logpath=/data/app_data/mongodb/log2811/mongodb.log logappend=true fork=true port=2811 dbpath=/data/app_data/mongodb/data2811/ pidfilepath=/data/app_data/mongodb/data2811/mongod.pid maxConns=2048 nohttpinterface=true directoryperdb=true replSet=test_shard1
/etc/init.d/mongod2811
#!/bin/bash
# mongod - Startup script for mongod
# chkconfig: 35 85 15
# description: Mongo is a scalable, document-oriented database.
# processname: mongod
# config: /etc/mongod.conf
# pidfile: /data/app_data/mongodb/data/mongodb/mongod.pid
. /etc/rc.d/init.d/functions
# things from mongod.conf get there by mongod reading it
# NOTE: if you change any OPTIONS here, you get what you pay for:
# this script assumes all options are in the config file.
CONFIGFILE="/etc/mongod2811.conf"
OPTIONS=" -f $CONFIGFILE"
SYSCONFIG="/etc/sysconfig/mongod"
# FIXME: 1.9.x has a --shutdown flag that parses the config file and
# shuts down the correct running pid, but that‘s unavailable in 1.8
# for now. This can go away when this script stops supporting 1.8.
DBPATH=`awk -F= ‘/^dbpath=/{print $2}‘ "$CONFIGFILE"`
PIDFILE=`awk -F= ‘/^pidfilepath=/{print $2}‘ "$CONFIGFILE"`
mongod=${MONGOD-/data/app_platform/mongodb/bin/mongod}
MONGO_USER=mongod
MONGO_GROUP=mongod
if [ -f "$SYSCONFIG" ]; then
. "$SYSCONFIG"
fi
# Handle NUMA access to CPUs (SERVER-3574)
# This verifies the existence of numactl as well as testing that the command works
NUMACTL_ARGS="--interleave=all"
if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/null
then
NUMACTL="numactl $NUMACTL_ARGS"
else
NUMACTL=""
fi
start()
{
echo -n $"Starting mongod: "
daemon --user "$MONGO_USER" $NUMACTL $mongod $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}
stop()
{
echo -n $"Stopping mongod: "
killproc -p "$PIDFILE" -d 300 /data/app_platform/mongodb/bin/mongod
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}
restart () {
stop
start
}
ulimit -n 12000
RETVAL=0
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/mongod ] && restart || :
;;
status)
status $mongod
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac
exit $RETVAL注意/data/app_data/mongodb/这个目录的权限设置成为mongod进程运行的用户权限。
# service mongod2811 start Starting mongod: about to fork child process, waiting until server is ready for connections. all output going to: /data/app_data/mongodb/log2811/mongodb.log forked process: 4589 child process started successfully, parent exiting [ OK ] # service mongod2822 start Starting mongod: about to fork child process, waiting until server is ready for connections. all output going to: /data/app_data/mongodb/log2822/mongodb.log forked process: 4656 child process started successfully, parent exiting [ OK ] # service mongod2833 start Starting mongod: about to fork child process, waiting until server is ready for connections. all output going to: /data/app_data/mongodb/log2833/mongodb.log forked process: 4723 child process started successfully, parent exiting [ OK ]
B.初始化Replica Set
# /data/app_platform/mongodb/bin/mongo --port 2811
MongoDB shell version: 2.4.6
connecting to: 127.0.0.1:2811/test
> rs.status();
{
"startupStatus" : 3,
"info" : "run rs.initiate(...) if not yet done for the set",
"ok" : 0,
"errmsg" : "can‘t get local.system.replset config from self or any seed (EMPTYCONFIG)"
}
> rs.initiate("mongo-sharding-replica1:2811");
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "mongo-sharding-replica1:2811",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
>
test_shard1:PRIMARY>
test_shard1:PRIMARY> rs.status();
{
"set" : "test_shard1",
"date" : ISODate("2015-01-07T10:16:51Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "mongo-sharding-replica1:2811",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 569,
"optime" : Timestamp(1420625715, 1),
"optimeDate" : ISODate("2015-01-07T10:15:15Z"),
"self" : true
}
],
"ok" : 1
}C.向Replica Set添加mongo-sharding-replica2和mongo-sharding-replica3
test_shard1:PRIMARY> rs.add("mongo-sharding-replica2:2822");
{ "ok" : 1 }
test_shard1:PRIMARY> rs.add("mongo-sharding-replica3:2833");
{ "ok" : 1 }
test_shard1:PRIMARY> rs.config();
{
"_id" : "test_shard1",
"version" : 5,
"members" : [
{
"_id" : 0,
"host" : "mongo-sharding-replica1:2811"
},
{
"_id" : 1,
"host" : "mongo-sharding-replica2:2822"
},
{
"_id" : 2,
"host" : "mongo-sharding-replica3:2833"
}
]
}
test_shard1:PRIMARY>D.查看mongo-sharding-replica2:2822和mongo-sharding-replica3:2833的状态
# /data/app_platform/mongodb/bin/mongo --port 2822
MongoDB shell version: 2.4.6
connecting to: 127.0.0.1:2822/test
test_shard1:SECONDARY> db.isMaster();
{
"setName" : "test_shard1",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"mongo-sharding-replica2:2822",
"mongo-sharding-replica3:2833",
"mongo-sharding-replica1:2811"
],
"primary" : "mongo-sharding-replica1:2811",
"me" : "mongo-sharding-replica2:2822",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2015-01-07T10:21:41.522Z"),
"ok" : 1
}
test_shard1:SECONDARY># /data/app_platform/mongodb/bin/mongo --port 2833
MongoDB shell version: 2.4.6
connecting to: 127.0.0.1:2833/test
test_shard1:SECONDARY> db.isMaster();
{
"setName" : "test_shard1",
"ismaster" : false,
"secondary" : true,
"hosts" : [
"mongo-sharding-replica3:2833",
"mongo-sharding-replica2:2822",
"mongo-sharding-replica1:2811"
],
"primary" : "mongo-sharding-replica1:2811",
"me" : "mongo-sharding-replica3:2833",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"localTime" : ISODate("2015-01-07T10:22:27.125Z"),
"ok" : 1
}
test_shard1:SECONDARY>同理对shard2的MongoDB实例进行相同的配置
3.配置config server
参考文章:
http://docs.mongodb.org/manual/administration/sharded-cluster-deployment/
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1600189
原文:http://john88wang.blog.51cto.com/2165294/1600189