如何从MongoDB Shell连接到副本集? [英] How do you connect to a replicaset from a MongoDB shell?

查看:89
本文介绍了如何从MongoDB Shell连接到副本集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我正在编写连接到mongodb的应用程序,则可以提供副本集的种子列表,驱动程序会将我定向到主节点,在该节点上可以运行写命令.

If I'm writing an application which connects to mongodb then I can provide a seed list for a replicaset, and the driver will direct me to the master node, where I can run write commands.

如何为命令行mongo shell指定种子列表,以连接到副本集.

How do I specify the seed list for a commandline mongo shell in order to conenct to a replicaset.

推荐答案

要连接到副本集Primary,请使用mongo shell --host选项:

To connect to a replica set Primary use the mongo shell --host option:

mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3],etc

例如:

$ mongo --host rs1/john.local:27019,john.local:27018
MongoDB shell version: v3.4.9
connecting to: mongodb://john.local:27019,john.local:27018/?replicaSet=rs1
2017-10-12T14:13:03.094+0000 I NETWORK  [thread1] Starting new replica set monitor for rs1/john.local:27019,john.local:27018
2017-10-12T14:13:03.096+0000 I NETWORK  [thread1] Successfully connected to john.local:27019 (1 connections now open to john.local:27019 with a 5 second timeout)
2017-10-12T14:13:03.096+0000 I NETWORK  [thread1] Successfully connected to john.local:27018 (1 connections now open to john.local:27018 with a 5 second timeout)
rs1:PRIMARY> db
test
rs1:PRIMARY>

注意:从版本3.4.2到3.4.10,存在一个错误(

Note: From versions 3.4.2 to 3.4.10 there was a bug (SERVER-28072) which prevented specifying the db after when using --host or --port.

这篇关于如何从MongoDB Shell连接到副本集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆