Show my code
conf.set( "mongo.input.uri" , "mongodb://127.0.0.1/stackoverflow.mrtest" );
conf.set( "mongo.output.uri" , "mongodb://127.0.0.1/stackoverflow.mrtest_out2" );
the code runs without error when the host is localhost
or 127.0.0.1
. But when the host changed to my ip wlan0 192.168.1.102
, it returned the following error
Cluster created with settings {hosts=[192.168.1.102:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Exception in monitor thread while connecting to server 192.168.1.102:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 more
I have open the port 27017.
sudo iptables -A INPUT -ptcp --dport 27017 -j ACCEPT
My OS is Ubuntu 14.04.
How should I fix it? Thank you!
If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.
In short, the top most causes of this error Mongodb connection refused in Ubuntu are conflicting rules in the server firewall for the MongoDB port or bad settings in mongod. conf file.
To connect to a MongoDB Server using username and password, you have to use 'username@hostname/dbname'. Where username is the username, password is the password for that user and dbname is the database to which you want to connect to. Note : You can use multiple hostname to connect to with a single command.
By default MongoDB only binds to the loopback interface which makes it only accessible from localhost. To change that you need to edit this line in mongod.conf
file;
# /etc/mongod.conf
# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip = 127.0.0.1
you can change it to bind_ip = 127.0.0.1,192.168.1.102
to allow LAN and local connections or you can remove or comment out that line to allow all connections.
For more info : MongoDB – Allow remote access
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With