Cloud9
Cloud9

Reputation: 21

perl: cannot connect to mongodb server

I am having a problem connecting to the mongodb server from my Perl script.

But on trying to connect via Perl, I am getting the error:

"MongoDB::SelectionError: No readable server available for matching read preference primary. MongoDB server status:
Topology type: Single; Member status:
localhost:27017 (type: Unknown, error: MongoDB::NetworkError: Could not 
connect to 'localhost:27017': Bad address )"

The simple perlscript is:

use strict;
use Data::Dumper;
use open qw/:std :utf8/;
use IO::Handle;
use MongoDB;

#my $client = MongoDB->connect('mongodb://localhost');   
# connect to localhost:27017
my $client = MongoDB::MongoClient->new(host => 'localhost', port => 27017);
my $db   = $client->get_database( 'example');
my $people_coll = $db->get_collection('people');
$db->drop;

The line:

my $client = MongoDB::MongoClient->new(host => 'localhost', port => 27017);

does not appear to throw the error, but trying to create or amend a dbase does. I have reviewed similar questions on mongodb connection issues and have tried turning off firewall, remove /tmp/mongodb-27017.sock (although I couldn't locate this file anywhere).

Any help is most welcome!

Upvotes: 2

Views: 690

Answers (1)

Daniel Peña
Daniel Peña

Reputation: 76

By default MongoDB Server is available to 127.0.0.1 So change localhost by 127.0.0.1

Then if you want to customize this, so you can access this MongoDB Server from other computer client check out this post: https://www.linkedin.com/pulse/mongodb-server-service-windows-10-daniel-pe%C3%B1a/

Upvotes: 2

Related Questions