简体   繁体   English

如何使用 Python 在 mongodb 的分片集合中启用分片?

[英]How to enable sharding in shard collections in mongodb using Python?

Currently, I am using a bash-script in order to enable sharding for DB and then shard collection.目前,我正在使用 bash 脚本来为数据库启用分片,然后进行分片收集。 I am using eval:我正在使用评估:

mongo <ip_host> --eval "sh.enableSharding(<name_db>)

and then, surprise, shard collection:然后,令人惊讶的是,碎片收集:

mongo <ip_host> --eval "sh.shardCollection("<db_name>.<collection>", {shardKey : 1})"

I am trying to do the same, but from Python using pymongo lib.我正在尝试做同样的事情,但是从 Python 使用 pymongo lib。 I have some ideas:我有一些想法:

client = pymongo.MongoClient()
client.admin.command('enableSharding', db_name)    
client.admin.command('shardCollection', f'{db_name}.{collection}), key=shard_key)

or using eval:或使用评估:

db_name.eval("sh.enableSharding", collection)
db_name.eval("sh.shardCollection", collection)

But neither of these methods work.但是这两种方法都不起作用。 Is there anything you could suggest or should I use os.system() to call bash-scripting from Python?有什么你可以建议的,或者我应该使用 os.system() 从 Python 调用 bash-scripting 吗?

Your command client.admin.command('enableSharding', db_name) is correct;您的命令client.admin.command('enableSharding', db_name)是正确的; however it can only be run against a mongos instance ;但是它只能针对mongos实例运行 I suspect you are trying to run it against a mongod instance.我怀疑您正试图针对mongod实例运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM