How to connect to multiple mongod instances with c++ driver

332 views
Skip to first unread message

Thomas

unread,
Sep 24, 2012, 12:38:29 PM9/24/12
to mongod...@googlegroups.com
Hello,

I connect my application (c++) with 2 differents mongo db instances (one localhost:27017 and the other localhost:27007) that both need authentication.
I can't find in the c++ driver how to do it properly (considering i need a pool of connection)

Previously I had two databases on the same instance and i used ScopedDbConnection + a hook (pool.addHook) defined as below :

class DbAuthHook : public DBConnectionHook
{
    void onCreate(DBClientBase *conn)
    {
        //authenticate
        string err;
        try
        {   
            if(conn->auth(NAConfig::dbName, NAConfig::userDb, NAConfig::passwordDb, err) == false)
            {
                myCout << "[TERM]Unable to authenticate to db : " << err << endl;
                exit(-1);
            }  
            //log auth
            if(conn->auth(NAConfig::logDbName, NAConfig::logUserDb, NAConfig::logPasswordDb, err) == false)
            {
                myCout << "[TERM]Unable to authenticate to log db : " << err << endl;
                exit(-1);
            }  
        }
        catch(std::exception &ex)
        {
            myCout << "[TERM]Error while authenticating with db : " << ex.what() << endl;
            exit(-1);
        }
    }
    void onHandedOut(DBClientBase *conn)
    {
    }
    void onDestroy(DBClientBase *conn)
    {
    }

};


In my hook i have no way to detect for which host it is called for.
Do you have any idea how can I handle this properly ?

thanks,

Thomas.

David Hows

unread,
Sep 25, 2012, 12:54:12 AM9/25/12
to mongod...@googlegroups.com
Thomas,

My understanding is that you have two independent mongod instances which contain different data and must be queried independently. You wish to have only one connection object (your hook as given) which will take all database requests and then route them accordingly.

There is no way within mongo itself to hold two unrelated connections, to different servers and deterministically route commands towards them. If you wish to do this it would be best to extend your hook class to determine from a given command which database it should use from your pool. 

You can do this a number of ways ranging from a simple flag to select database right through to pre-preparing a list of all databases and collections and examining the query to determine where i should be routed.

If i have misunderstood any clarifications would be great,

Good luck,

David

Thomas

unread,
Sep 25, 2012, 4:41:03 AM9/25/12
to mongod...@googlegroups.com
Hello David,

I do not want to have one connection object, but one per mongo instance. 
The fact is that in order to use both authentication and ScopedDbConnection (i really need a pool since multithreaded app), i need to add a hook in pool global variable that will make the authentication.
There is no way in the hook to determine from which host the onCreate method is call for.

What I would like is to be able to switch on the mongo host directly in the hook

Here is an example of how i use db connection :

on init :
DbAuthHook *dbhook = new DbAuthHook();
pool.addHookd(dbhook);

when request to db is needed :
ScopedDbConnection logconn(logcstring);
logconn->insert(log_collection, BSON("test" << "log"));
ScopedDbConnection dbconn(dbcstring);
dbconn->insert(data_collection, BSON("data" << "new_data"));


I hope it is a little bit more clearer,

Thomas.

gaur hari

unread,
Sep 26, 2012, 1:40:07 AM9/26/12
to mongod...@googlegroups.com
Hello Thomas,

Can you guide me how to setup connection pooling in c++ for mongodb in case of single database.

With Regards
Gaurhari

gaur hari

unread,
Sep 26, 2012, 1:41:17 AM9/26/12
to mongod...@googlegroups.com
how to handle below

David Hows

unread,
Sep 26, 2012, 2:54:16 AM9/26/12
to mongod...@googlegroups.com
Thomas,

To confirm, your issue is that you wish to be able to use the same DbAuthHook class to implement auth on two different connections too two different servers/databases.

Your problem is that you are unable too determine which database is which from within DbAuthHook?

My question is how do you intend to pass the auth variables into that class? 

If you are loading them statically (they are hard coded inside your application) within DbAuthHook by comparing the give server (by IP + Port) could you not just use conn.getServerAddress() or conn.toString() to get the connection string and then compare to from within DbAuthHook?

Cheers,

David

Thomas

unread,
Sep 26, 2012, 5:20:12 AM9/26/12
to mongod...@googlegroups.com
Indeed that what I have done (static loading) i compare getServerAddress.
It was not working since the getServerAddress from the replica was not exactly the same than the one given in my configuration file (localhost replaced by host name).
I changed it and now it works, 
thank you for your time and your answers David,

Thomas.

To Gaur :
Which version of c++ driver do you have ?
(it is not exactly the same behaviour since 2.2)
But basically my previous example show a pooling connection to 2 dbs, you can easily remove one db and it will work.
The main idea is that when you want to request db, you create a ScopedDbConnection object with the connection string to your db, and when you have finished call the done method
Exemple :

ScopedDbConnection conn(_logcstring);
conn->insert(NAConfig::logCollection, ABSONObject);
conn.done();

ScopedDbConnection will handle the pool for you.

gaur hari

unread,
Sep 26, 2012, 6:28:28 AM9/26/12
to mongod...@googlegroups.com
Thanks Thomas

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

presid...@gmail.com

unread,
Aug 13, 2013, 2:02:18 AM8/13/13
to mongod...@googlegroups.com
Hi Thomas,have you solved the problem?  It seems impossible to auth  two different instances enven with two hooks.
Reply all
Reply to author
Forward
0 new messages