在阿里4g服务使用解压包方式mogodb测试。安装参考https://www.cnblogs.com/mnote/p/8979299.html

参考https://www.cnblogs.com/pfnie/articles/6759105.html 使用这个mongodb-linux-x86_64-rhel70-latest.tgz安装包安装。前面的包在虚拟机安装会缺少各种包。

[root@izuf6acp8dofkr757b6j7vz ~]# cd /usr/local/mongodb/mongodbserver/bin
[root@izuf6acp8dofkr757b6j7vz bin]# mongo admin
MongoDB shell version v4.1.6-190-g54c2a45
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
2021-02-04T10:27:07.562+0800 E QUERY    [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13
@(connect):1:6
2021-02-04T10:27:07.566+0800 F -        [main] exception: connect failed
2021-02-04T10:27:07.566+0800 E -        [main] exiting with code 1
[root@izuf6acp8dofkr757b6j7vz bin]# mongo -nodb
MongoDB shell version v4.1.6-190-g54c2a45
> replicaSet=new ReplSetTest({"nodes":3})
> replicaSet.startSet()
ReplSetTest starting set
ReplSetTest n is : 0
{"useHostName" : true,"oplogSize" : 40,"keyFile" : undefined,"port" : 20000,"replSet" : "__unknown_name__","dbpath" : "$set-$node","restart" : undefined,"pathOpts" : {"node" : 0,"set" : "__unknown_name__"},"setParameter" : {"writePeriodicNoops" : false,"numInitialSyncConnectAttempts" : 60}
}
ReplSetTest Starting....
Resetting db path '/data/db/__unknown_name__-0'
2021-02-04T10:33:48.496+0800 E QUERY    [js] Error: Caught std::exception of type boost::filesystem::filesystem_error: boost::filesystem::create_directory: No such file or directory: "/data/db/__unknown_name__-0" :
MongoRunner.runMongod@src/mongo/shell/servers.js:761:17
ReplSetTest/this.start@src/mongo/shell/replsettest.js:2296:20
ReplSetTest/this.startSet@src/mongo/shell/replsettest.js:540:24
@(shell):1:1

没有启动服务情况下,只能用mongo -nodb连接。启动副本集报错,参考https://www.qedev.com/bigdata/147259.html。

手动创建/data/db目录,然后重新操作启动成功。

再开一个shell,可以看到端口是2000到20002

[root@izuf6acp8dofkr757b6j7vz ~]# ps -ef|grep mongo
root     26481 17086  0 10:47 pts/0    00:00:00 mongo -nodb
root     26529 26481  1 10:47 pts/0    00:00:01 /usr/local/mongodb/mongodbserver/bin/mongod --oplogSize 40 --port 20000 --replSet __unknown_name__ --dbpath /data/db/__unknown_name__-0 --setParameter writePeriodicNoops=false --setParameter numInitialSyncConnectAttempts=60 --bind_ip 0.0.0.0 --setParameter enableTestCommands=1 --setParameter disableLogicalSessionCacheRefresh=true --setParameter orphanCleanupDelaySecs=1
root     26561 26481  1 10:47 pts/0    00:00:01 /usr/local/mongodb/mongodbserver/bin/mongod --oplogSize 40 --port 20001 --replSet __unknown_name__ --dbpath /data/db/__unknown_name__-1 --setParameter writePeriodicNoops=false --setParameter numInitialSyncConnectAttempts=60 --bind_ip 0.0.0.0 --setParameter enableTestCommands=1 --setParameter disableLogicalSessionCacheRefresh=true --setParameter orphanCleanupDelaySecs=1
root     26593 26481  1 10:47 pts/0    00:00:01 /usr/local/mongodb/mongodbserver/bin/mongod --oplogSize 40 --port 20002 --replSet __unknown_name__ --dbpath /data/db/__unknown_name__-2 --setParameter writePeriodicNoops=false --setParameter numInitialSyncConnectAttempts=60 --bind_ip 0.0.0.0 --setParameter enableTestCommands=1 --setParameter disableLogicalSessionCacheRefresh=true --setParameter orphanCleanupDelaySecs=1
root     27269 27205  0 10:48 pts/1    00:00:00 grep --color=auto mongo

验证各个服务器的状态

[root@izuf6acp8dofkr757b6j7vz ~]# mongo -nodb
MongoDB shell version v4.1.6-190-g54c2a45
> conn1=new Mongo("localhost:20000")
connection to localhost:20000
> primaryDB=conn1.getDB("test")
test
> primaryDB.isMaster()
{"ismaster" : false,"secondary" : false,"info" : "Does not have a valid replica set config","isreplicaset" : true,"maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"maxWriteBatchSize" : 100000,"localTime" : ISODate("2021-02-04T02:54:09.499Z"),"logicalSessionTimeoutMinutes" : 30,"connectionId" : 2,"minWireVersion" : 0,"maxWireVersion" : 8,"readOnly" : false,"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(0, 0),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(0, 0)
}

info提示没有配置,在第一个启动副本集的shell窗口回车后继续命令(还是在mongo的命令行内)

>replicaSet.initiate()

在第二shell窗体再查下状态

> primaryDB.isMaster()
{"hosts" : ["izuf6acp8dofkr757b6j7vz:20000","izuf6acp8dofkr757b6j7vz:20001","izuf6acp8dofkr757b6j7vz:20002"],"setName" : "__unknown_name__","setVersion" : 2,"ismaster" : true,"secondary" : false,"primary" : "izuf6acp8dofkr757b6j7vz:20000","me" : "izuf6acp8dofkr757b6j7vz:20000","electionId" : ObjectId("7fffffff0000000000000001"),"lastWrite" : {"opTime" : {"ts" : Timestamp(1612407771, 1),"t" : NumberLong(1)},"lastWriteDate" : ISODate("2021-02-04T03:02:51Z"),"majorityOpTime" : {"ts" : Timestamp(1612407771, 1),"t" : NumberLong(1)},"majorityWriteDate" : ISODate("2021-02-04T03:02:51Z")},"maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"maxWriteBatchSize" : 100000,"localTime" : ISODate("2021-02-04T03:06:08.509Z"),"logicalSessionTimeoutMinutes" : 30,"connectionId" : 2,"minWireVersion" : 0,"maxWireVersion" : 8,"readOnly" : false,"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1612407771, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1612407771, 1)
}

下面验证master和slave的查询。savle的默认不能查询,设置SalveOk后可以

[root@izuf6acp8dofkr757b6j7vz ~]# mongo -nodb
MongoDB shell version v4.1.6-190-g54c2a45
> conn1=new Mongo("localhost:20000")
connection to localhost:20000
> primaryDB=conn1.getDB("test")
test
> conn2=new Mongo("localhost:20001")
connection to localhost:20001
> primaryDB2=conn2.getDB("test")
test
> primaryDB.getCollectionInfos()
[ ]
> primaryDB2.getCollectionInfos()
2021-02-04T11:17:33.353+0800 E QUERY    [js] Error: listCollections failed: {"operationTime" : Timestamp(1612407771, 1),"ok" : 0,"errmsg" : "not master and slaveOk=false","code" : 13435,"codeName" : "NotMasterNoSlaveOk","$clusterTime" : {"clusterTime" : Timestamp(1612407771, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:872:1
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:921:20
@(shell):1:1
> conn2.setSlaveOk()
> primaryDB2.getCollectionInfos()
[ ]

写入数据后同步了

> for(i=0;i<1000;i++){primaryDB.mycoll.insert({count:i})}
WriteResult({ "nInserted" : 1 })
> primaryDB.mycoll.count()
1000
> primaryDB2.mycoll.count()
1000
> primaryDB2.mycoll.find()
{ "_id" : ObjectId("601b68ee6b3275be206d4775"), "count" : 0 }
{ "_id" : ObjectId("601b68ee6b3275be206d4778"), "count" : 3 }
{ "_id" : ObjectId("601b68ee6b3275be206d477d"), "count" : 8 }
{ "_id" : ObjectId("601b68ee6b3275be206d478c"), "count" : 23 }
{ "_id" : ObjectId("601b68ee6b3275be206d478d"), "count" : 24 }
{ "_id" : ObjectId("601b68ee6b3275be206d477c"), "count" : 7 }
{ "_id" : ObjectId("601b68ee6b3275be206d4783"), "count" : 14 }
{ "_id" : ObjectId("601b68ee6b3275be206d477a"), "count" : 5 }
{ "_id" : ObjectId("601b68ee6b3275be206d4786"), "count" : 17 }
{ "_id" : ObjectId("601b68ee6b3275be206d4779"), "count" : 4 }
{ "_id" : ObjectId("601b68ee6b3275be206d4784"), "count" : 15 }
{ "_id" : ObjectId("601b68ee6b3275be206d4787"), "count" : 18 }
{ "_id" : ObjectId("601b68ee6b3275be206d478e"), "count" : 25 }
{ "_id" : ObjectId("601b68ee6b3275be206d477e"), "count" : 9 }
{ "_id" : ObjectId("601b68ee6b3275be206d4780"), "count" : 11 }
{ "_id" : ObjectId("601b68ee6b3275be206d4790"), "count" : 27 }
{ "_id" : ObjectId("601b68ee6b3275be206d4776"), "count" : 1 }
{ "_id" : ObjectId("601b68ee6b3275be206d4782"), "count" : 13 }
{ "_id" : ObjectId("601b68ee6b3275be206d4788"), "count" : 19 }
{ "_id" : ObjectId("601b68ee6b3275be206d4777"), "count" : 2 }
Type "it" for more

从节点不能写入

> primaryDB2.mycoll.insert({count:1001})
WriteCommandError({"operationTime" : Timestamp(1612409071, 689),"ok" : 0,"errmsg" : "not master","code" : 10107,"codeName" : "NotMaster","$clusterTime" : {"clusterTime" : Timestamp(1612409071, 689),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
})
> primaryDB2.runCommand({"getLastError":1})
{"connectionId" : 24,"err" : "not master","code" : 10107,"codeName" : "NotMaster","n" : 0,"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1612409071, 689),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1612409071, 689)
}

模拟故障转移,虽然关闭有报错,但是进程确实关闭了

> primaryDB.adminCommand({"shutdown":1})
2021-02-04T11:31:37.222+0800 E QUERY    [js] Error: error doing query: failed: network error while attempting to run command 'shutdown' on host 'localhost:20000'  :
DB.prototype.runCommand@src/mongo/shell/db.js:168:1
DB.prototype.adminCommand@src/mongo/shell/db.js:186:16
@(shell):1:1
> primaryDB2.isMaster()
{"hosts" : ["izuf6acp8dofkr757b6j7vz:20000","izuf6acp8dofkr757b6j7vz:20001","izuf6acp8dofkr757b6j7vz:20002"],"setName" : "__unknown_name__","setVersion" : 2,"ismaster" : true,"secondary" : false,"primary" : "izuf6acp8dofkr757b6j7vz:20001","me" : "izuf6acp8dofkr757b6j7vz:20001","electionId" : ObjectId("7fffffff0000000000000002"),"lastWrite" : {"opTime" : {"ts" : Timestamp(1612409498, 1),"t" : NumberLong(2)},"lastWriteDate" : ISODate("2021-02-04T03:31:38Z"),"majorityOpTime" : {"ts" : Timestamp(1612409498, 1),"t" : NumberLong(2)},"majorityWriteDate" : ISODate("2021-02-04T03:31:38Z")},"maxBsonObjectSize" : 16777216,"maxMessageSizeBytes" : 48000000,"maxWriteBatchSize" : 100000,"localTime" : ISODate("2021-02-04T03:32:36.323Z"),"logicalSessionTimeoutMinutes" : 30,"connectionId" : 24,"minWireVersion" : 0,"maxWireVersion" : 8,"readOnly" : false,"ok" : 1,"$clusterTime" : {"clusterTime" : Timestamp(1612409498, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}},"operationTime" : Timestamp(1612409498, 1)
}
> primaryDB2.mycoll.insert({count:1001})
WriteResult({ "nInserted" : 1 })

在第一个窗口关闭副本集,多次回车后输入

>replicaSet.stopSet()

mongodb 副本集测试相关推荐

  1. MongoDB副本集搭建、连接php

    文章目录 PHP连接MongoDB MongoDB副本集介绍 MongoDB副本集搭建 MongoDB副本集测试 PHP连接MongoDB php连接MongoDB需要安装拓展包有两种,分别是mong ...

  2. Mongodb 副本集+分片集群搭建

    环境需求: 在三台机器上搭建副本集,在副本集上做分片 服务器IP: 192.168.1.232/192.168.1.238/192.168.1.241 安装Mongodb all nodes: tar ...

  3. MongoDB副本集同步原理

    MongoDB的同步原理,官方文档介绍的比较少,网上资料也不是太多,下面是结合官方文档.网上资料和测试时候的日志,整理出来的一点东西. 因为MongoDB的每个分片也是副本集,所以只需要搞副本集的同步 ...

  4. MongoDB副本集权限重置

    背景:某公司线上的一套MongoDB副本集,只知道一个应用库以及该库下对应的一个用户.除此以外,对这套集群一无所知,他们的需求是要拥有超级用户权限,方便以后管理. 我虽是一个小渣渣,但以前单位一个很照 ...

  5. MongoDB副本集学习(一):概述和环境搭建

    MongoDB副本集概述 以下图片摘自MongoDB官方文档:http://docs.mongodb.org/manual/core/replication-introduction/ Primary ...

  6. mongodb副本集+分片集群部署 step by step

    mongodb副本集+分片集群部署step by step 本文只讲述mongodb副本集+分片集群的部署,关于mongdb shading & replica set原理优点等不在本文讨论范 ...

  7. MongoDB副本集(Replica Sets)搭建教程

    MongoDB副本集(Replica Sets)搭建教程 最近在生产环境下部署公司项目.由于项目中有使用到MongoDB,所以参考网上的博客,在服务器上进行搭建MongoDB副本集.今天打算将其记录成 ...

  8. MongoDB副本集详解与搭建

    1. 简介 MongoDB中的副本集(Replica Set)是一组维护相同数据集的mongod服务. 副本集可提供冗余和高可用性,是所有生产部署的基础.也可以说,副本集类似于有自动故障恢复功能的主从 ...

  9. 数据库系列之MongoDB副本集集群环境

    MongoDB集群环境有3种模式:主从复制.副本集和分片,本文详细介绍副本集模式的部署和配置,并进行测试验证. 数据库系列之MongoDB主从复制集群环境 数据库系列之MongoDB副本集集群环境 数 ...

最新文章

  1. 基于Spark MLlib平台的协同过滤算法---电影推荐系统
  2. 防抖函数和节流函数的实现,这个是在某保险公司笔试题遇到的。
  3. Docker 安装 MongoDB
  4. 软件测试 学习之路 CSS(一)
  5. iPhone上传文件到ftp服务器,将文件上传到iPhone上的FTP服务器
  6. 非法控制计算机信息系统罪的标准,非法获取计算机信息系统数据、非法控制计算机信息系统罪立案标准...
  7. JS 继承各种方法的优劣比较 ----JS 学习笔记(五)
  8. CloudComputing是什么
  9. HDU 2460 Network(双连通+树链剖分+线段树)
  10. Spring WebFlux – SpringReact式编程
  11. 容器技术Docker K8s 25 容器服务ACK基础与进阶-监控管理
  12. 150.1 go语言开发实战慕课版
  13. 关于html转换为pdf案例的一些测试与思考
  14. NPN与PNP的区别
  15. 提高模型准确率:组合模型
  16. 计算机excel怎么删除重复项,excel怎么删除重复项
  17. 金融级湖仓一体架构——SequoiaDB巨杉数据库初探
  18. ‘真三国无双5’完美存档修改
  19. manjaro折腾手记
  20. ChromeOptions--禁止加载图片

热门文章

  1. guest additions
  2. activiti学习资料--initiator的作用及其使用
  3. 单边指数信号的特点_方波和单边指数信号的卷积.doc
  4. sim卡没坏但苹果手机无服务_苹果6sp无服务信号不好解决办法
  5. PCB线路板的制作工艺流程分享!2021-08-21
  6. C++ primer(第五版)简单读书笔记
  7. Android studio创建第一个项目并运行
  8. 人工智能时代是什么时代?
  9. iOS app中不能跳转到商店更新
  10. 【MicroPython ESP32】手动配网和wifi信息保存示例