千家信息网

mongodb3.2 sharding deploy

发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,一、部署软件pc1、pc2、pc3分别安装mongodb,操作如下:[root@pc1 ~]# tail /etc/security/limits.confmongod soft nproc 4000
千家信息网最后更新 2025年01月21日mongodb3.2 sharding deploy

一、部署软件

pc1pc2pc3分别安装mongodb,操作如下:

[root@pc1 ~]# tail /etc/security/limits.conf

mongod soft nproc 40000* hard nofile 1000000* soft nofile 1000000* soft core unlimited* soft stack 10240* - nofile 65535push - nproc 65535push - nofile 320000work - nproc 10000

[root@pc1 ~]# tail /etc/security/limits.d/90-nproc.conf

*          soft    nproc     1024root       soft    nproc     unlimited

[root@pc1 ~]# cat /etc/yum.repos.d/mongodb.repo

[mongodb-org-3.2]name=MongoDB Repositorybaseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.2/x86_64/gpgcheck=1enabled=1gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

[root@pc1 ~]# yum -y install mongodb-org

[root@pc1 ~]# chkconfig mongodb off

[root@pc1 ~]# mkdir -p /data/mongodb/{config,data/{config,logs,shard{1,2,3}_1}}

[root@pc1 ~]# chown -R mongod.mongod /data/mongodb

[root@pc1 ~]# cp /etc/mongod.conf /data/mongodb/config/shard1.conf

[root@pc1 ~]# tree /data/

/data/

└── mongodb

── config

│ └── shard1.conf

└── data

── config

── logs

── shard1_1

── shard2_1

└── shard3_1

二、配置服务

副本集1的配置文件与启动脚本如下:

[root@pc1 ~]# egrep "^[^#$]" /data/mongodb/config/shard1.conf

systemLog:  destination: file  logAppend: true  path: /data/mongodb/data/logs/shard1.logstorage:  dbPath: /data/mongodb/data/shard1_1  directoryPerDB: true  journal:    enabled: trueprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/shard1.pid  # location of pidfilenet:  port: 27027  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.replication:  oplogSizeMB: 500  replSetName: shard1sharding:  clusterRole: shardsvr

[root@pc1 ~]# cp /etc/init.d/mongod /etc/init.d/shard1

[root@pc1 ~]# vim /etc/init.d/shard1

[root@pc1 ~]# egrep "^[^#$]" /etc/init.d/shard1

. /etc/rc.d/init.d/functionsCONFIGFILE="/data/mongodb/config/shard1.conf"OPTIONS=" -f $CONFIGFILE"mongod=${MONGOD-/usr/bin/mongod}MONGO_USER=mongodMONGO_GROUP=mongodSYSCONFIG="/etc/sysconfig/mongod"if [ -f "$SYSCONFIG" ]; then    . "$SYSCONFIG"fiNUMACTL_ARGS="--interleave=all"if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/nullthen    NUMACTL="numactl $NUMACTL_ARGS"else    NUMACTL=""fiPIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | awk -F'#' '{print $1}'`PIDDIR=`dirname $PIDFILEPATH`start(){  # Make sure the default pidfile directory exists  if [ ! -d $PIDDIR ]; then    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR  fiif test -f /sys/kernel/mm/transparent_hugepage/defrag; thenecho never > /sys/kernel/mm/transparent_hugepage/defragfi  # Recommended ulimit values for mongod or mongos  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings  #  ulimit -f unlimited  ulimit -t unlimited  ulimit -v unlimited  ulimit -n 64000  ulimit -m unlimited  ulimit -u 64000  echo -n $"Starting mongod: "  daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/shard1}stop(){  echo -n $"Stopping mongod: "  mongo_killproc "$PIDFILEPATH" $mongod  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/shard1}restart () {        stop        start}mongo_killproc(){  local pid_file=$1  local procname=$2  local -i delay=300  local -i duration=10  local pid=`pidofproc -p "${pid_file}" ${procname}`  kill -TERM $pid >/dev/null 2>&1  usleep 100000  local -i x=0  while [ $x -le $delay ] && checkpid $pid; do    sleep $duration    x=$(( $x + $duration))  done  kill -KILL $pid >/dev/null 2>&1  usleep 100000  checkpid $pid # returns 0 only if the process exists  local RC=$?  [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"  RC=$((! $RC)) # invert return code so we return 0 when process is dead.  return $RC}RETVAL=0case "$1" in  start)    start    ;;  stop)    stop    ;;  restart|reload|force-reload)    restart    ;;  condrestart)    [ -f /var/lock/subsys/shard1 ] && restart || :    ;;  status)    status $mongod    RETVAL=$?    ;;  *)    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"    RETVAL=1esacexit $RETVAL


副本集2的配置文件与启动脚本如下:

[root@pc1 ~]# egrep "^[^#$]" /data/mongodb/config/shard2.conf

systemLog:  destination: file  logAppend: true  path: /data/mongodb/data/logs/shard2.logstorage:  dbPath: /data/mongodb/data/shard2_1  directoryPerDB: true  journal:    enabled: trueprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/shard2.pid  # location of pidfilenet:  port: 27028  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.replication:  oplogSizeMB: 500  replSetName: shard2sharding:  clusterRole: shardsvr

[root@pc1 ~]# cp /etc/init.d/mongod /etc/init.d/shard2

[root@pc1 ~]# vim /etc/init.d/shard2

[root@pc1 ~]# egrep "^[^#$]" /etc/init.d/shard2

. /etc/rc.d/init.d/functionsCONFIGFILE="/data/mongodb/config/shard2.conf"OPTIONS=" -f $CONFIGFILE"mongod=${MONGOD-/usr/bin/mongod}MONGO_USER=mongodMONGO_GROUP=mongodSYSCONFIG="/etc/sysconfig/mongod"if [ -f "$SYSCONFIG" ]; then    . "$SYSCONFIG"fiNUMACTL_ARGS="--interleave=all"if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/nullthen    NUMACTL="numactl $NUMACTL_ARGS"else    NUMACTL=""fiPIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | awk -F'#' '{print $1}'`PIDDIR=`dirname $PIDFILEPATH`start(){  # Make sure the default pidfile directory exists  if [ ! -d $PIDDIR ]; then    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR  fiif test -f /sys/kernel/mm/transparent_hugepage/defrag; thenecho never > /sys/kernel/mm/transparent_hugepage/defragfi  # Recommended ulimit values for mongod or mongos  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings  #  ulimit -f unlimited  ulimit -t unlimited  ulimit -v unlimited  ulimit -n 64000  ulimit -m unlimited  ulimit -u 64000  echo -n $"Starting mongod: "  daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/shard2}stop(){  echo -n $"Stopping mongod: "  mongo_killproc "$PIDFILEPATH" $mongod  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/shard2}restart () {        stop        start}mongo_killproc(){  local pid_file=$1  local procname=$2  local -i delay=300  local -i duration=10  local pid=`pidofproc -p "${pid_file}" ${procname}`  kill -TERM $pid >/dev/null 2>&1  usleep 100000  local -i x=0  while [ $x -le $delay ] && checkpid $pid; do    sleep $duration    x=$(( $x + $duration))  done  kill -KILL $pid >/dev/null 2>&1  usleep 100000  checkpid $pid # returns 0 only if the process exists  local RC=$?  [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"  RC=$((! $RC)) # invert return code so we return 0 when process is dead.  return $RC}RETVAL=0case "$1" in  start)    start    ;;  stop)    stop    ;;  restart|reload|force-reload)    restart    ;;  condrestart)    [ -f /var/lock/subsys/shard2 ] && restart || :    ;;  status)    status $mongod    RETVAL=$?    ;;  *)    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"    RETVAL=1esacexit $RETVAL


副本集3的配置文件与启动脚本如下:

[root@pc1 ~]# egrep "^[^#$]" /data/mongodb/config/shard3.conf

systemLog:  destination: file  logAppend: true  path: /data/mongodb/data/logs/shard3.logstorage:  dbPath: /data/mongodb/data/shard3_1  directoryPerDB: true  journal:    enabled: trueprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/shard3.pid  # location of pidfilenet:  port: 27026  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.replication:  oplogSizeMB: 500  replSetName: shard3sharding:  clusterRole: shardsvr

[root@pc1 ~]# cp /etc/init.d/mongod /etc/init.d/shard3

[root@pc1 ~]# vim /etc/init.d/shard3

[root@pc1 ~]# egrep "^[^#$]" /etc/init.d/shard3 (略,内容基本与shard2 、shard1相同)

复制配置文件到pc2pc3,并启动服务

[root@pc1 ~]# scp /data/mongodb/config/*.conf pc2:/data/mongodb/config/

[root@pc1 ~]# scp /etc/init.d/shard* pc2:/etc/init.d/

[root@pc1 ~]# scp /data/mongodb/config/*.conf pc3:/data/mongodb/config/

[root@pc1 ~]# scp /etc/init.d/shard* pc2:/etc/init.d/

[root@pc1 ~]#

[root@pc1 ~]# /etc/init.d/shard1 start

[root@pc1 ~]# ssh pc2 '/etc/init.d/shard1 start'

[root@pc1 ~]# ssh pc3 '/etc/init.d/shard1 start'


[root@pc1 ~]# /etc/init.d/shard2 start

[root@pc1 ~]# ssh pc2 '/etc/init.d/shard2 start'

[root@pc1 ~]# ssh pc3 '/etc/init.d/shard2 start'

[root@pc1 ~]# /etc/init.d/shard3 start

[root@pc1 ~]# ssh pc2 '/etc/init.d/shard3 start'

[root@pc1 ~]# ssh pc3 '/etc/init.d/shard3 start'


配置副本集1如下操作:

[root@pc1 ~]# mongo localhost:27027

MongoDB shell version: 3.2.7connecting to: localhost:27027/test> use adminswitched to db admin> rs.initiate(){"info2" : "no configuration specified. Using a default configuration for the set","me" : "pc1:27027","ok" : 1}shard1:OTHER> rs.add("pc2:27027"){ "ok" : 1 }shard1:PRIMARY> rs.addArb("pc3:27027"){ "ok" : 1 }shard1:PRIMARY> rs.status(){"set" : "shard1","date" : ISODate("2016-06-28T03:59:41.403Z"),"myState" : 1,"term" : NumberLong(1),"heartbeatIntervalMillis" : NumberLong(2000),"members" : [{"_id" : 0,"name" : "pc1:27027","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 1634,"optime" : {"ts" : Timestamp(1467086346, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2016-06-28T03:59:06Z"),"infoMessage" : "could not find member to sync from","electionTime" : Timestamp(1467086301, 2),"electionDate" : ISODate("2016-06-28T03:58:21Z"),"configVersion" : 3,"self" : true},{"_id" : 1,"name" : "pc2:27027","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 54,"optime" : {"ts" : Timestamp(1467086346, 1),"t" : NumberLong(1)},"optimeDate" : ISODate("2016-06-28T03:59:06Z"),"lastHeartbeat" : ISODate("2016-06-28T03:59:40.050Z"),"lastHeartbeatRecv" : ISODate("2016-06-28T03:59:41.041Z"),"pingMs" : NumberLong(1),"syncingTo" : "pc1:27027","configVersion" : 3},{"_id" : 2,"name" : "pc3:27027","health" : 1,"state" : 7,"stateStr" : "ARBITER","uptime" : 35,"lastHeartbeat" : ISODate("2016-06-28T03:59:40.053Z"),"lastHeartbeatRecv" : ISODate("2016-06-28T03:59:41.044Z"),"pingMs" : NumberLong(2),"configVersion" : 3}],"ok" : 1}

配置副本集2、配置副本集3的操作与配置副本集基本相同,本文不再罗嗦。注意:配置副本集1时,pc1primarypc2secondary ,而pc3arbiter;配置副本集2时,pc2primarypc3secondary,而pc1arbiter;配置副本集3时,pc3primarypc1secondary,而pc2arbiter


config server 的配置文件与启动文件如下:

[[root@pc1 ~]# egrep "^[^#$]" /data/mongodb/config/config.conf

systemLog:  destination: file  logAppend: true  path: /data/mongodb/data/logs/config.logstorage:  dbPath: /data/mongodb/data/config  directoryPerDB: true  journal:    enabled: trueprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/config.pid  # location of pidfilenet:  port: 27029  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.replication:  oplogSizeMB: 500  replSetName: configrssharding:  clusterRole: configsvr

[root@pc1 ~]# cp /etc/init.d/mongod /etc/init.d/mconfig

[root@pc1 ~]# vim /etc/init.d/mconfig

[root@pc1 ~]# egrep "^[^#$]" /etc/init.d/mconfig

. /etc/rc.d/init.d/functionsCONFIGFILE="/data/mongodb/config/config.conf"OPTIONS=" -f $CONFIGFILE"mongod=${MONGOD-/usr/bin/mongod}MONGO_USER=mongodMONGO_GROUP=mongodSYSCONFIG="/etc/sysconfig/mongod"if [ -f "$SYSCONFIG" ]; then    . "$SYSCONFIG"fiNUMACTL_ARGS="--interleave=all"if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/nullthen    NUMACTL="numactl $NUMACTL_ARGS"else    NUMACTL=""fiPIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | awk -F'#' '{print $1}'`PIDDIR=`dirname $PIDFILEPATH`start(){  # Make sure the default pidfile directory exists  if [ ! -d $PIDDIR ]; then    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR  fiif test -f /sys/kernel/mm/transparent_hugepage/defrag; thenecho never > /sys/kernel/mm/transparent_hugepage/defragfi  # Recommended ulimit values for mongod or mongos  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings  ulimit -f unlimited  ulimit -t unlimited  ulimit -v unlimited  ulimit -n 64000  ulimit -m unlimited  ulimit -u 64000  echo -n $"Starting mongod: "  daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mconfig}stop(){  echo -n $"Stopping mongod: "  mongo_killproc "$PIDFILEPATH" $mongod  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mconfig}restart () {        stop        start}mongo_killproc(){  local pid_file=$1  local procname=$2  local -i delay=300  local -i duration=10  local pid=`pidofproc -p "${pid_file}" ${procname}`  kill -TERM $pid >/dev/null 2>&1  usleep 100000  local -i x=0  while [ $x -le $delay ] && checkpid $pid; do    sleep $duration    x=$(( $x + $duration))  done  kill -KILL $pid >/dev/null 2>&1  usleep 100000  checkpid $pid # returns 0 only if the process exists  local RC=$?  [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"  RC=$((! $RC)) # invert return code so we return 0 when process is dead.  return $RC}RETVAL=0case "$1" in  start)    start    ;;  stop)    stop    ;;  restart|reload|force-reload)    restart    ;;  condrestart)    [ -f /var/lock/subsys/mconfig ] && restart || :    ;;  status)    status $mongod    RETVAL=$?    ;;  *)    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"    RETVAL=1esacexit $RETVAL

[root@pc1 ~]# scp /data/mongodb/config/config.conf pc2:/data/mongodb/config

[root@pc1 ~]# scp /etc/init.d/mconfig pc2:/etc/init.d/

[root@pc1 ~]#

[root@pc1 ~]# scp /data/mongodb/config/config.conf pc2:/data/mongodb/config

[root@pc1 ~]# scp /etc/init.d/mconfig pc3:/etc/init.d/

[root@pc1 ~]#

[root@pc1 ~]# /etc/init.d/mconfig start

Starting mongod: [确定]

[root@pc1 ~]# lsof -i:27029

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME

mongod 2765 mongod 6u IPv4 11520 0t0 TCP *:27029 (LISTEN)

[root@pc1 ~]# ssh pc2 '/etc/init.d/mconfig start'

[root@pc1 ~]# ssh pc2 '/etc/init.d/mconfig start'


为了保持config server的一致性,实现各config server的副本集功能,完成效果如下所示(config server的副本集不支持使用arbiter)

[root@pc1 ~]# mongo localhost:27029

MongoDB shell version: 3.2.7connecting to: localhost:27029/testconfigrs:SECONDARY> use adminswitched to db adminconfigrs:SECONDARY> rs.slaveOk()configrs:SECONDARY> rs.status(){"set" : "configrs","date" : ISODate("2016-07-01T03:25:49.471Z"),"myState" : 2,"term" : NumberLong(2),"syncingTo" : "pc3:27029","configsvr" : true,"heartbeatIntervalMillis" : NumberLong(2000),"members" : [{"_id" : 0,"name" : "pc1:27029","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 3937,"optime" : {"ts" : Timestamp(1467339616, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2016-07-01T02:20:16Z"),"syncingTo" : "pc3:27029","configVersion" : 3,"self" : true},{"_id" : 1,"name" : "pc2:27029","health" : 1,"state" : 2,"stateStr" : "SECONDARY","uptime" : 3936,"optime" : {"ts" : Timestamp(1467339616, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2016-07-01T02:20:16Z"),"lastHeartbeat" : ISODate("2016-07-01T03:25:48.578Z"),"lastHeartbeatRecv" : ISODate("2016-07-01T03:25:47.481Z"),"pingMs" : NumberLong(1),"syncingTo" : "pc3:27029","configVersion" : 3},{"_id" : 2,"name" : "pc3:27029","health" : 1,"state" : 1,"stateStr" : "PRIMARY","uptime" : 3936,"optime" : {"ts" : Timestamp(1467339616, 1),"t" : NumberLong(2)},"optimeDate" : ISODate("2016-07-01T02:20:16Z"),"lastHeartbeat" : ISODate("2016-07-01T03:25:49.146Z"),"lastHeartbeatRecv" : ISODate("2016-07-01T03:25:47.585Z"),"pingMs" : NumberLong(1),"electionTime" : Timestamp(1467339615, 1),"electionDate" : ISODate("2016-07-01T02:20:15Z"),"configVersion" : 3}],"ok" : 1}configrs:SECONDARY> exitbye[root@pc1 ~]#


mongos 路由配置文件与启动脚本内容如下:

[root@pc1 ~]# egrep "^[^#$]" /data/mongodb/config/mongos.conf

systemLog:  destination: file  logAppend: true  path: /data/mongodb/data/logs/mongos.logprocessManagement:  fork: true  # fork and run in background  pidFilePath: /var/run/mongodb/mongos.pid  # location of pidfilenet:  port: 27017  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.sharding:  configDB: pc1:27029,pc2:27029,pc3:27029  #注: mongodb3.4时格式必须为 configDB: configrs_name/configServer_list

[root@pc1 ~]# cp /etc/init.d/mongod /etc/init.d/mongos

[root@pc1 ~]# vim /etc/init.d/mongos

[root@pc1 ~]# egrep "^[^#$]" /etc/init.d/mongos

. /etc/rc.d/init.d/functionsCONFIGFILE="/data/mongodb/config/mongos.conf"OPTIONS=" -f $CONFIGFILE"mongod=${MONGOD-/usr/bin/mongos}MONGO_USER=mongodMONGO_GROUP=mongodSYSCONFIG="/etc/sysconfig/mongod"if [ -f "$SYSCONFIG" ]; then    . "$SYSCONFIG"fiNUMACTL_ARGS="--interleave=all"if which numactl >/dev/null 2>/dev/null && numactl $NUMACTL_ARGS ls / >/dev/null 2>/dev/nullthen    NUMACTL="numactl $NUMACTL_ARGS"else    NUMACTL=""fiPIDFILEPATH=`awk -F'[:=]' -v IGNORECASE=1 '/^[[:blank:]]*(processManagement\.)?pidfilepath[[:blank:]]*[:=][[:blank:]]*/{print $2}' "$CONFIGFILE" | tr -d "[:blank:]\"'" | awk -F'#' '{print $1}'`PIDDIR=`dirname $PIDFILEPATH`start(){  # Make sure the default pidfile directory exists  if [ ! -d $PIDDIR ]; then    install -d -m 0755 -o $MONGO_USER -g $MONGO_GROUP $PIDDIR  fiif test -f /sys/kernel/mm/transparent_hugepage/defrag; thenecho never > /sys/kernel/mm/transparent_hugepage/defragfi  # Recommended ulimit values for mongod or mongos  # See http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings  #  ulimit -f unlimited  ulimit -t unlimited  ulimit -v unlimited  ulimit -n 64000  ulimit -m unlimited  ulimit -u 64000  echo -n $"Starting mongod: "  daemon --user "$MONGO_USER" --check $mongod "$NUMACTL $mongod $OPTIONS >/dev/null 2>&1"  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongos}stop(){  echo -n $"Stopping mongod: "  mongo_killproc "$PIDFILEPATH" $mongod  RETVAL=$?  echo  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongos}restart () {        stop        start}mongo_killproc(){  local pid_file=$1  local procname=$2  local -i delay=300  local -i duration=10  local pid=`pidofproc -p "${pid_file}" ${procname}`  kill -TERM $pid >/dev/null 2>&1  usleep 100000  local -i x=0  while [ $x -le $delay ] && checkpid $pid; do    sleep $duration    x=$(( $x + $duration))  done  kill -KILL $pid >/dev/null 2>&1  usleep 100000  checkpid $pid # returns 0 only if the process exists  local RC=$?  [ "$RC" -eq 0 ] && failure "${procname} shutdown" || rm -f "${pid_file}"; success "${procname} shutdown"  RC=$((! $RC)) # invert return code so we return 0 when process is dead.  return $RC}RETVAL=0case "$1" in  start)    start    ;;  stop)    stop    ;;  restart|reload|force-reload)    restart    ;;  condrestart)    [ -f /var/lock/subsys/mongos ] && restart || :    ;;  status)    status $mongod    RETVAL=$?    ;;  *)    echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"    RETVAL=1esacexit $RETVAL

[root@pc1 ~]# scp /data/mongodb/config/mongos pc2:/data/mongodb/config/

[root@pc1 ~]# scp /etc/init.d/mongos pc2:/etc/init.d/

[root@pc1 ~]# scp /data/mongodb/config/mongos pc3:/data/mongodb/config/

[root@pc1 ~]# scp /etc/init.d/mongos pc3:/etc/init.d/

[root@pc1 ~]# /etc/init.d/mongos start

Starting mongod: [确定]

[root@pc1 ~]# ssh pc2 '/etc/init.d/mongos start'

Starting mongod: [确定]

[root@pc1 ~]# ssh pc3 '/etc/init.d/mongos start'

Starting mongod: [确定]

[root@pc1 ~]#

有些网朋友说:需要实现mongos路由的副本集(即多台mongos路由之间的复制),但从原理分析,我个人没能理解这种方案的功能,当然本人也尝试过,发现设置副本集之后mongos服务无法启动(并无深入研究,请各位朋友提宝贝意见)


三、实现分片

连接到任何一台mongos路由上,操作如下:

[root@pc1 ~]# mongo

MongoDB shell version: 3.2.7

connecting to: test

mongos> use admin

switched to db admin

mongos> sh.status()

--- Sharding Status ---  sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("577226b7511b1f96da0ddba2")}  shards:  active mongoses:"3.2.7" : 3  balancer:Currently enabled:  yesCurrently running:  noFailed balancer rounds in last 5 attempts:  0Migration Results for the last 24 hours:No recent migrations  databases:

mongos> sh.addShard("shard1/pc1:27027")

{ "shardAdded" : "shard1", "ok" : 1 }

mongos> sh.addShard("shard3/pc2:27026")

{ "shardAdded" : "shard3", "ok" : 1 }

mongos> sh.addShard("shard2/pc3:27028")

{ "shardAdded" : "shard2", "ok" : 1 }

mongos> sh.status()

--- Sharding Status ---  sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("577226b7511b1f96da0ddba2")}  shards:{  "_id" : "shard1",  "host" : "shard1/pc1:27027,pc2:27027" }{  "_id" : "shard2",  "host" : "shard2/pc1:27028,pc3:27028" }{  "_id" : "shard3",  "host" : "shard3/pc2:27026,pc3:27026" }  active mongoses:"3.2.7" : 3  balancer:Currently enabled:  yesCurrently running:  noFailed balancer rounds in last 5 attempts:  5Last reported error:  mongos specified a different config database string : stored : pc1:27029 vs given : pc1:27029,pc2:27029,pc3:27029Time of Reported error:  Tue Jun 28 2016 07:34:07 GMT+0000 (UTC)Migration Results for the last 24 hours:No recent migrations  databases:

mongos> use config

switched to db config

mongos> db.chunks.find()

mongos> db.settings.find()

{ "_id" : "chunksize", "value" : NumberLong(64) }

mongos> db.settings.save({"_id":"chunksize","value":NumberLong(5)}) #不建议修改chunk的值,本例为了实例效果改为5M。最小的chunksize(1M),默认chunksize为64M

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

mongos> db.settings.find()

{ "_id" : "chunksize", "value" : NumberLong(5) }

mongos> use admin

switched to db admin

mongos> sh.enableSharding("test")

{ "ok" : 1 }

mongos> sh.shardCollection("test.user",{uid:1})

{ "collectionsharded" : "test.user", "ok" : 1 }

mongos> use test

switched to db test

mongos> db.use.ensureIndex({uid:1})

{"raw" : {"shard1/pc1:27027,pc2:27027" : {"createdCollectionAutomatically" : true,"numIndexesBefore" : 1,"numIndexesAfter" : 2,"ok" : 1,"$gleStats" : {"lastOpTime" : Timestamp(1467102931, 2),"electionId" : ObjectId("7fffffff0000000000000001")}}},"ok" : 1}

mongos> for (i=1;i<=100000;i++) db.user.insert({uid:'user'+i,age:(i),address:'#'+i+',shangdi south road,beijing',preferbooks:['book'+i,'hello world']})

WriteResult({ "nInserted" : 1 })

mongos> sh.status()

--- Sharding Status ---  sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("577234b7c92b86a15a2901d4")}  shards:{  "_id" : "shard1",  "host" : "shard1/pc1:27027,pc2:27027" }{  "_id" : "shard2",  "host" : "shard2/pc2:27028,pc3:27028" }{  "_id" : "shard3",  "host" : "shard3/pc1:27026,pc3:27026" }  active mongoses:"3.2.7" : 3  balancer:Currently enabled:  yesCurrently running:  noFailed balancer rounds in last 5 attempts:  0Migration Results for the last 24 hours:2 : Success1 : Failed with error 'aborted', from shard1 to shard2  databases:{  "_id" : "test",  "primary" : "shard1",  "partitioned" : true }test.usershard key: { "uid" : 1 }unique: falsebalancing: truechunks:shard11shard21shard31{ "uid" : { "$minKey" : 1 } } -->> { "uid" : "user2" } on : shard2 Timestamp(2, 0){ "uid" : "user2" } -->> { "uid" : "user8" } on : shard3 Timestamp(3, 0){ "uid" : "user8" } -->> { "uid" : { "$maxKey" : 1 } } on : shard1 Timestamp(3, 1)

mongos> use config

switched to db config

mongos> show collections

actionlogchangelogchunkscollectionsdatabaseslockpingslocksmongossettingsshardstagsversion

mongos> db.settings.find()

{ "_id" : "chunksize", "value" : NumberLong(64) }

mongos> db.shards.find()

{ "_id" : "shard1", "host" : "shard1/pc1:27027,pc2:27027" }{ "_id" : "shard3", "host" : "shard3/pc1:27026,pc3:27026" }{ "_id" : "shard2", "host" : "shard2/pc2:27028,pc3:27028" }

mongos> db.databases.find()

{ "_id" : "test", "primary" : "shard1", "partitioned" : true }

mongos> db.chunks.find()

{ "_id" : "test.user-uid_MinKey", "lastmod" : Timestamp(2, 0), "lastmodEpoch" : ObjectId("57723675c92b86a15a290209"), "ns" : "test.user", "min" : { "uid" : { "$minKey" : 1 } }, "max" : { "uid" : "user2" }, "shard" : "shard2" }{ "_id" : "test.user-uid_\"user2\"", "lastmod" : Timestamp(3, 0), "lastmodEpoch" : ObjectId("57723675c92b86a15a290209"), "ns" : "test.user", "min" : { "uid" : "user2" }, "max" : { "uid" : "user8" }, "shard" : "shard3" }{ "_id" : "test.user-uid_\"user8\"", "lastmod" : Timestamp(3, 1), "lastmodEpoch" : ObjectId("57723675c92b86a15a290209"), "ns" : "test.user", "min" : { "uid" : "user8" }, "max" : { "uid" : { "$maxKey" : 1 } }, "shard" : "shard1" }

mongos>exit


四、补充命令

db.runCommand({addshard:"shard1/pc1:27027,pc2:27027,pc3:27027",name:"shard1"});

db.runCommand({addshard:"shard2/pc1:27028,pc2:27028,pc3:27028",name:"shard2"});

db.runCommand({addshard:"shard3/pc1:27026,pc2:27026,pc3:27026",name:"shard3"});

use admin

db.runCommand({enablesharding:"db_name"})

db.runCommand({shardcollection:"db_name.collection_name",key:{fields_name:"hashed"}})

db.runCommand({shardcollection:"db_name.collection_name",key:{fields_name:1}})

sh.shardCollection("db_name.collection_name",{fields_name1:1,fields_name2:1})

0