c++: mongodb SCRAM-SHA-1 mechanism support not compiled into client library 错误的解决


版本:

mongodb 3.4.x

mongo-cxx-driver-legacy-1.1.0

编译器: c++11

直接使用驱动的鉴权函数 auth(), 结果报  mongodb SCRAM-SHA-1 mechanism support not compiled into client library. (Some mechanisms require the driver be compiled with the flags –ssl or –use-sasl-client) 这个异常。

如果你就按照提示的这段信息去做,你就被坑了,别问我为什么知道

真实情况是,mongodb的驱动需要初始化,不初始化就会报这个错误,这该死的提示信息。做法如下:

在使用之前调用  mongo::client::initialize();   一次即可,不用重复初始化。

把特~~你以为这就结束了?

我的调用 mongo 初始化函数之后就挂了~~~

最后查出来了原因,需要在 mongo-cxx-driver-legacy-1.1.0 编译的时候加 –c++11=on  选项。

OK 都搞定了。

用的时候,记得每次调用 connect() 之后都要 auth() 一下

参考链接

https://stackoverflow.com/questions/28448406/the-authentication-mechanism-scram-sha-1-is-not-supported

http://stackoverflow.com/questions/30107966/mongoclientoptionsoptions-at-options-h32

 

问题原文链接:http://blog.csdn.net/lili861217/article/details/52779920

tips:

如果实在windows下面,如果是默认的,可以不用auth,initalize之后就可以直接用了,如果是在linux下面initalize挂了,就是编译c++ driver的时候,没有加 –c++11=on 选项

测试代码:

using namespace mongo;

void run()
{

mongo::client::initialize();

mongo::DBClientConnection conn;
conn.connect("127.0.0.1:27017");

std::string errMsg;
//conn.createCollection("guang");

BSONObj p = BSONObjBuilder().append("name", "guang").append("age", "26").obj();

std::string dbName = "guang_test";
std::string collection = "testCollection";
std::string tdb = dbName + "."+collection;

conn.insert(tdb, p);

conn.insert(tdb, p);

conn.insert(tdb, p);

//create databases

conn.createCollection("hello");
BSONObj p1 = BSONObjBuilder().append("hello", "p1").obj();

}
void testMongo() {

#ifdef CSG_WIN
WSAData wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
return ;
}
#endif

try
{
run();
CSG_LOG_INFO("conn ok");
}
catch (const mongo::DBException &e)
{
CSG_LOG_INFO("caught " << e.what() );
}
getchar();
return ;
}

删掉CSG_LOG_**,这个是我自己的库的log.
如果win+linux都能正常运行,且mongo里面能读到数据,就说明正常了。

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注