centos mongodb cluster install 完全版

分享一则用yum安装的mongodb初始化脚本。

install_mongodb.sh会安装mongodb所需的基本环境。

配置副本集的时候会需要辅助文件hosts.conf。

说明:该示例为一主一丛一仲裁。

[root@server mongodb]# cat install_mongdb.sh
#!/bin/bash

function func_yum()
{
cat >> /etc/yum.repos.d/Mongodb.repo << EOF
[mongodb-org-${MRELEASE}]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/6Server/mongodb-org/${MRELEASE}/x86_64/
gpgcheck=0
enabled=1
EOF
yum clean all
yum install -y mongodb-org
}

function func_env()
{
##configure selinux=disabled##
if [ `getenforce` = "Enforcing" ]; then
    echo "selinux=enforcing,now replace the config to disabled."
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    echo "now selinux=`getenforce`."
else
    echo "selinux=`getenforce` already."
fi
##configure limits parameters##
cat >> /etc/security/limits.conf << EOF
* soft nofile     655350
* hard nofile     655350
* soft nproc     65535
* hard nproc     65535
* soft core         unlimited
* hard core         unlimited
* soft memlock     50000000
* hard memlock     50000000
EOF
##iptables cleanning up##
iptables -F
service iptables save
echo "iptables cleaned up already."
iptables -nvL
##configure the linux system core parameters
cat > /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 0
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 68719476736
kernel.shmall = 4294967296
net.ipv4.conf.all.rp_filter = 0
net.ipv4.conf.default.rp_filter = 0
kernel.shmmni = 4096
kernel.sem = 50100 64128000 50100 1280
fs.file-max = 655350
net.ipv4.ip_local_port_range = 10000 65000
net.core.rmem_default = 1048576
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_syn_backlog = 4096
net.core.netdev_max_backlog = 10000
vm.overcommit_memory = 0
fs.aio-max-nr = 1048576
net.ipv4.tcp_timestamps = 0
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
EOF
sysctl -p
}

function func_mconfig()
{
###edit mongodb config file###
parastr=(  
"dbpath=/mongodb/data"  
"logpath=/mongodb/logs/mongo.log"  
"pidfilepath=/var/run/mongodb/mongodb.pid"  
"unixSocketPrefix=/mongodb/socket"  
"directoryperdb=true"  
"replSet=neunnm"
"bind_ip=0.0.0.0"
"logappend=true"  
"port = 27017"
"maxConns=20000"
"oplogSize=30720"
"fork=true"
"nohttpinterface=true"
"nojournal=true"  
)  
rm -f /etc/mongod.conf
if [ ! -f /etc/mongod.conf ]; then  
  size=${#parastr[@]};  
  for ((i=0;i<$size;i++))  
  do  
    eval tmp=\${parastr[i][@]}  
    echo $tmp >> /etc/mongod.conf
  done  
fi
}

function func_menu()
{
if [ ${MMENU} != "/mongodb" ]; then
sed -i 's@dbpath=/mongodb/data@dbpath='"${MMENU}"'/data@g' /etc/mongod.conf
sed -i 's@logpath=/mongodb/logs/mongo.log@logpath='"${MMENU}"'/logs/mongo.log@g' /etc/mongod.conf
sed -i 's@unixSocketPrefix=/mongodb/socket@unixSocketPrefix='"${MMENU}"'/socket@g' /etc/mongod.conf
fi
}

function func_replSet()
{
if [ -z ${MREPL} ]; then
    MREPL=neunnm
    echo "You want to use Mongdb RepliSet name as ${MREPL}."
else
    echo "You write the RepliSet name.It is ${MREPL}."
    sed -i 's@replSet=neunnm@replSet='"${MREPL}"'@g' /etc/mongod.conf
fi
}

###################################MAIN############################
echo -e "Please choose your mongodb release with the list : \n\t3.0\n\t3.1\n\t3.2\n\t3.3"
read -p "Please input your choice [default 3.0] : " MRELEASE
if [ -z ${MRELEASE} ]; then
     MRELEASE=3.0
     echo "You want to use Mongodb ${MRELEASE} release."
     func_yum
else    
     echo "You choose to use Mongodb ${MRELEASE} release."
     func_yum
fi
func_env
read -p "Please input your mongodb data menu [default /mongodb] : " MMENU
read -p "Please input your mongodb replSet name [default neunnm}] : " MREPL
if [ -z ${MMENU} ];then
    MMENU=/mongodb
fi
mkdir -pv ${MMENU}/{data,logs,socket}
func_mconfig
func_menu
func_replSet

################################STARTUP############################
rm -f /etc/init.d/mongod
read -p "Would U want to start your mongodb up ? [Y/N] : " SANSWER
case ${SANSWER} in
    Y|y)echo
        echo "Now startup your mongodb with #mongod -f /etc/mongod.conf"
        echo "If you want to off it,you can use it #pkill mongod"
        mongod -f /etc/mongod.conf
    ;;
    N|n)echo
        echo "You can start it whenever. #mongod -f /etc/mongod.conf"
    ;;
    *)echo
        echo "Your input must like Y|y|N|n."
    ;;
esac

[root@ntp-server mongodb]# cat replset_config.sh
#!/bin/bash

for i in `seq 1 2`
do
HOST=`cat hosts.conf | grep -v ^# | grep -v ^$ | sed -n ${i}p | awk '{print $1}'`
PORT=`cat hosts.conf | grep -v ^# | grep -v ^$ | sed -n ${i}p | awk '{print $2}'`

NUM=`expr ${i} - 1`
INTO="{_id: ${NUM}, host: '${HOST}:${PORT}'},"
if [ -z "${MOUT}" ]; then
    MOUT="config={_id: 'neunnm', members:["
fi
MOUT=${MOUT}${INTO}
done

HOST3=`cat hosts.conf | grep -v ^# | grep -v ^$ | sed -n 3p | awk '{print $1}'`
PORT3=`cat hosts.conf | grep -v ^# | grep -v ^$ | sed -n 3p | awk '{print $2}'`
INTO3="{_id: 2, host: '${HOST3}:${PORT3}', arbiterOnly:true}]}"
MCONFIG=${MOUT}${INTO3}

mongo --host=127.0.0.1:27017 --eval "$MCONFIG"
mongo --host=127.0.0.1:27017 --eval "$MCONFIG;rs.initiate(config)"
sleep 10s
mongo --host=127.0.0.1:27017 --eval "rs.status()"

[root@ntp-server mongodb]# cat hosts.conf
127.0.0.1 27017
127.0.0.1 28017
127.0.0.1 29017

neunnm:PRIMARY> rs.status()
{
    "set" : "neunnm",
    "date" : ISODate("2016-03-22T06:26:46.867Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "members" : [
        {
            "_id" : 0,
            "name" : "127.0.0.1:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 5328,
            "optime" : {
                "ts" : Timestamp(1458627964, 2),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2016-03-22T06:26:04Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1458627964, 1),
            "electionDate" : ISODate("2016-03-22T06:26:04Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "127.0.0.1:28017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 57,
            "optime" : {
                "ts" : Timestamp(1458627964, 2),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2016-03-22T06:26:04Z"),
            "lastHeartbeat" : ISODate("2016-03-22T06:26:46.198Z"),
            "lastHeartbeatRecv" : ISODate("2016-03-22T06:26:45.361Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "127.0.0.1:27017",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "127.0.0.1:29017",
            "health" : 1,
            "state" : 7,
            "stateStr" : "ARBITER",
            "uptime" : 57,
            "lastHeartbeat" : ISODate("2016-03-22T06:26:46.198Z"),
            "lastHeartbeatRecv" : ISODate("2016-03-22T06:26:45.314Z"),
            "pingMs" : NumberLong(0),
            "configVersion" : 1
        }
    ],
    "ok" : 1
}

上一篇:PHP设计模式之适配器模式


下一篇:java新手笔记32 jdk5新特性