基于Ubuntu的MongoDB Docker镜像中mongodump SASL认证报错,需添加依赖?
Why This Happens
That error pops up because the mongodump tool in your current Docker image doesn't have SASL (Simple Authentication and Security Layer) support compiled in, which is required for using the SCRAM-SHA-1 authentication mechanism. Also, a quick heads-up: the mongodb-10gen package you're using is for older MongoDB 2.x versions, and SCRAM-SHA-1 was only introduced in MongoDB 3.0. So you'll want to make sure your MongoDB server is running a version that actually supports this auth method too.
How to Fix It
1. Add SASL Dependencies to Your Dockerfile
Modify your Dockerfile to install SASL-related system packages before or alongside MongoDB. These libraries will add the necessary SASL support to MongoDB client tools like mongodump:
FROM ubuntu RUN apt-get update RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list RUN apt-get update # Install SASL libraries to enable auth support RUN apt-get install -y libsasl2-dev libsasl2-modules RUN apt-get install -y mongodb-10gen RUN mkdir -p /data/db EXPOSE 27017 CMD ["--port", "27017", "--smallfiles"] # Split args properly for correct parsing ENTRYPOINT ["/usr/bin/mongod"] # Fixed path (missing leading /) and use exec form
Note: I fixed two issues in your original Dockerfile:
- The
ENTRYPOINTpath was missing a leading/(should be/usr/bin/mongodinstead ofusr/bin/mongod)- Split the
CMDarguments into separate strings (Docker requires this to parse commands correctly)
2. Consider Upgrading to an Official MongoDB Image
If your MongoDB server is running 3.0 or newer, it's way more reliable to use the official MongoDB Docker image instead of the old mongodb-10gen package. Official images come pre-configured with all necessary dependencies (including SASL support) and are actively maintained:
# Example: Use official MongoDB 3.6 image (match your server version) FROM mongo:3.6 RUN mkdir -p /data/db EXPOSE 27017 CMD ["--port", "27017", "--smallfiles"]
3. Fix Your mongodump Command
Your original mongodump command has a syntax error with the port parameter—use --port (double dash) instead of -port. Also, --out expects a directory (not a JSON file), since mongodump writes multiple backup files to that location:
mongodump --host *.*.*.* --port 27017 -u user -p pass --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-1 --out /tmp/backup/mongodump
How to Verify
- Rebuild your Docker image:
docker build -t my-custom-mongo . - Start the container:
docker run -d my-custom-mongo - Enter the container's bash shell:
docker exec -it <your-container-id> bash - Run the corrected
mongodumpcommand—you shouldn't hit the SASL error anymore.
内容的提问来源于stack exchange,提问作者Rudziankoŭ

