如何从 MongoDB shell 连接到副本集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13912765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:57:37  来源:igfitidea点击:

How do you connect to a replicaset from a MongoDB shell?

mongodbmongo-shell

提问by Richard Warburton

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.

如果我正在编写一个连接到 mongodb 的应用程序,那么我可以为副本集提供一个种子列表,驱动程序会将我定向到主节点,在那里我可以运行写入命令。

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

如何为命令行mongoshell指定种子列表以连接到副本集。

回答by Gianfranco P.

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

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

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

For example:

例如:

$ 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>

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.

注意:从 3.4.2 到 3.4.10 版本存在一个错误 ( SERVER-28072),它阻止在使用 --host 或 --port 后指定数据库。

回答by Shemeshey

The answers above are for Mongo 3.2.

以上答案适用于Mongo 3.2

According to Mongo 3.4documentation, the shell was changed a bit:

根据Mongo 3.4文档,外壳略有更改:

In 3.2:
mongo --host host1,host2,host3/myRS myDB
or,
mongo --host host1:27017,host2:27017,host3:27017/myRS myDB

在 3.2:
mongo --host host1,host2,host3/myRS myDB
或,
mongo --host host1:27017,host2:27017,host3:27017/myRS myDB

In 3.4:
mongo "mongodb://host1,host2,host3/myDB?replicaSet=myRS"
or,
mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"

在 3.4:
mongo "mongodb://host1,host2,host3/myDB?replicaSet=myRS"
或,
mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"

回答by Doron Levari

All you have to do is to use --host and give it one of your hosts in the replicaset but with the name of the replicaset as a prefix.

您所要做的就是使用 --host 并为其提供副本集中的主机之一,但使用副本集的名称作为前缀。

For example:

例如:

mongo --host my_mongo_server1

will connect to my_mongo_server1, it may just be yet another SECONDARY node.

将连接到 my_mongo_server1,它可能只是另一个 SECONDARY 节点。

But:

但:

mongo --host my_repl_set_name/my_mongo_server1

will always connect to the PRIMARY node in the replica set, even if it's not my_mongo_server1.

将始终连接到副本集中的 PRIMARY 节点,即使它不是 my_mongo_server1。

Why? The answer is "replica set monitor". In the example above, mongo shell would connect to the specified node, start a new replica set monitor for the replica set and will use the specified node just to seed it. From there, the monitor will figure out all nodes in the replica set and will switch the connection to the PRIMARY node.

为什么?答案是“副本集监视器”。在上面的示例中,mongo shell 将连接到指定的节点,为副本集启动一个新的副本集监视器,并将使用指定的节点为其播种。从那里,监视器将找出副本集中的所有节点,并将连接切换到 PRIMARY 节点。

Hope that helped.

希望有所帮助。

回答by kristina

You can use the "name/seed1,seed2,..." format:

您可以使用“name/seed1,seed2,...”格式:

> conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
> db = conn.getDB("test")

This should give you a connection to whichever node is currently primary and handle failover okay. You can specify one or more seeds and it'll find the rest.

这应该为您提供与当前主节点的连接,并可以正常处理故障转移。您可以指定一个或多个种子,它会找到其余的。

Note that (AFAIK) the shell does not allow you to route reads to secondaries with a replica set connection.

请注意,(AFAIK)shell 不允许您使用副本集连接将读取路由到辅助节点。

回答by Chris Heald

To the best of my knowledge, the mongo command line client will not accept seeds to forward you to the master node, because you may often want to actually operate on the secondary node rather than being forwarded.

据我所知,mongo 命令行客户端不会接受种子将您转发到主节点,因为您可能经常希望在辅助节点上实际操作而不是被转发。

However, once connected to any node in the RS, you can discover the RS topology via rs.config()or db.isMaster(). You could then use this information to reconnect to the primary node. Depending on your shell, you might be able to use mongo --eval "db.isMaster()['primary']"to automatically connect to the master.

但是,一旦连接到 RS 中的任何节点,您就可以通过rs.config()或发现 RS 拓扑db.isMaster()。然后,您可以使用此信息重新连接到主节点。根据您的 shell,您也许可以使用mongo --eval "db.isMaster()['primary']"自动连接到主服务器。

回答by geekysteven

In the shell, you can first use:

在shell中,你可以先使用:

mongo --nodb

to open a mongo session without connecting to mongo replicaset

在不连接到 mongo 副本集的情况下打开 mongo 会话

Then, like kristina said, then you should be able to use

然后,就像克里斯蒂娜所说的那样,你应该可以使用

conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")

to connect to a replicaset.

连接到副本集。

Or eventually put

或者最终把

conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")

in your js file and

在你的 js 文件和

mongo --nodb yourcode.js

回答by Sam Hiatt

You can use the --hostparam to specify the replSet name and seed list, then mongowill automatically connect to the current primary host.

您可以使用--hostparam 指定 replSet 名称和种子列表,然后mongo将自动连接到当前的主要主机。

example:
mongo --host rs0/1.example.com:27017,2.example.com:27017,3.example.com:27017 [dbname]

例子:
mongo --host rs0/1.example.com:27017,2.example.com:27017,3.example.com:27017 [dbname]

回答by bruin

I am using v3.4. Also new to mongodb stuff... Although the help info from "man mongo" suggests to use "--host replicaSet/host:port,host:port" url, it does not work for me. However, I can connect to my replicaSet according to official document, as below:

我正在使用 v3.4。也是 mongodb 的新手……虽然来自“man mongo”的帮助信息建议使用“--host replicaSet/host:port,host:port”url,但它对我不起作用。但是,我可以根据官方文档连接到我的replicaSet ,如下所示:

$ mongo "mongodb://c1m,c2m,c3m/?replicaSet=rs0"
MongoDB shell version v3.4.1
connecting to: mongodb://c1m,c2m,c3m/?replicaSet=rs0
2017-02-08T14:46:43.818+0800 I NETWORK  [main] Starting new replica set monitor for rs0/c1m:27017,c2m:27017,c3m:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten]
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten]
rs0:PRIMARY>

So I guess the man page of my mongo is outdated (I am using CentOS 7.3).

所以我猜我的 mongo 的手册页已经过时了(我使用的是 CentOS 7.3)。

回答by mreppy

Building on the answer by Chris Heald these two bash aliases let me connect to the master with one command (where db1.test.test is one member of the replica set, acme is the database name, mreppy is my account etc) It will fail of course if db1 is down, but it's still handy.

基于 Chris Heald 的回答,这两个 bash 别名让我可以使用一个命令连接到主服务器(其中 db1.test.test 是副本集的一个成员,acme 是数据库名称,mreppy 是我的帐户等)它将失败当然如果 db1 宕机了,但它仍然很方便。

alias whichprimary='mongo db1.test.test/acme --username mreppy --password testtest --quiet --eval "db.isMaster()['"'primary'"']"' 
alias connectprimary='mongo -u mreppy -p testtest `whichprimary`/acme'

The quoting in the eval alias is hard, I used How to escape single-quotes within single-quoted strings?for help :-)

eval 别名中的引用很难,我使用了如何在单引号字符串中转义单引号?求助 :-)

回答by deep cool

mongodb://< dbuser >:< dbpassword >@example.com:< port >,example2.com:< port >/< dbname >?replicaSet=setname