MongoDB三节点高可用模式安装

时间:2022-10-20 01:23:06
设备:   三个1G、20G、1核的虚拟机,系统是SentOS7 min 清除原始自数据目录: rm -fr /home/mongosingle/ 创建目录: mkdir -p /home/mongosingle/data/replset   启动服务: Server1: /usr/local/mongodb/bin/mongod --shardsvr --replSet replyset --port 27020 --dbpath /home/mongosingle/data/replset  --logpath /home/mongosingle/data/replset.log --logappend --fork --nojournal --oplogSize 7000 Server2: /usr/local/mongodb/bin/mongod --shardsvr --replSet replyset --port 27020 --dbpath /home/mongosingle/data/replset  --logpath /home/mongosingle/data/replset.log --logappend --fork --nojournal  --oplogSize 7000 Server3: /usr/local/mongodb/bin/mongod --shardsvr --replSet replyset --port 27020 --dbpath /home/mongosingle/data/replset  --logpath /home/mongosingle/data/replset.log --logappend --fork --nojournal --oplogSize 7000   在任意节点链接数据库: /usr/local/mongodb/bin/mongo 127.0.0.1:27020   初始化配置(建立关联关系): config = { _id:"replyset", members:[                      {_id:0,host:"192.168.229.129:27020 "},                      {_id:1,host:"192.168.229.130:27020 "},                      {_id:2,host:"192.168.229.131:27020 "}                 ]          } rs.initiate(config);   存储数据 for(var i=0;i<100000;i++){db.test8.save({"id" : i , "name" : "name" + i , "value" : "value" + i}); print(i);} for (var i = 0; i < 1000000; i++) { var start = (new Date()).getTime(); var collName = "test" + i; var doc = {name: "name" +i, seq: i}; db.createCollection(collName); db[collName].createIndex({name: 1}); db[collName].insert(doc); var end = (new Date()).getTime(); print("cost: " + (end - start)); } 从节点可读: 在root目录下的.mongodb.js文件中添加re.slaveOk();