美文网首页我爱编程
构建大数据平台(九)mongoDB集群搭建

构建大数据平台(九)mongoDB集群搭建

作者: Mr_ZhaiDK | 来源:发表于2018-03-01 14:58 被阅读0次

一、前提:

(1)mongoDB使用稳定版本:mongodb-linux-x86_64-rhel70-3.6.3.tgz;

(2)可在master上安装后,scp到其他slave机器上。

二、配置mongoDB:

首先要注意在生产环境中目录结构要定义好,防止在项目过多的时候找不到所需的项目

  1. 目录统一放在/home下面 ,首先创建mongodb项目目录
#创建项目目录
cd /home
mkdir mongodb

#进入 mongodb目录中新建三个目录conf、logs 、db 
cd /home/mongodb
#conf存储配置文件目录,logs用来存储日志目录,db用来存储数据目录
mkdir conf logs db
  1. 将 mongodb-linux-x86_64-rhel70-3.6.3.tgz 放在 /home/mongodb下,并解压缩,解压缩完成后可删除安装包:
#解压缩
tar -zxvf mongodb-linux-x86_64-rhel70-3.6.3.tgz

#完成后可删除
rm -f mongodb-linux-x86_64-rhel70-3.6.3.tgz
  1. 修改配置文件

(1)进入conf目录新建mongodb的配置文件mongodb.conf

 cd /home/mongodb/conf/
 touch mongodb.conf

(2)编写配置文件mongodb.conf,内容如下 :

其中dbpath是数据库文件目录,logpath是日志目录,port是mongodb所占用的端口,fork是true的时候表示在后台启动

fork=true
port=27017
bind_ip=0.0.0.0
dbpath=/home/mongodb/db
logpath=/home/mongodb/logs/mongodb.log
logappend=true
replSet=mongodbBak
directoryperdb=true
journal=true

(3)将安装配置好的mongodb目录复制到所有slave节点中:

scp -r /home/mongodb/ root@slave1:/home/

scp -r /home/mongodb/ root@slave2:/home/

scp -r /home/mongodb/ root@slave3:/home/

三、启动mongodb:

1)所有节点启动mongodb

cd /home/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/
./mongod -f /home/mongodb/conf/mongodb.conf

2)单机mongodb,开机自启动

(1)添加开机启动服务:

vi /lib/systemd/system/mongodb.service

粘贴一下内容

[Unit]
Description=The Mongodb Process Manager
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/home/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/mongod -f /home/mongodb/conf/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/home/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/mongod --shutdown --config /home/mongodb/conf/mongodb.conf
PrivateTmp=true

[Install]
WantedBy=multi-user.target

(2) 设置开机启动:

systemctl daemon-reload 
systemctl start mongodb.service 
systemctl enable mongodb.service

(3) 检查是否安装成功:

ps -ef|grep mongodb

四、初始化mongodb集群:

  1. 任意一台mongodb登陆:
cd /home/mongodb/mongodb-linux-x86_64-rhel70-3.6.3/bin/
./mongo 192.168.2.158
  1. 在连接mongdo后,输入一下内容:
#使用admin库
use admin
#输入config 内容
config = {"_id" : "mongodbBak","members" : [{"_id" : 0, "host" : "192.168.2.158:27017"},{"_id" : 1, "host" : "192.168.2.159:27017"},{"_id" : 2, "host" : "192.168.2.177:27017"},{"_id" : 3, "host" : "192.168.2.178:27017"}]}
  1. 然后回车,执行初始化副本集配置命令:
rs.initiate(config);
#输出
{
    "ok" : 1,
    "operationTime" : Timestamp(1519887335, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1519887335, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}
  1. 查看集群节点的状态
rs.status();
#输出
{
    "set" : "mongodbBak",
    "date" : ISODate("2018-03-01T06:56:58.886Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "heartbeatIntervalMillis" : NumberLong(2000),
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(1519887417, 1),
            "t" : NumberLong(1)
        },
        "readConcernMajorityOpTime" : {
            "ts" : Timestamp(1519887417, 1),
            "t" : NumberLong(1)
        },
        "appliedOpTime" : {
            "ts" : Timestamp(1519887417, 1),
            "t" : NumberLong(1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1519887417, 1),
            "t" : NumberLong(1)
        }
    },
    "members" : [
        {
            "_id" : 0,
            "name" : "192.168.2.158:27017",
            "health" : 1,
            "state" : 1,
            "stateStr" : "PRIMARY",
            "uptime" : 724,
            "optime" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-03-01T06:56:57Z"),
            "infoMessage" : "could not find member to sync from",
            "electionTime" : Timestamp(1519887346, 1),
            "electionDate" : ISODate("2018-03-01T06:55:46Z"),
            "configVersion" : 1,
            "self" : true
        },
        {
            "_id" : 1,
            "name" : "192.168.2.159:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 83,
            "optime" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-03-01T06:56:57Z"),
            "optimeDurableDate" : ISODate("2018-03-01T06:56:57Z"),
            "lastHeartbeat" : ISODate("2018-03-01T06:56:58.601Z"),
            "lastHeartbeatRecv" : ISODate("2018-03-01T06:56:57.724Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "192.168.2.158:27017",
            "configVersion" : 1
        },
        {
            "_id" : 2,
            "name" : "192.168.2.177:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 83,
            "optime" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-03-01T06:56:57Z"),
            "optimeDurableDate" : ISODate("2018-03-01T06:56:57Z"),
            "lastHeartbeat" : ISODate("2018-03-01T06:56:58.602Z"),
            "lastHeartbeatRecv" : ISODate("2018-03-01T06:56:57.938Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "192.168.2.158:27017",
            "configVersion" : 1
        },
        {
            "_id" : 3,
            "name" : "192.168.2.178:27017",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 83,
            "optime" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1519887417, 1),
                "t" : NumberLong(1)
            },
            "optimeDate" : ISODate("2018-03-01T06:56:57Z"),
            "optimeDurableDate" : ISODate("2018-03-01T06:56:57Z"),
            "lastHeartbeat" : ISODate("2018-03-01T06:56:58.602Z"),
            "lastHeartbeatRecv" : ISODate("2018-03-01T06:56:57.320Z"),
            "pingMs" : NumberLong(0),
            "syncingTo" : "192.168.2.158:27017",
            "configVersion" : 1
        }
    ],
    "ok" : 1,
    "operationTime" : Timestamp(1519887417, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1519887417, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

至此,mongodb集群搭建完毕!

相关文章

网友评论

    本文标题:构建大数据平台(九)mongoDB集群搭建

    本文链接:https://www.haomeiwen.com/subject/dlpgxftx.html