B4J Question Connecting to Mongodb

lucdrb

Active Member
Licensed User
Longtime User
Hi,

I'm learning how to use a MongoDb Database and I've a problem with the connection, I've change the URI adress of the MongoDbAlexa for example
B4X:
    mongo.Initialize("", "mongodb://192.168.0.33:27017")
    db = mongo.GetDatabase("sites")
    ranks = db.GetCollection("ranks")

and I got this error message

Waiting for debugger to connect...
Program started.
Error occurred on line: 18
com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.0.33:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]
at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:375)
at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:104)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:203)
at com.mongodb.operation.CountOperation.execute(CountOperation.java:207)
at com.mongodb.operation.CountOperation.execute(CountOperation.java:54)
at com.mongodb.Mongo.execute(Mongo.java:818)
at com.mongodb.Mongo$2.execute(Mongo.java:805)
at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:185)
at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:165)
at anywheresoftware.mongo.MongoCollectionWrapper.getCount(MongoCollectionWrapper.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:613)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:159)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
at b4j.example.main.main(main.java:29)

Do I miss something in the connection string?
The MongoDB is installed on a Ubuntu Linux computer.

Luc
 

OliverA

Expert
Licensed User
Longtime User
Or it may be a port issue due to this:
Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect
Are you sure your DB installation is using 27017 as port? If so, are you sure external access is allowed to this port and/or to the DB itself?
 
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
Unfortunately:


The DB don't have password or user name, I didn't fix any

Are you sure your DB installation is using 27017 as port? If so, are you sure external access is allowed to this port and/or to the DB itself?

I have access to it on the computer where it's installed


MongoDB shell version v3.4.9

connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.9
Server has startup warnings:
2016-02-11T17:28:03.946+0100 I STORAGE [initandlisten]
2016-02-11T17:28:03.946+0100 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2016-02-11T17:28:03.946+0100 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2016-02-11T17:28:04.093+0100 I CONTROL [initandlisten]
2016-02-11T17:28:04.093+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2016-02-11T17:28:04.093+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2016-02-11T17:28:04.093+0100 I CONTROL [initandlisten]
>
But I don't know how to reach it in remote on the 192.168.0.33 adress

Thank for your help.

Luc
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

lucdrb

Active Member
Licensed User
Longtime User
@OlivierA
Many thanks for your help.:)
Connection made.:D
I can go further with the study of B4J and MongoDB

Luc
 
Upvote 0
Top