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 127.0.0.1:27017
HINT: Mongo driver connection error:
TL;DR
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.
# ./autogen.sh --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
Investigating
Check Logs of MongoDB
The below logs come out when hit the error of “Mongo driver connection error”.
I NETWORK [listener] connection accepted from 127.0.0.1:55966 #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 127.0.0.1:55966 (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 127.0.0.1:56140 #4 (1 connection now open)
I NETWORK [conn4] received client metadata from 127.0.0.1:56140 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 127.0.0.1:56140
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.

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.
https://docs.mongodb.com/manual/core/authentication-mechanisms/index.html


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 mongo_fdw.so 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 “:”.

Solution
Use the MongoDB’s Meta Driver in mongo_fdw when compiling. And below is the procedure.
# ./autogen.sh --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
分类:PostgreSQL