Shavit Talman
Shavit Talman

Reputation: 21

Getting "could not start SASLPrep for password: generic server error" error when trying to insert/find documents in mongoDB in C++

I have a mongDB running in my VM (Ubuntu) in access control mode. I created an admin user with this role:

    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]

I installed the mongoDB driver for C++ 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. How to fix this?

Upvotes: 2

Views: 630

Answers (1)

rodyherrera
rodyherrera

Reputation: 1

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.

Upvotes: 0

Related Questions