分享一则用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
}

centos mongodb cluster install 完全版的更多相关文章

  1. centos在yum install报错:Another app is currently holding the yum lock解决方法

    centos在yum install报错:Another app is currently holding the yum lock,这个问题可能是很多的新手经常遇到问题,之前也有人问我,包括本人在刚 ...

  2. Centos 64位 Install certificate on apache 即走https协议

    Centos 64位 Install certificate on apache 即走https协议 一: 先要apache 请求ssl证书的csr 一下是步骤: 重要注意事项 An Importan ...

  3. CentOS 7.1 中文正式版下载 - 最流行的免费开源企业级 Linux 服务器操作系统

    如果说 Ubuntu 是现今最受桌面用户欢迎的 Linux 操作系统,那么 CentOS 就是最受公司.企业.IDC 喜爱的 Linux 发行版了.得益于极为出色的稳定性,全球范围内无数著名网站均选用 ...

  4. linux(centos )mongodb install

    file down :http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.4.9.tgz /usr/local/ tar zxvf mongo ...

  5. CentOS MongoDB 高可用实战

    原文:https://www.sunjianhua.cn/archives/centos-mongodb.html 一.MongoDB 单节点 1.1.Windows 版安装 1.1.1 获取社区版本 ...

  6. CentOS搭建OpenVPN服务(简易版)

    OpenVPN服务端配置 1. 安装OpenVPN软件包 默认的Centos软件源里面没有OpenVPN的软件包,我们可以添加rpmforge的repo,从而实现yum安装openvpn 针对Cent ...

  7. centos 7 部署 汉化版 gitlab

    =============================================== 2017/11/12_第6次修改                       ccb_warlock 更 ...

  8. Centos 7 minimal install 无网络无ifconfig的解决

    Centos7这个比较不厚道, minimal install下居然不带net-tools 先要连上网络 修改/etc/sysconfig/network-scripts/ifcfg-ens12312 ...

  9. CentOS 7 MySql 解压版安装配置

    下载 访问www.mysql.com 点击DOWNLOADS-->Community-->MySQL Community Server 选择要下载的版本,目前可选择的有:5.5.5.6.5 ...

随机推荐

  1. js 判断移动设备、pc端、android、iPhone、是否为微信、微博、qq空间

    varbrowser = {   versions: function () {      var u = navigator.userAgent, app = navigator.appVersio ...

  2. [课程设计]任务进度条&开发日志目录

    任务进度条&开发日志目录 周期 时间 任务 Sprint One   11.14     ●  Scrum团队分工及明确任务1.0    Sprint One   11.15   ●  Scr ...

  3. java中filter的用法 内部资料 请勿转载 谢谢合作

    filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 Java代码   import javax.servlet ...

  4. iOS开发,URL编码和解码

    URL传递数据中,如果含有中文,需要进行编码: NSString *urlEncodeString = [urlStr stringByAddingPercentEncodingWithAllowed ...

  5. tomcat 增加压缩设置

    conf/server.xml中,Connector的配置中添加 compression="on" compressionMinSize="512" noCom ...

  6. jQuery事件总结

    blur() 触发或绑定blur事件.$("input").blur(function(){ $("input").css("background-c ...

  7. CentOS系统启动流程

    CentOS系统启动流程 POST --> Boot Sequence(BIOS) --> Boot Loader(MBR) --> kernel(ramdisk) --> r ...

  8. 7.dotnet core 如何发邮件

    需要用到的Nuget包 "MailKit": "1.8.1", 方法 /// <summary> /// 发送邮件(支持Html发送,支持添加一个附 ...

  9. linux内核追踪——find_next_bit函数详详详解

    写在前面 宗旨:把话说清楚,把道理讲透彻. 约定:所有代码均来自Linux内核2.6.24版. 建议:本文介绍得十分详细,但也略显繁琐,读者可以先看“Ⅴ.总结”部分带注释的源码,如果哪里不清楚,再回头 ...

  10. Cheatsheet: 2016 04.01 ~ 04.30

    .NET String format Setting up Ubuntu for .NET Development ASP.​NET Core and Angular2 - Part 1 - Upda ...