分享一则用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. easyui dialog iframe

    function toGrant(obj,url,showMsg) {                                        var dialog=$('#dlg_grant' ...

  2. 以小时候玩的贪吃蛇为例,对于Java图像界面的学习感悟

    简介 正文 01.JFrame是啥? 02.JPanel 03. KeyListener 04.Runnable 05.游戏Running 06.游戏初始类编写 07.main 简介: 一直以来用代码 ...

  3. Swift 语法

    三目运算        let p=10         let x:Int? =  12         let  m:Optional = 11         print(x!+p+m!)   ...

  4. CentOS 7.2.1511编译安装Nginx1.10.1+MySQL5.6.33+PHP5.6.26

    准备篇 一.防火墙配置 CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.se ...

  5. JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度

    alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height()); //浏览器时下窗口文档的高度 alert($(docum ...

  6. FMDB

    一.FMDB简介 1.FMDB简介 iOS中原生的SQLite API在进行数据存储的时候,需要使用C语言中的函数,操作比较繁琐.于是,就出现了一系列将SQLite API进行封装的库,例如FMDB. ...

  7. marquee 标签 文字滚动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. SQL检查锁&解锁

    use master go --检索死锁进程 select spid, blocked, loginame, last_batch, status, cmd, hostname, program_na ...

  9. Node聊天程序实例06:server.js

    作者:vousiu 出处:http://www.cnblogs.com/vousiu 本实例参考自Mike Cantelon等人的<Node.js in Action>一书. server ...

  10. RDLC 子报表

    1.RDLC 设计页面,拖入table或者矩形 2.右击表格或者矩形单元格,插入--子报表 3.输入名称和将此报表用作子报表 名称:显示在设计页面上的,仅作观看作用 将此报表用作子报表:填写目录下的需 ...