当前位置:Gxlcms > 数据库问题 > mongodb3.2 sharding deploy

mongodb3.2 sharding deploy

时间:2021-07-01 10:21:17 帮助过:19人阅读

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 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 ~]# 


[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 ~]# 

[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

[root@pc1 ~]#


副本集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 ~]# 

[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

[root@pc1 ~]# 


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

[root@pc1 ~]# 

[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 ~]# 

[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

. /etc/rc.d/init.d/functions

CONFIGFILE="/data/mongodb/config/shard3.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/shard3

}

stop()

{

  echo -n $"Stopping mongod: "

  mongo_killproc "$PIDFILEPATH" $mongod

  RETVAL=$?

  echo

  [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/shard3

}

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

}

人气教程排行