开发者

In perl, how to connect to remote mongodb?

Set up a db in mongoHQ,

The mongo URI:

mongodb://<user>:<password>@staff.mongohq.com:10022/testdb

The mongodb connection as described by the perldoc mongodb:

my $connection = MongoDB::Connection->new(host => 'localhost', port => 27017);
my $database   = $connection->foo;

If the user is: dbuser passwo开发者_C百科rd is: dbpass

What's connection string in perl?

Thanks


As per the CPAN docs,

host should be in the format mongodb://staff.mongohq.com:10022.

username and password are the constructor attributes for those fields.

So, for example, you might say

my $connection = MongoDB::Connection->new(host => 'mongodb://staff.mongohq.com:10022', username => 'dbuser', password => 'dbpass', db_name => 'testdb');
my $database = $connection->testdb;


use MongoDB;
use MongoDB::OID;

my $conn = MongoDB::Connection->new(
    host => 'remote server's inet address or host name', 
    port => 27017, 
    username=>'your_user_name', 
    password => 'your_password', 
    db_name => 'your_db_name'
);

This code will connect and authenticate you to remote mongodb.

For more details on how to use mongodb with perl check http://search.cpan.org/dist/MongoDB/lib/MongoDB/Tutorial.pod


for staff.mongohq.com:10022

The host is: staff.mongohq.com The port is port: 10022

See also, MongoDB::Connection documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜