If I have a mongo at : server.com/mongo
on port 80. I cannot open any port here. How could I connect to it with Java?
This is not working:
mongodb://user:pass@server.com/mongo:80/dbName
The exception after 30sec:
Error during connection on server.com / dbName: 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=server.com/mongo:80, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketException: server.com/mongo}, caused by {java.net.UnknownHostException: server.com/mongo}}]
I also tried with:
mongodb://user:pass@server.com:80/mongo/dbName
The exception is:
Error during connection on server.com / dbName: The connection string contains an invalid host 'server.com:80/mongo'. The port '80/mongo' is not a valid, it must be an integer between 0 and 65535
My java code:
String url = "mongodb://user:pass@server.com/mongo:80/dbName";
MongoClientURI mongoUri = new MongoClientURI(uri);
MongoClient mongo = new MongoClient(mongoUri);
MongoDatabase db = mongo.getDatabase(dbName);
MongoIterable<String> collectionNames = db.listCollectionNames();
for (String name : collectionNames) {
System.out.println(name);
}
My apache2 site configuration:
<VirtualHost *:80>
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
LogLevel warn
<Location /mongo>
ProxyPass http://localhost:27017/
ProxyPassReverse http://localhost:27017/
</Location>
<Location /mongo/>
ProxyPass http://localhost:27017/
ProxyPassReverse http://localhost:27017/
</Location>
ExpiresActive On
ExpiresDefault "access plus 5184000 seconds"
AllowEncodedSlashes On
</VirtualHost>
Curl is responding (curl server.com/mongo
, curl server.com:80/mongo
)
It looks like you are trying to access MongoDB over HTTP on the native driver port.
My authentification is OK in mongo:
>db.auth("user", "pass")
1
>use dbName
switched to db dbName
EDIT
With a mongo CLI nothing is responding:
mongo server.com
mongo server.com:80
mongo server.com:80/mongo
mongo server.com/mongo
mongo server.com/mongo:80
Same with:
mongo user:pass@server.com/...
And:
mongo -u user -p pass server.com/...
Just to be clear: here mongo
is not my database name but the extra path on the server