Title: MongoDB安装模式[原创]

Categories: linux

Description: 运维

Keywords: linux

Linux Mongodb

date:2017-11-11 author:张建新

查看硬盘分配情况

    [root@dscn022 mongodb]# lsblk
    NAME             MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sda                8:0    0 278.5G  0 disk
    ├─sda1             8:1    0   500M  0 part /boot
    └─sda2             8:2    0 224.9G  0 part
      ├─rhel-root    253:0    0    50G  0 lvm  /
      ├─rhel-swap    253:1    0     4G  0 lvm  [SWAP]
      ├─rhel-home    253:2    0 100.9G  0 lvm  /home
      ├─rhel-var     253:3    0    20G  0 lvm  /var
      └─rhel-var_log 253:4    0    50G  0 lvm  /var/log
    sdb                8:16   0   3.7T  0 disk
    └─sdb1             8:17   0   3.7T  0 part /home/hadmin/logs
    sdc                8:32   0   3.7T  0 disk
    └─sdc1             8:33   0   3.7T  0 part
    sdd                8:48   0   3.7T  0 disk
    └─sdd1             8:49   0   3.7T  0 part
    sde                8:64   0   3.7T  0 disk
    └─sde1             8:65   0   3.7T  0 part
    sdf                8:80   0   3.7T  0 disk
    └─sdf1             8:81   0   3.7T  0 part /home/hadmin/data/mongodb/prev
    sdg                8:96   0   3.7T  0 disk
    └─sdg1             8:97   0   3.7T  0 part /home/hadmin/data/mongodb/curr
    sdh                8:112  0   3.7T  0 disk
    └─sdh1             8:113  0   3.7T  0 part /home/hadmin/data/mongodb/next
    sdi                8:128  0   3.7T  0 disk
    └─sdi1             8:129  0   3.7T  0 part
    sdj                8:144  0   3.7T  0 disk
    └─sdj1             8:145  0   3.7T  0 part
    sdk                8:160  0   3.7T  0 disk
    └─sdk1             8:161  0   3.7T  0 part
    sdl                8:176  0   3.7T  0 disk
    └─sdl1             8:177  0   3.7T  0 part
    sdm                8:192  0   3.7T  0 disk
    └─sdm1             8:193  0   3.7T  0 part
    loop0              7:0    0   3.8G  0 loop /mnt

进行配置

| ------- | ------ | ------- |
| :------| ------: | :------: |
| curr | 1 | 2 | 3 |  4  | 5 |
| prev | 5 | 1 | 2 |  3  | 4 |
| next | 2 | 3 | 4 |  5  | 1 |

分配mongoDB 目录

    mkdir -p  /home/hadmin/data/mongodb/prev
    mkdir -p  /home/hadmin/data/mongodb/curr
    mkdir -p  /home/hadmin/data/mongodb/next

进行mount

 mount    /dev/sdf1      /home/hadmin/data/mongodb/prev
 mount    /dev/sdg1     /home/hadmin/data/mongodb/curr
 mount   /dev/sdh1     /home/hadmin/data/mongodb/next

查看硬盘大小

    [root@dscn022 mongodb]# df -h
    Filesystem                Size  Used Avail Use% Mounted on
    /dev/mapper/rhel-root      50G  5.1G   45G  11% /
    devtmpfs                  126G     0  126G   0% /dev
    tmpfs                     126G     0  126G   0% /dev/shm
    tmpfs                     126G   50M  126G   1% /run
    tmpfs                     126G     0  126G   0% /sys/fs/cgroup
    /dev/sda1                 494M  127M  368M  26% /boot
    /dev/mapper/rhel-var       20G  101M   20G   1% /var
    /dev/mapper/rhel-home     101G  1.5G  100G   2% /home
    /dev/mapper/rhel-var_log   50G   58M   50G   1% /var/log
    /dev/loop0                3.8G  3.8G     0 100% /mnt
    tmpfs                     126G   64K  126G   1% /tmp
    tmpfs                      26G     0   26G   0% /run/user/0
    /dev/sdb1                 3.6T   89M  3.4T   1% /home/hadmin/logs
    /dev/sdf1                 3.6T  390M  3.4T   1% /home/hadmin/data/mongodb/prev
    /dev/sdg1                 3.6T  390M  3.4T   1% /home/hadmin/data/mongodb/curr
    /dev/sdh1                 3.6T  390M  3.4T   1% /home/hadmin/data/mongodb/next

脚本 (傻瓜模式)

    #!/bin/bash

    # $1就是shard几
    # $2目录,重新规划磁盘后可以修改此处
    # $3是关闭位,启动不传,关闭传 --shutdown

    INDEX=$1

    PORT=$(( 27018 + $INDEX ))
    HOSTNAME=`cat /etc/hostname`
    SHARD="TOD-Shard-$INDEX"
    DB_PATH=" /home/hadmin/data/mongodb/"$2
    PID_FILE="/home/hadmin/data/pids/mongodb/$SHARD.pid"
    LOG_FILE="/home/hadmin/data/logs/mongodb/$SHARD.log"

    numactl --interleave=all /home/hadmin/mongodb/bin/mongod --logpath $LOG_FILE --logappend --logRotate rename --timeStampFormat ctime --fork --pidfilepath $PID_FILE --port $PORT --bind_ip $HOSTNAME --dbpath $DB_PATH --journalCommitInterval 100 --storageEngine wiredTiger --wiredTigerCacheSizeGB 4 --replSet $SHARD --enableMajorityReadConcern --shardsvr --oplogSize 10240 $3

cfgsvr.start

    #!/bin/bash

    numactl --interleave=all /home/hadmin/mongodb/bin/mongod -f /home/hadmin/mongodb/conf/configserver.conf

route

    #!/bin/bash

    /home/hadmin/mongodb/bin/mongos -f /home/hadmin/mongodb/conf/router.conf

shard_curr.start

    #!/bin/bash

    /home/hadmin/mongodb/sbin/common.sh 1 curr

shard_next.start

    #!/bin/bash

    /home/hadmin/mongodb/sbin/common.sh 2 next

shard_prev.start

    #!/bin/bash

    /home/hadmin/mongodb/sbin/common.sh 5 prev

initConfig.sh

    #!/bin/bash
    /home/hadmin/mongodb/bin/mongo dscn022:27018 <<EOF
    config={
        _id:"ConfigServer",
        configsvr:true,
        members:[
            {_id:0,host:"dscn022:27018",priority:2},
            {_id:1,host:"dscn023:27018"},
            {_id:2,host:"dscn024:27018"}],
        settings:{getLastErrorDefaults:{w:"majority",wtimeout:5000}}
    };
    rs.initiate(config);
    exit;
    EOF

initShard.sh

    #!/bin/bash
        /home/hadmin/mongodb/bin/mongo dscn022:27017 <<EOF
    sh.addShard("TOD-Shard-1/dscn022:27019,dscn023:27019,dscn026:27019");
    sh.addShard("TOD-Shard-2/dscn023:27020,dscn024:27020,dscn022:27020");
    sh.addShard("TOD-Shard-3/dscn024:27021,dscn025:27021,dscn023:27021");
    sh.addShard("TOD-Shard-4/dscn025:27022,dscn026:27022,dscn024:27022");
    sh.addShard("TOD-Shard-5/dscn025:27023,dscn026:27023,dscn022:27023");
    sh.setBalancerState(false);
    exit;
    EOF

initRS.sh

    #!/bin/bash
    ############
    # init rs1 #
    ############
    /home/hadmin/mongodb/bin/mongo dscn022:27019 <<EOF
    config={_id:"TOD-Shard-1",
        members:[
            {_id:0,host:"dscn022:27019",priority:2},
            {_id:1,host:"dscn026:27019"},
            {_id:2,host:"dscn023:27019"}],
        settings:{getLastErrorDefaults:{w:"majority",wtimeout:5000}}};
    rs.initiate(config);
    exit;
    EOF

    ############
    # init rs2 #
    ############
    /home/hadmin/mongodb/bin/mongo dscn022:27020 <<EOF
    config={_id:"TOD-Shard-2",
        members:[
            {_id:0,host:"dscn023:27020",priority:2},
            {_id:1,host:"dscn022:27020"},
            {_id:2,host:"dscn024:27020"}],
        settings:{getLastErrorDefaults:{w:"majority",wtimeout:5000}}};
    rs.initiate(config);
    exit;
    EOF

    ############
    # init rs3 #
    ############
    /home/hadmin/mongodb/bin/mongo dscn024:27021 <<EOF
    config={_id:"TOD-Shard-3",
        members:[
            {_id:0,host:"dscn024:27021",priority:2},
            {_id:1,host:"dscn023:27021"},
            {_id:2,host:"dscn025:27021"}],
        settings:{getLastErrorDefaults:{w:"majority",wtimeout:5000}}};
    rs.initiate(config);
    exit;
    EOF

    ############
    # init rs4 #
    ############
    /home/hadmin/mongodb/bin/mongo dscn025:27022 <<EOF
    config={_id:"TOD-Shard-4",
        members:[
            {_id:0,host:"dscn025:27022",priority:2},
            {_id:1,host:"dscn024:27022"},
            {_id:2,host:"dscn026:27022"}],
        settings:{getLastErrorDefaults:{w:"majority",wtimeout:5000}}};
    rs.initiate(config);
    exit;
    EOF

    ############
    # init rs5 #
    ############
    /home/hadmin/mongodb/bin/mongo dscn026:27023 <<EOF
    config={_id:"TOD-Shard-5",
        members:[
            {_id:0,host:"dscn026:27023",priority:2},
            {_id:1,host:"dscn025:27023"},
            {_id:2,host:"dscn022:27023"}],
        settings:{getLastErrorDefaults:{w:"majority",wtimeout:5000}}};
    rs.initiate(config);
    exit;
    EOF

总结(总体流程)

脚本执行流程

提醒

感谢兽兽提供的脚本

感谢民权协助