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
本文原創授權為:署名 - 非商業性使用 - 禁止演繹,協議普通文本 | 協議法律文本


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM