如何在mongodb+srv方案中使用自定义服务名称?

gab6jxml  于 2022-11-03  发布在  Go
关注(0)|答案(1)|浏览(151)

根据文档https://www.mongodb.com/docs/v4.4/reference/connection-string/#std-label-connections-dns-seedlist mongodb+srv://server.example.com/相当于_mongodb._tcp.server.example.com的记录。
但是,我有多个Mongo服务,我想连接到_mymongodb._tcp.server.example.com。我如何指定使用mymongodb服务名称而不是mongodb?我已经盯着文档看了几个小时,似乎找不到答案。

sz81bmfz

sz81bmfz1#

It's not possible, the string _mongodb._tcp is prefixed with the host name, and it is hardcoded in the source code. https://github.com/mongodb/mongo/blob/3e63636e66ced4aed6d1aaaafcdca7ae19cf96f2/src/mongo/client/mongo_uri.cpp#L408
The only possible way is to handle that manually, for example in Linux shell with:

srvs=$(dig +short SRV _mymongodb._tcp.server.example.com | awk '{print $4":"$3}' | paste -d,)
mongo "mongodb://$srvs/"

相关问题