简体   繁体   English

使用rmongodb连接到MongoDB副本集

[英]Connecting to MongoDB replica set with rmongodb

Has anyone been able to connect to a MongoDB replica set using rmongodb ? 有没有人能够使用rmongodb连接到MongoDB副本集? No matter how I configure mongo.create I get an authentication error, even though the same host/username/password work fine when connecting via the mongo shell. 无论我如何配置mongo.create ,即使通过mongo shell进行连接时,相同的主机/用户名/密码都可以正常工作,但mongo.create收到身份验证错误。

My code does the equivalent of: 我的代码等效于:

> mongo.create(c("rs-1.mysite.com:12345","rs-2.mysite.com:12345"), "rsName", "user", "password", "my_db")
Unable to connect to replset
Authentication failed.

Update: 更新:

Looking at the logs of all the nodes in the replica set, I do not see any attempt to authenticate when I run the code above. 查看副本集中所有节点的日志,当我运行上面的代码时,我看不到任何尝试进行身份验证的尝试。 Therefore, this may be a rmongodb bug. 因此,这可能是rmongodb错误。

As Sim has noted, rmongodb 1.0.3 does not resolve hostnames . 正如Sim所指出的那样, rmongodb 1.0.3不能解析主机名

It is, however, possible to connect to replica sets from rmongodb with with a few caveats: 但是,可以从rmongodb连接到副本集,但要注意以下几点:

  • you must include all hostnames (if the primary isn't found in the seed host list, rmongodb will fail to connect) 您必须包含所有主机名(如果在种子主机列表中找不到主主机名,则rmongodb将无法连接)
  • hostnames must be provided as IPs 主机名必须作为IP提供
  • if using an admin user, you must first auth to the admin database (this, at least, is expected behaviour but worth noting) 如果使用管理员用户,则必须首先对管理员数据库进行身份验证(至少这是预期的行为,但值得注意)
  • I could only get the connection to work by not providing a replSet name 我只能通过提供replSet名称来使连接正常工作

So my working connect string looks like: 所以我的工作连接字符串看起来像:

mongo.create(c("192.168.1.123:27017","192.168.1.124:27018","192.168.1.125:27017"),"","user","password", "thedb")

NB: I only tested this with MongoDB 2.2.0. 注意:我仅使用MongoDB 2.2.0进行了测试。

Looking at the C source, it seems that rmongodb does not resolve hostnames into IP addresses. 从C源代码来看, rmongodb似乎没有将主机名解析为IP地址。 The only way this works is if you pass an IP address string to the driver... 唯一可行的方法是将IP地址字符串传递给驱动程序。

By passing an IP address and port number you can connect to one of the nodes. 通过传递IP地址和端口号,您可以连接到其中一个节点。 I still cannot get rmongodb to successfully connect to a replica set. 我仍然无法使rmongodb成功连接到副本集。

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

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