Problem connecting to the DB with mongo-cxx driver with authentication

I have a mongDB V4.4 running in my VM (Ubuntu 20.04) in access control mode. I created an admin user with this role:
roles: [ { role: “root”, db: “admin” } ]
I installed the mongoDB driver for C++ (V3.6.2) and wrote a mini-program to test it. My code (basically copied from mongocxx tutorial):

mongocxx::instance instance{};
  mongocxx::uri uri(
      "mongodb://userName:password@localhost:27017/?authSource=admin");
  mongocxx::client conn(uri);

  mongocxx::database db = conn["test"];   
  mongocxx::collection coll = db["test"];

  mongocxx::cursor cursor = coll.find({});
  for (auto doc : cursor) {
    std::cout << bsoncxx::to_json(doc) << "\n";
  }

I inserted a document in test.test from mongo shell. The ‘find’ command works with the same user in mongo shell. When running this mini program I get:

terminate called after throwing an instance of 'mongocxx::v_noabi::query_exception'
what():  could not start SASLPrep for password: generic server error
Aborted (core dumped)

When I cancel the MonogDB access control and connect to it with the same lines but without the user/password it works great and I can insert and find documents.
I also installed Compass and copied the connection string from above in order to connect to MongoDB from it. It worked fine and I was able to read/write/delete and so on.
How to fix this?

1 Like