I'm having problems using Java MongoClient to access collections in my database

I fought for access to MongoDB collections in my openshift web application from my Java client and with an error at every step. I can connect, but I cannot request collections in any way.

Here is the current error message:

JBWEB000070: exception

org.springframework.web.util.NestedServletException: request processing failed; The nested exception is com.mongodb.MongoTimeoutException: thrown after 30,000 ms while waiting for a server that matches ReadPreferenceServerSelector {readPreference = primary}. Cluster state client view: {type = UNKNOWN, servers = [{address = 127.12.158.2: 27017, type = UNKNOWN, state = CONNECTING, exception = {com.mongodb.MongoSecurityException: exception authentication} caused by {com.mongodb. MongoCommandException: command failed with error 18: "auth fail" on the server 127.12.158.2:27017. Full answer: {"code": 18, "ok": 0.0, "errmsg": "auth failed"}}}]

    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:965)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

JBWEB000071: the root cause

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=127.12.158.2:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth fails' on server 127.12.158.2:27017. The full response is { "code" : 18, "ok" : 0.0, "errmsg" : "auth fails" }}}]

    com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
    com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
    com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:167)
    com.mongodb.operation.FindOperation.execute(FindOperation.java:394)
    com.mongodb.operation.FindOperation.execute(FindOperation.java:57)
    com.mongodb.Mongo.execute(Mongo.java:760)
    com.mongodb.Mongo$2.execute(Mongo.java:747)
    com.mongodb.OperationIterable.iterator(OperationIterable.java:47)
    com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:135)
    com.mongodb.FindIterableImpl.iterator(FindIterableImpl.java:36)
    org.jboss.tools.example.springmvc.mongodb.model.util.MongodbUtil.getCataloguesWithoutJoins(MongodbUtil.java:184)

, mongoClient, MondodbUtil, stacktrace:

public static MongoClient getMongoClient() {
    // If mongoClient not yet set up 
    if (mongoClient == null) {
        mongoClient = new MongoClient(new MongoClientURI(
                "mongodb://"+System.getenv("OPENSHIFT_MONGODB_DB_USER")+":"+
            System.getenv("OPENSHIFT_MONGODB_DB_PASSWORD")+"@"+
            System.getenv("OPENSHIFT_MONGODB_DB_HOST")+":"+
            System.getenv("OPENSHIFT_MONGODB_DB_PORT")+"/jbosseap"));
    }

    return mongoClient;
}

:

public static MongoClient getMongoClient() {
    // If mongoClient not yet set up 
    if (mongoClient == null) {
        List<ServerAddress> seeds = new ArrayList<ServerAddress>();
        seeds.add( new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"), 
                    Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        credentials.add(
            MongoCredential.createMongoCRCredential(
                "OPENSHIFT_MONGODB_DB_USER",
                "jbosseap",
                "OPENSHIFT_MONGODB_DB_PASSWORD".toCharArray()
            )
        );
        mongoClient = new MongoClient( seeds, credentials );

    }
    return mongoClient;
}

And also:

public static MongoClient getMongoClient() {
    // If mongoClient not yet set up 
    if (mongoClient == null) {

            mongoClient = new MongoClient(
                new ServerAddress(System.getenv("OPENSHIFT_MONGODB_DB_HOST"), 
                Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"))));

    }
    return mongoClient;
}

I have run out of ideas now, so seeking your wisdom!
0
2

mongod ? :

  • Windows cmd.exe
  • Ctrl+Shift+Enter
  • cd c:/Mongo/mongodb/bin ( bin)
  • , db : c:/data/db ( )
  • mongod, : mongod
  • mongod :

2017-02-26T11: 33: 37.233 + 0530 FTDC [initandlisten] "E:/data/db/diagnostic.data" 2017-02-26T11: 33: 37.359+ 0530 NETWORK [thread1] 27017

  1. .

, .

+1

.

OPENSHIFT_MONGODB_DB_USER , . "admin" , .

( , ) ​​ OPENSHIFT_MONGODB_DB_PASSWORD.

0

Source: https://habr.com/ru/post/1648252/


All Articles