【传智播客】MongoDB复制集
Last updated
Last updated
dbpath=shards/config-1-d
logpath=shards/config-1-log/mongod.log
fork=true
bind_ip_all=true
logappend=true
auth=false
port=8887
oplogSize=1024
journal=true
replSet=imooc ps -ef|grep mongo
root 5356 1 1 08:19 ? 00:00:01 bin/mongod -f shards/config-1.conf
root 5388 1 0 08:20 ? 00:00:00 bin/mongod -f shards/config-2.conf
root 5416 1 0 08:20 ? 00:00:00 bin/mongod -f shards/config-3.conf
root 5444 5019 0 08:21 pts/0 00:00:00 grep --color=auto mongo
[root@node1 mongodb-linux-x86_64-rhel70-3.6.3]#bin/mongo localhost:8887/admin
> db
admin> config = {
... "_id":"imooc",
... members:[{"_id":0,"host":"127.0.0.1:8887"}, {"_id":1,"host":"127.0.0.1:8888"},{"_id":2,"host":"127.0.0.1:8889"}]
... }> config.members[2]
{ "_id" : 2, "host" : "127.0.0.1:8889" }
> config.members[2] = {"_id":2,"host":"127.0.0.1:8889","arbiterOnly":true}
{ "_id" : 2, "host" : "127.0.0.1:8889", "arbiterOnly" : true }
>> config #重新观看config对象
{
"_id" : "imooc",
"members" : [
{
"_id" : 0,
"host" : "127.0.0.1:8887"
},
{
"_id" : 1,
"host" : "127.0.0.1:8888"
},
{
"_id" : 2,
"host" : "127.0.0.1:8889",
"arbiterOnly" : true
}
]
}
> rs.initiate(config) # 通过对象初始化复制集
{
"ok" : 1, # 1,证明成功
"operationTime" : Timestamp(1519652253, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1519652253, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
imooc:SECONDARY> # 这里也表明成功
imooc:PRIMARY> rs.status() #查看复制机的状态,rs.help()可以查看rs的所有函数#从节点
bin/mongo localhost:8888/test
MongoDB shell version v3.6.3
connecting to: mongodb://localhost:8888/test
MongoDB server version: 3.6.3
imooc:SECONDARY>
# 投票节点
[root@node1 mongodb-linux-x86_64-rhel70-3.6.3]# bin/mongo localhost:8889/test
MongoDB shell version v3.6.3
connecting to: mongodb://localhost:8889/test
MongoDB server version: 3.6.3
imooc:ARBITER>mooc:PRIMARY>
imooc:PRIMARY> db
admin
imooc:PRIMARY> use test
switched to db test
imooc:PRIMARY> db
test
imooc:PRIMARY> db.imooc.insert({"name":"imooc"})
WriteResult({ "nInserted" : 1 })
imooc:PRIMARY>imooc:SECONDARY> rs.slaveOk(true)
imooc:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
test 0.000GB
imooc:SECONDARY> db
test
imooc:SECONDARY> show collections
imooc
imooc:SECONDARY> db.imooc.find()
{ "_id" : ObjectId("5a94106ae4c4ddec5e2d389a"), "name" : "imooc" }
imooc:SECONDARY>