Liunx服务器一台
以下示例为单机版安装集群,没有分片
可以从下载中心下载:
https://www.mongodb.com/download-center#production
Shell:
wgethttps://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon2-4.0.1.tgz 2.解压缩下载的压缩文件使用下面的命令解压缩刚才下载的文件
tar-zxvfmongodb-linux-*-4.0.1.tgz 3.添加路径到环境变量打开/etc/profile,然后添加下面的脚本进去:
exportPATH=<mongodb-install-directory>/bin:$PATH为你的程序安装目录,然后刷新环境变量:
source/etc/profile创建文件夹:
mkdir-p/opt/mongodb/rs0-0/opt/mongodb/rs0-1/opt/mongodb/rs0-2
2.创建配置文件切换到rs0-0目录中,创建配置文件touchmongod.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#forkandruninbackground pidFilePath:/opt/mongodb/rs0-0/mongod.pid#locationofpidfile timeZoneInfo:/usr/share/zoneinfo net: port:27017 bindIp:0.0.0.0#Listentolocalinterfaceonly,commenttolistenonallinterfaces. replication: replSetName:rs0切换到rs0-1目录中,创建配置文件touchmongod.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#forkandruninbackground pidFilePath:/opt/mongodb/rs0-1/mongod.pid#locationofpidfile timeZoneInfo:/usr/share/zoneinfo net: port:27018 bindIp:0.0.0.0#Listentolocalinterfaceonly,commenttolistenonallinterfaces. replication: replSetName:rs0切换到rs0-2目录中,创建配置文件touchmongod.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#forkandruninbackground pidFilePath:/opt/mongodb/rs0-2/mongod.pid#locationofpidfile timeZoneInfo:/usr/share/zoneinfo net: port:27019 bindIp:0.0.0.0#Listentolocalinterfaceonly,commenttolistenonallinterfaces. replication: replSetName:rs0 3.启动实例实例0:
mongod--replSetrs0--port27017--smallfiles--oplogSize128实例1:
mongod--replSetrs0--port27018--smallfiles--oplogSize128实例2:
mongod--replSetrs0--port27019--smallfiles--oplogSize128 4.配置集群启动起来之后,使用mongo客户端命令切换到其中一个实例上
mongo--port27017然后在Mongoshell中输入:
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
本文原创授权为:署名-非商业性使用-禁止演绎,协议普通文本|协议法律文本本文内容总结:环境准备,MongoDB安装,集群搭建,
原文链接:https://www.cnblogs.com/savorboard/p/mongodb-4-cluster-install.html