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 选项

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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里面能读到数据,就说明正常了。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值