环境准备
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.confsystemLog: destination: file logAppend: true path: /opt/mongodb/rs0-0/logs/mongodb.log storage: dbPath: /opt/mongodb/rs0-0/data journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: /opt/mongodb/rs0-0/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfonet: 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.confsystemLog: destination: file logAppend: true path: /opt/mongodb/rs0-1/logs/mongodb.log storage: dbPath: /opt/mongodb/rs0-1/data journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: /opt/mongodb/rs0-1/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfonet: 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.confsystemLog: destination: file logAppend: true path: /opt/mongodb/rs0-2/logs/mongodb.log storage: dbPath: /opt/mongodb/rs0-2/data journal: enabled: trueprocessManagement: fork: true # fork and run in background pidFilePath: /opt/mongodb/rs0-2/mongod.pid # location of pidfile timeZoneInfo: /usr/share/zoneinfonet: 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 --bind_ip localhost,192.168.1.11 --dbpath /opt/mongodb/rs0-0 --smallfiles --oplogSize 128
实例1:
mongod --replSet rs0 --port 27018 --bind_ip localhost,192.168.1.11 --dbpath /opt/mongodb/rs0-1 --smallfiles --oplogSize 128
实例2:
mongod --replSet rs0 --port 27019 --bind_ip localhost,192.168.1.11 --dbpath /opt/mongodb/rs0-2 --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
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦