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
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.
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?