ERROR: client: Failed to look up SRV record "_mongodb._tcp.cluster0.acawkvf.mongodb.net": The specified host is unknown. The parameter: client, in function mongoc_client_set_server_api, cannot be NULL

Hello,

I am trying to connect to a local database but getting this error:

ERROR:       client: Failed to look up SRV record "_mongodb._tcp.cluster0.acawkvf.mongodb.net": The specified host is unknown.
The parameter: client, in function mongoc_client_set_server_api, cannot be NULL

Following the official tutorial:
https://mongocxx.org/mongocxx-v3/tutorial/

C-driver is 3.6.7, system is Ubuntu 22.04.
Here is the code:

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/builder/stream/helpers.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/builder/stream/array.hpp>


using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;




int main() {
    mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://127.0.0.1:27017");
mongocxx::client client(uri);
mongocxx::database db = client["university"];
mongocxx::collection coll = db["unidata"];
    
    return 0;
}

This is test.cpp. I compile it like this:
c++ --std=c++11 test.cpp $(pkg-config --cflags --libs libmongocxx)
Compiles fine, then I ./test and get above error.
Kindly help me out.

Hi @Z.O.E_N_A ,

Do you have a local instance of mongod running on your machine? The error seems to indicate it’s not able to find the MongoDB server.

If you are having trouble with running a local instance, I suggest to create a free Atlas cluster and use it. Here are the steps to follow - Getting Your Free MongoDB Atlas Cluster | MongoDB

Also, here’s a tutorial which has some sample code that may be of your help - Getting Started with MongoDB and C++ | MongoDB

Yes I do have an instance running.
I did systemctl start mongod.
systemctl status mongod gives:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-03-15 15:45:24 PKT; 5min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 10439 (mongod)
     Memory: 229.7M
        CPU: 3.986s
     CGroup: /system.slice/mongod.service
             └─10439 /usr/bin/mongod --config /etc/mongod.conf

Mar 15 15:45:24 computer1 systemd[1]: Started MongoDB Database Server.

I tried using my Atlas cluster. Changed URI to mongodb+srv://********:********@cluster0.acawkvf.mongodb.net/?retryWrites=true&w=majority
This was the result. I chose c++ driver and version 3.6 and above.

No suitable servers found (`serverSelectionTryOnce` set): [Failed to receive length header from server. calling hello on 'ac-gu1mevj-shard-00-02.acawkvf.mongodb.net:27017'] [connection closed calling hello on 'ac-gu1mevj-shard-00-00.acawkvf.mongodb.net:27017'] [Failed to receive length header from server. calling hello on 'ac-gu1mevj-shard-00-01.acawkvf.mongodb.net:27017']

I tried connecting to Compass too from Atlas.
It gave me this:
connection <monitor> to 1x.xxx.xxx.xxx:27017 closed

I went into Network Access and whitelisted everything, so now I can connect to Compass but not to my c++ program.
It now gives me:
DEBUG: cluster: Authentication failed: bad auth : authentication failed bad auth : authentication failed

Anyone? My password or username do not contain anything but alphabets. I can connect fine to Compass. I have whitelisted my IP address (again), made a new user and gave it admin role. Still same error of bad auth. Why cant I connect to my c++ program?

Can you double check your username and password are correctly set by calling below methods after uri object is created?

cout << "password: " << uri.password() << std::endl;
cout << "username: " << uri.username() << std::endl;

It didnt even reach that point. When I ran ./test I got the same error as above: bad auth.

Are you adding above code before client object is created, ie, before mongocxx::client client(uri); ?

Yes

int main() {
    mongocxx::instance instance{}; 
mongocxx::uri uri("mongodb+srv://*******:********************@clust.enzybol.mongodb.net/?retryWrites=true&w=majority");
std::cout << "password: " << uri.password() << std::endl;
std::cout << "username: " << uri.username() << std::endl;
mongocxx::client client(uri);
mongocxx::database db = client["university"];
mongocxx::collection coll = db["unidata"];
return 0;