developer tip

MongoDB 셸에서 복제 세트에 어떻게 연결합니까?

optionbox 2020. 11. 10. 08:02
반응형

MongoDB 셸에서 복제 세트에 어떻게 연결합니까?


mongodb에 연결하는 응용 프로그램을 작성하는 경우 복제 세트에 대한 시드 목록을 제공 할 수 있으며 드라이버가 쓰기 명령을 실행할 수있는 마스터 노드로 안내합니다.

mongo복제 세트에 연결하기 위해 명령 줄 셸에 대한 시드 목록을 어떻게 지정합니까?


복제본 세트 기본에 연결하려면 mongo 쉘 --host옵션을 사용하십시오 .

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까지 --host 또는 --port를 사용할 때 db를 지정하지 못하게 하는 버그 ( SERVER-28072 )가있었습니다.


위의 답변은 Mongo 3.2에 대한 것 입니다.

Mongo 3.4 문서 에 따르면 셸이 약간 변경되었습니다.

3.2에서 :
mongo --host host1,host2,host3/myRS myDB
또는
mongo --host host1:27017,host2:27017,host3:27017/myRS myDB

3.4에서 :
mongo "mongodb://host1,host2,host3/myDB?replicaSet=myRS"
또는
mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"


"name / seed1, seed2, ..."형식을 사용할 수 있습니다.

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

이렇게하면 현재 기본 노드에 대한 연결이 제공되고 장애 조치를 정상적으로 처리 할 수 ​​있습니다. 하나 이상의 시드를 지정하면 나머지를 찾을 수 있습니다.

(AFAIK) 셸에서는 복제본 세트 연결을 사용하여 읽기를 보조로 라우팅 할 수 없습니다.


--host를 사용하고 복제 세트의 호스트 중 하나를 제공하지만 복제 세트의 이름을 접두사로 지정하기 만하면됩니다.

예를 들면 :

mongo --host my_mongo_server1

my_mongo_server1에 연결되며 또 다른 SECONDARY 노드 일 수 있습니다.

그러나:

mongo --host my_repl_set_name/my_mongo_server1

my_mongo_server1이 아니더라도 복제본 세트의 PRIMARY 노드에 항상 연결됩니다.

왜? 정답은 "복제본 세트 모니터"입니다. 위의 예에서 mongo 쉘은 지정된 노드에 연결하고 복제 세트에 대한 새 복제 세트 모니터를 시작하고 지정된 노드를 사용하여 시드합니다. 여기에서 모니터는 복제본 세트의 모든 노드를 파악하고 연결을 PRIMARY 노드로 전환합니다.

도움이 되었기를 바랍니다.


내가 아는 한 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.


In the shell, you can first use:

mongo --nodb

to open a mongo session without connecting to mongo replicaset

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

mongo --nodb yourcode.js

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

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


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.

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 :-)


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:

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


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

참고URL : https://stackoverflow.com/questions/13912765/how-do-you-connect-to-a-replicaset-from-a-mongodb-shell

반응형