在阿里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()