广告
返回顶部
首页 > 资讯 > 数据库 >mongodb3.2 sharding deploy
  • 237
分享到

mongodb3.2 sharding deploy

2024-04-02 19:04:59 237人浏览 泡泡鱼
摘要

一、部署软件pc1、pc2、pc3分别安装mongodb,操作如下:[root@pc1 ~]# tail /etc/security/limits.confmonGod soft npr

一、部署软件

pc1、pc2、pc3分别安装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 65535
push - nproc 65535
push - nofile 320000
work - nproc 10000

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

*          soft    nproc     1024
root       soft    nproc     unlimited

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

[mongoDB-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.2/x86_64/
gpGCheck=1
enabled=1
gpgkey=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.log
storage:
  dbPath: /data/mongodb/data/shard1_1
  directoryPerDB: true
  journal:
    enabled: true
proceSSManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/shard1.pid  # location of pidfile
net:
  port: 27027
  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
replication:
  oplogSizeMB: 500
  replSetName: shard1
sharding:
  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/functions
CONFIGFILE="/data/mongodb/config/shard1.conf"
OPTIONS=" -f $CONFIGFILE"
mongod=${MONGOD-/usr/bin/mongod}
MONGO_USER=mongod
MONGO_GROUP=mongod
SYSCONFIG="/etc/sysconfig/mongod"
if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi
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
PIDFILEPATH=`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
  fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
  # 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=0
case "$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=1
esac
exit $RETVAL


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

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

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/data/logs/shard2.log
storage:
  dbPath: /data/mongodb/data/shard2_1
  directoryPerDB: true
  journal:
    enabled: true
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/shard2.pid  # location of pidfile
net:
  port: 27028
  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
replication:
  oplogSizeMB: 500
  replSetName: shard2
sharding:
  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/functions
CONFIGFILE="/data/mongodb/config/shard2.conf"
OPTIONS=" -f $CONFIGFILE"
mongod=${MONGOD-/usr/bin/mongod}
MONGO_USER=mongod
MONGO_GROUP=mongod
SYSCONFIG="/etc/sysconfig/mongod"
if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi
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
PIDFILEPATH=`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
  fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
  # 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=0
case "$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=1
esac
exit $RETVAL


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

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

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/data/logs/shard3.log
storage:
  dbPath: /data/mongodb/data/shard3_1
  directoryPerDB: true
  journal:
    enabled: true
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/shard3.pid  # location of pidfile
net:
  port: 27026
  #bindIp: 127.0.0.1  # Listen to local interface only, comment to listen on all interfaces.
replication:
  oplogSizeMB: 500
  replSetName: shard3
sharding:
  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{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   fi if test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defrag fi   # 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=0 case "$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=1 esac exit $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.7
connecting to: localhost:27029/test
configrs:SECONDARY> use admin
switched to db admin
configrs: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> exit
bye
[root@pc1 ~]#


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

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

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/data/logs/mongos.log
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongos.pid  # location of pidfile
net:
  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/functions
CONFIGFILE="/data/mongodb/config/mongos.conf"
OPTIONS=" -f $CONFIGFILE"
mongod=${MONGOD-/usr/bin/mongos}
MONGO_USER=mongod
MONGO_GROUP=mongod
SYSCONFIG="/etc/sysconfig/mongod"
if [ -f "$SYSCONFIG" ]; then
    . "$SYSCONFIG"
fi
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
PIDFILEPATH=`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
  fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
  # 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=0
case "$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=1
esac
exit $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:  yes
Currently running:  no
Failed balancer rounds in last 5 attempts:  0
Migration 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:  yes
Currently running:  no
Failed balancer rounds in last 5 attempts:  5
Last reported error:  mongos specified a different config database string : stored : pc1:27029 vs given : pc1:27029,pc2:27029,pc3:27029
Time 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%15),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:  yes
Currently running:  no
Failed balancer rounds in last 5 attempts:  0
Migration Results for the last 24 hours:
2 : Success
1 : Failed with error 'aborted', from shard1 to shard2
  databases:
{  "_id" : "test",  "primary" : "shard1",  "partitioned" : true }
test.user
shard key: { "uid" : 1 }
unique: false
balancing: true
chunks:
shard11
shard21
shard31
{ "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

actionlog
changelog
chunks
collections
databases
lockpings
locks
mongos
settings
shards
tags
version

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})

您可能感兴趣的文档:

--结束END--

本文标题: mongodb3.2 sharding deploy

本文链接: https://www.lsjlt.com/news/42459.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mongodb3.2 sharding deploy
    一、部署软件pc1、pc2、pc3分别安装mongodb,操作如下:[root@pc1 ~]# tail /etc/security/limits.confmongod soft npr...
    99+
    2022-10-18
  • mongodb3.2安装
    mongodb3.x版本有好多新功能,关于这方面参考官网即可。。。mongodb3.x配置文件使用yaml格式,和salt以及ansible格式一样一样滴,哈哈。。。下载mongodb3.2版本[root@...
    99+
    2022-10-18
  • mongodb3.2 replica sets
    一、配置文件 /etc/mongod.conf[root@mongo01 ~]# egrep -v "^(#|$)" /etc/mong...
    99+
    2022-10-18
  • mongodb3.2+性能监视
    我这是复制集模式,加个认证参数--authenticationDatabasemongostat --port 20012 -uadmin -p='xxxxx' --authenticationDataba...
    99+
    2022-10-18
  • MongoDB3.2如何升级至3.4.6
    MongoDB3.2如何升级至3.4.6,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。MongoDB 升级测试步骤:1、MongoDB版本升级...
    99+
    2022-10-18
  • MONGODB SHARDING
    Sharding Introduction Sharding is a method for storing data across multiple machines. MongoDB ...
    99+
    2022-10-18
  • MySQL Sharding
    Sharding分类: ...
    99+
    2022-10-18
  • mongodb3.2安装与基本配置
    一、使用yum安装mongodb 3.2[root@node2 ~]# cat /etc/yum.repos.d/mongodb.repo [mongodb-org-3.2] ...
    99+
    2022-10-18
  • Linux Deploy怎么使用
    这篇文章主要讲解了“Linux Deploy怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux Deploy怎么使用”吧!Linux Deploy是一款可以在Android设备...
    99+
    2023-07-05
  • 如何利用ceph-deploy
    这篇文章主要为大家展示了“如何利用ceph-deploy”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何利用ceph-deploy”这篇文章吧。利用ceph-deploy利用ceph-depl...
    99+
    2023-06-27
  • Linux Deploy该如何安装
    今天给大家介绍一下Linux Deploy该如何安装。文章的内容小编觉得不错,现在给大家分享一下,觉得有需要的朋友可以了解一下,希望对大家有所帮助,下面跟着小编的思路一起来阅读吧。Linux Deploy 是一个在 Android 上运行的...
    99+
    2023-06-28
  • linux deploy怎么安装php
    本文操作环境:Windows7系统、PHP7.1、Dell G3。linux deploy怎么安装php?[Linux Deploy]安装PHP环境继上篇安装完 Dotnet环境后,想到PHP很多好用的开源的系统,于是在手机的Linux 中...
    99+
    2016-06-03
    linux deploy php
  • linux deploy如何安装php
    本篇内容介绍了“linux deploy如何安装php”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!linux deploy安装php的方法...
    99+
    2023-06-21
  • Linux deploy该如何入门
    今天就跟大家聊聊有关Linux deploy该如何入门,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。作为一名程序员,不管是前端还是后端,自然是离不开和服务器打交道的。比如我就一直想有...
    99+
    2023-06-28
  • [MongoDB] Sharding 分片
    ...
    99+
    2022-10-18
  • MongoDB sharding分片
    背景当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量。这时,我们就可以通过在多台机器上分割数据,使得数据库系统能存储和处理更多的数据。1、MongoDB sh...
    99+
    2022-10-18
  • linux deploy的作用是什么
    本文小编为大家详细介绍“linux deploy的作用是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“linux deploy的作用是什么”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。linux deplo...
    99+
    2023-07-06
  • mongodb之sharding搭建
    mongodb版本3.2.7资源划分:192.168.1.11:27017----》config server192.168.1.11:27018----》mongos192.168.1.11:27019-...
    99+
    2022-10-18
  • Linux系统中怎么安装deploy
    本篇内容主要讲解“Linux系统中怎么安装deploy”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Linux系统中怎么安装deploy”吧!一、Deploy简介:Linux deploy是一个...
    99+
    2023-06-28
  • sharding切分是什么
    今天就跟大家聊聊有关sharding切分是什么,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。什么是切分  数据库切分是一个固有的关系流程,可以通过一...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作