0

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

  • isnt it suppose to be just `mongodb://user:pass@server.com:80/mongo` ? Can you connect to your mongodb with mongo CLI client using the url ? – suenda Feb 03 '18 at 17:20
  • Thanks @suenda question edited Just to be clear: here "mongo" is not my database name but the extra path on the server – Kévin Darty Feb 03 '18 at 17:32
  • Your apache2 conf list your monogodb url as http://localhost:27017, isnt that a problem ? mongodb is not a HTTP server – suenda Feb 03 '18 at 17:44
  • Here is a way to proxy TCP connections : https://stackoverflow.com/questions/32619379/how-to-setup-a-reverse-proxy-on-several-ports-tcp-udp – suenda Feb 03 '18 at 17:45

1 Answers1

0

The main problem is that the path part (/mongo) is understood by mongo as the called database instead of the following path part (/dbName).

The solution using NGinx is to edit an available site as follow:

upstream stream_mongo_backend {
   server localhost:27017;
}

server {
        listen       80;
        listen  443 ssl;
        server_name  server.com;

        include path/to/cert.conf;

        location /mongo {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://stream_mongo_backend/;
                proxy_redirect off;
        }
}