MongoDB 副本集 pymongo使用
Posted ExplorerMan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB 副本集 pymongo使用相关的知识,希望对你有一定的参考价值。
搭建没有仲裁节点的副本集,推荐使用
2.清空node2的db文件夹 和 log 文件夹
rm -rf /var/lib/mongod/*
rm -rf /var/log/mongod/*
3.修改node2和port、replSet
vim /etc/mongod.conf
replSet = arg/192.168.1.161:27017,192.168.1.162:27017
复制mongod.conf到node2
scp /etc/mogod.conf node2:/etc/
4.启动三个节点,分别进入 对应的node文件夹中:
systemctl start mongod
5.连接node1节点(27017):
mongo --host 192.168.1.161 --port 27017
6.执行初始化命令:
> config={"_id":"arg",members:[{"_id":1,"host":"192.168.1.161:27017"},{"_id":2,"host":"192.168.1.162:27017"}]}
{
"_id" : "arg",
"members" : [
{
"_id" : 1,
"host" : "192.168.1.161:27017"
},
{
"_id" : 2,
"host" : "192.168.1.162:27017"
}
]
}
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
7.查看状态,发现有1个主节点,1个从节点。
> rs.status()
{
"set" : "arg",
"date" : ISODate("2018-09-06T06:50:16Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.1.161:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1306,
"optime" : Timestamp(1536216613, 2),
"optimeDate" : ISODate("2018-09-06T06:50:13Z"),
"electionTime" : Timestamp(1536216578, 1),
"electionDate" : ISODate("2018-09-06T06:49:38Z"),
"self" : true
},
{
"_id" : 2,
"name" : "192.168.1.162:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 50,
"optime" : Timestamp(1536216613, 2),
"optimeDate" : ISODate("2018-09-06T06:50:13Z"),
"lastHeartbeat" : ISODate("2018-09-06T06:50:16Z"),
"lastHeartbeatRecv" : ISODate("2018-09-06T06:50:15Z"),
"pingMs" : 0,
"syncingTo" : "192.168.1.161:27017"
}
],
"ok" : 1
}
使用MongoDB 副本集,第1个参数是主host,第2个是replSet名,第3个是模式
client = MongoReplicaSetClient(eval(mongodb_set), replicaSet=mongodb_replicaSet, read_preference=eval(mongodb_read_preference))
下面getClient函数的conf
{
"mongodb_host" : "192.168.1.161",
"mongodb_set" : "192.168.1.161:27017",
"mongodb_replicaSet" : "arg",
"mongodb_read_preference" : "ReadPreference.SECONDARY_PREFERRED",
"mongodb_port" : 27017,
"mongodb_user":"ceph",
"mongodb_passwd":"monpassword",
"is_mongo_authenticate" : 0,
"is_mongo_replicat" : 1,
}
pymongo连接mongodb
def getClient(conf, dbname):
‘‘‘
conf : json conf objet
conf=load_conf(configfile)
db = getClient(conf)[‘ceph‘]
collection =db[‘cluster‘]
cursor=collection.find_one()
Return a connexion to database specified in conf file
take care with authentication
‘‘‘
mongodb_host = conf.get("mongodb_host", "127.0.0.1")
mongodb_port = conf.get("mongodb_port", "27017")
mongodb_URL = "mongodb://"+mongodb_host+":"+str(mongodb_port)
#mongodb replication
is_mongo_replicat = conf.get("is_mongo_replicat", 0)
mongodb_set = "‘"+conf.get("mongodb_set","")+"‘"
mongodb_replicaSet =conf.get("mongodb_replicaSet",None)
mongodb_read_preference = conf.get("mongodb_read_preference",None)
if is_mongo_replicat == 1:
# 使用MongoDB 副本集,第1个参数是主host,第2个是replSet名,第3个是模式
client = MongoReplicaSetClient(eval(mongodb_set), replicaSet=mongodb_replicaSet, read_preference=eval(mongodb_read_preference))
else:
#if not replicated
client = MongoClient(mongodb_URL)
# mongo db authentication
is_mongo_authenticate = conf.get("is_mongo_authenticate", 0)
mongodb_user = conf.get("mongodb_user", "ceph")
mongodb_passwd = conf.get("mongodb_passwd", "empty")
if is_mongo_authenticate == 1:
client[dbname].authenticate(mongodb_user,mongodb_passwd)
return client[dbname]
8.kill 掉node6,即端口 27021
9.然后连接到node7上:mongo --host 192.168.80.128 --port 27022,查看状态,发现node7为从节点,node8为主节点了,原来的node6 不可达、不健康的。
>XbqSet:SECONDARY> rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-27T22:08:24.894Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 0,
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : Timestamp(0, 0),
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:08:23.748Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:07:43.464Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "Failed attempt to connect to 192.168.80.128:27021; couldn‘t connect to server 192.168.80.128:27021 (192.168.80.128), connection attempt failed",
"configVersion" : -1
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 833,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 431,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:08:23.409Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:08:23.521Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1501193266, 1),
"electionDate" : ISODate("2017-07-27T22:07:46Z"),
"configVersion" : 1
}
],
"ok" : 1
}
10.重新启动node6:mongod --config mongodb.conf
11.继续连接node7:mongo --host 192.168.80.128 --port 27022,查看状态,原来的node6 变为了 从节点。
rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-27T22:12:41.275Z"),
"myState" : 2,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 54,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:12:40.382Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:12:41.213Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1090,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 688,
"optime" : Timestamp(1501192872, 1),
"optimeDate" : ISODate("2017-07-27T22:01:12Z"),
"lastHeartbeat" : ISODate("2017-07-27T22:12:39.673Z"),
"lastHeartbeatRecv" : ISODate("2017-07-27T22:12:39.820Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1501193266, 1),
"electionDate" : ISODate("2017-07-27T22:07:46Z"),
"configVersion" : 1
}
],
"ok" : 1
}```
五、增加、删除节点
1.cp -r node8 node10,修改node10的datapath、logpath的port,并将port 改为 27024,启动 node10。
2.进入到主节点中,mongo --host 192.168.80.128 --port 27023,一定要在主节点中进行操作。
3.增加节点:rs.add(“192.168.80.128:27024”)
>XbqSet:PRIMARY> rs.add("192.168.80.128:27024")
{ "ok" : 1 }
4.查看状态,rs.status(),发现新增加的节点 为从节点,即 SECONDARY。
XbqSet:PRIMARY> rs.status()
{
"set" : "XbqSet",
"date" : ISODate("2017-07-29T14:31:44.872Z"),
"myState" : 1,
"members" : [
{
"_id" : 1,
"name" : "192.168.80.128:27021",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 630,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"lastHeartbeat" : ISODate("2017-07-29T14:31:43.555Z"),
"lastHeartbeatRecv" : ISODate("2017-07-29T14:31:43.457Z"),
"pingMs" : 0,
"configVersion" : 2
},
{
"_id" : 2,
"name" : "192.168.80.128:27022",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1854,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"lastHeartbeat" : ISODate("2017-07-29T14:31:43.555Z"),
"lastHeartbeatRecv" : ISODate("2017-07-29T14:31:43.186Z"),
"pingMs" : 0,
"configVersion" : 2
},
{
"_id" : 3,
"name" : "192.168.80.128:27023",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1953,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"electionTime" : Timestamp(1501338022, 1),
"electionDate" : ISODate("2017-07-29T14:20:22Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 4,
"name" : "192.168.80.128:27024",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 7,
"optime" : Timestamp(1501338697, 1),
"optimeDate" : ISODate("2017-07-29T14:31:37Z"),
"lastHeartbeat" : ISODate("2017-07-29T14:31:43.566Z"),
"lastHeartbeatRecv" : ISODate("2017-07-29T14:31:43.579Z"),
"pingMs" : 6,
"configVersion" : 2
}
],
"ok" : 1
}
5.删除节点:rs.remove(hostportstr)
XbqSet:PRIMARY> rs.remove("192.168.80.128:27024")
{ "ok" : 1 }
查看状态,发现 端口为27024的节点 没有了。
以上是关于MongoDB 副本集 pymongo使用的主要内容,如果未能解决你的问题,请参考以下文章