MongoDB 4.0 开发环境搭建集群

环境准备

Liunx 服务器一台

以下示例为单机版安装集群, 没有分片

MongoDB 安装

1.下载 MongoDB tgz 安装包:

可以从下载中心下载:

https://www.mongodb.com/download-center#production

Shell:

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon2-4.0.1.tgz

2.解压缩下载的压缩文件

使用下面的命令解压缩刚才下载的文件

tar -zxvf mongodb-linux-*-4.0.1.tgz

3.添加路径到环境变量

打开 /etc/profile ,然后添加下面的脚本进去:

export PATH=<mongodb-install-directory>/bin:$PATH

为你的程序安装目录,然后刷新环境变量:

source /etc/profile

集群搭建

1.为每个实例创建目录

创建文件夹:

mkdir -p /opt/mongodb/rs0-0 /opt/mongodb/rs0-1 /opt/mongodb/rs0-2

2.创建配置文件

切换到 rs0-0 目录中,创建配置文件touch mongod.conf,内容为:

#mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb/rs0-0/logs/mongodb.log 

storage:
  dbPath: /opt/mongodb/rs0-0/data
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongodb/rs0-0/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017 
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: rs0

切换到 rs0-1 目录中,创建配置文件touch mongod.conf,内容为:

#mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb/rs0-1/logs/mongodb.log 

storage:
  dbPath: /opt/mongodb/rs0-1/data
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongodb/rs0-1/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27018 
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: rs0

切换到 rs0-2 目录中,创建配置文件touch mongod.conf,内容为:

#mongod.conf

systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb/rs0-2/logs/mongodb.log 

storage:
  dbPath: /opt/mongodb/rs0-2/data
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /opt/mongodb/rs0-2/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27019
  bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

replication:
  replSetName: rs0

3.启动实例

实例0:

mongod --replSet rs0 --port 27017  --smallfiles --oplogSize 128

实例1:

mongod --replSet rs0 --port 27018   --smallfiles --oplogSize 128

实例2:

mongod --replSet rs0 --port 27019  --smallfiles --oplogSize 128

4.配置集群

启动起来之后,使用mongo客户端命令切换到其中一个实例上

mongo --port 27017

然后在 Mongo shell中输入:

rsconf = {
  _id: "rs0",
  members: [
    {
     _id: 0,
     host: "<hostname>:27017"
    },
    {
     _id: 1,
     host: "<hostname>:27018"
    },
    {
     _id: 2,
     host: "<hostname>:27019"
    }
   ]
}

替换 为你的主机名或者ip地址,然后执行:

rs.initiate( rsconf )

输入 rs.conf() 来查看你的集群信息:

{
    "_id": "rs0",
    "version": 1,
    "protocolVersion": NumberLong(1),
    "writeConcernMajorityJournalDefault": true,
    "members": [
        {
            "_id": 0,
            "host": "<hostname>:27017",
            "arbiterOnly": false,
            "buildIndexes": true,
            "hidden": false,
            "priority": 1,
            "tags": {},
            "slaveDelay": NumberLong(0),
            "votes": 1
        },
        {
            "_id": 1,
            "host": "<hostname>:27018",
            "arbiterOnly": false,
            "buildIndexes": true,
            "hidden": false,
            "priority": 1,
            "tags": {},
            "slaveDelay": NumberLong(0),
            "votes": 1
        },
        {
            "_id": 2,
            "host": "<hostname>:27019",
            "arbiterOnly": false,
            "buildIndexes": true,
            "hidden": false,
            "priority": 1,
            "tags": {},
            "slaveDelay": NumberLong(0),
            "votes": 1
        }
    ],
    "settings": {
        "chainingAllowed": true,
        "heartbeatIntervalMillis": 2000,
        "heartbeatTimeoutSecs": 10,
        "electionTimeoutMillis": 10000,
        "catchUpTimeoutMillis": -1,
        "catchUpTakeoverDelayMillis": 30000,
        "getLastErrorModes": {},
        "getLastErrorDefaults": {
            "w": 1,
            "wtimeout": 0
        },
        "replicaSetId": ObjectId("5b7412b72362045708008077")
    }
}

本文地址:https://www.cnblogs.com/savorboard/p/mongodb-4-cluster-install.html
作者博客:Savorboard
本文原创授权为:署名 - 非商业性使用 - 禁止演绎,协议普通文本 | 协议法律文本

posted @ 2018-08-16 18:44  Savorboard  阅读(5855)  评论(2编辑  收藏  举报