mongo_fdw HINT: Mongo driver connection error:

Based Environment

  • CentOS Linux release 7.5.1804 (Core)
  • mongo_fdw (Latest commit 5fe371a  on 31 Mar)
  • PostgreSQL 10.5
  • MongoDB 4.0.10 running with auth mode

Issue Description

After following Usage – mongo_fdw, encounter the below error.

ERROR:  could not connect to
HINT:  Mongo driver connection error: 


Use higher version of mongodb’s driver instead of v0.8 driver as following commands. Or start MongoDB as no-auth mode and you could refer this post.

# ./ --with-master
# export PKG_CONFIG_PATH=mongo-c-driver/src/:mongo-c-driver/src/libbson/src
# make -f Makefile.meta
# make -f Makefile.meta install


Check Logs of MongoDB

The below logs come out when hit the error of “Mongo driver connection error”.

I NETWORK  [listener] connection accepted from #3 (1 connection now open)
I ACCESS   [conn3]  authenticate db: admin { authenticate: 1, user: "mongo_user", nonce: "5aa953898e9c3690", key: "a6464137e460629785ee3eba9e43d3cb", $db: "admin" }
I NETWORK  [conn3] end connection (0 connections now open)

Then use “mongo” command to link to MongoDB with user “mongo_user” and its password, in order to get the correct log to be the constrast and to verify user authentication of MongoDB. So checkout the output.

I NETWORK  [listener] connection accepted from #4 (1 connection now open)
I NETWORK  [conn4] received client metadata from conn4: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "4.0.10" }, os: { type: "Linux", name: "CentOS Linux release 7.5.1804 (Core) ", architecture: "x86_64", version: "Kernel 3.10.0-862.el7.x86_64" } }
I ACCESS   [conn4] Successfully authenticated as principal mongo_user on admin from client

Obviously, mongo_fdw miss the last line of “Successfully authenticated” log, which is the key point. And if start MongoDB without auth mode, mongo_fdw will work fine and you’d check this post.

Tcpdump the TCP Stream

Now we’re sure the key point is failed user authentication in our case, but the log of MongoDB doesn’t show failed reasons. So try to dump the TCP stream between mongo_fdw and MongoDB, maybe find some clues.

mongo auth mechanism not specified

OK, it is “Auth mechanism not specified”, which seems handshake negotiation logical in MongoDB’s driver. Maybe the driver’s issue?

Check the Code of MongoDB

Starting in version 4.0, MongoDB removes support for the deprecated MongoDB Challenge-Response (MONGODB-CR) authentication mechanism.
Populate MONGODB-CR in v3.6.1
Populate MONGODB-CR in v3.6.1
If it's empty, then error.
If it’s empty, then error.

Yahaha, the reason is mongo_fdw send authentication information without “mechanism” key-value, and MongoDB complains it as fault since version 4.0.

Why? It must be the v0.8 driver’s question who doesn’t get the furture v4.0 changes. And the solution will be to try the new driver which called MongoDB’s Meta Driver. But I’d like to check the corresponding codes in mongo_fdw to do double check in the next step.

Check the Code of mongo_fdw and driver

# grep -R "Mongo driver connection error" 
mongo_wrapper.c:						errhint("Mongo driver connection error: %d", err)));
mongo_wrapper.c:							errhint("Mongo driver connection error: %s", str)));
mongo_wrapper_meta.c:						errhint("Mongo driver connection error")));
Binary file mongo_wrapper_meta.o matches
Binary file mongo_wrapper.o matches
Binary file matches

There’re two wrapper files, one for v0.8 driver and another for meta driver. From the below screenshoot, we will find out that we used mongo_wrapper.c which depends on v0.8 driver in our case because the error output does contains “:”.

mongo wrapper


Use the MongoDB’s Meta Driver in mongo_fdw when compiling. And below is the procedure.

# ./ --with-master
# export PKG_CONFIG_PATH=mongo-c-driver/src/:mongo-c-driver/src/libbson/src
# make -f Makefile.meta
# make -f Makefile.meta install


Tagged as:


Fill in your details below or click an icon to log in: 徽标

您正在使用您的 账号评论。 注销 /  更改 )

Twitter picture

您正在使用您的 Twitter 账号评论。 注销 /  更改 )

Facebook photo

您正在使用您的 Facebook 账号评论。 注销 /  更改 )

Connecting to %s

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理