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?

2 Likes

I’ve been getting the same error after a thousand attempts to solve it, at first I even thought it was my computer’s environment, I thought it was something software, I assumed it was maybe a library problem; I tried in other environments and wow, same error. I really didn’t know what exactly I was doing wrong, my server was working correctly and I could connect to it, but not from C++.

After searching and searching, there was something that I had not done, and that was to try the mongoc library (connector to the database through C, not C++, obviously you can also use it in C++), and curiously I was able to do the Successful connection through libmongoc instead of using libmongocxx.

Let’s assume that it is an error in the libmongocxx library, because no matter how much I searched, I couldn’t find ways to solve the error.

Therefore, my recommendation is to simply use libmongoc instead of libmongocxx.

I hope it can help you, and others who may have the same problem, greetings.

SASLprep defines the steps required to prepare passwords for comparison and authentication. It’s a is a prerequisite for the SCRAM-SHA-256 spec and required for compliance if you are using this auth method.

The error indicates that the saslprep couldn’t be performed, hence the authentication fails.
The C++ Driver wraps the C Driver and relies on it for authentication.

In order to resolve this, you can perform one of the below actions:

  • Make sure the C Driver you are using is 1.25 or above (this has in built support for saslprep).
  • If you are using the C Driver version < 1.25 - build it with libicu
  • Switch to SCRAM-SHA-1 that doesn’t require saslprep. You can do so by adding authMechanism=SCRAM-SHA-1 to your connection string. Example:
    mongoc_client_t *client = mongoc_client_new ("mongodb://user:password@localhost/?authMechanism=SCRAM-SHA-1&authSource=mydb");

My suggestion would be to just use the latest versions of C (1.27.1) and C++ (3.10.1) driver - that should resolve your issue.