Để khởi tạo Mongo với các tập lệnh Triple và initDB của người dùng ban đầu-DB chỉ với một docker-compose.yml, không có bất kỳ cấu hình bổ sung nào, bạn có thể sử dụng hình ảnh bitnami/Mongo.
Trong trường hợp của tôi, tôi đã không chạy các tập lệnh của mình theo thư mục /Docker-entrypoint-initdb.d trong container sau khi đặt các biến môi trường; MongoDB_username và MongoDB_Password [các biến env cụ thể cho hình ảnh bitnami] vì Mongod chạy với tùy chọn --Auth tự động khi bạn đặt các biến này. Do đó, tôi đã gặp lỗi xác thực khi container đang trong quá trình thực thi các tập lệnh./docker-entrypoint-initdb.d directory in the container after setting environment variables; MONGODB_USERNAME and MONGODB_PASSWORD [specific env variables for bitnami image] because mongod runs with --auth option automatically when you set these variables. Consequently, I got authentication errors when the container was in the process of executing the scripts.
Bởi vì, nó đã kết nối với: MongoDB: //192.168.192.2: 27017/máy nén
Nhật ký đầu cuối của lỗi
Tệp docker đầu tiên:
version: "3"
services:
mongodb:
container_name: mongodb
image: 'docker.io/bitnami/mongodb:4.2-debian-10'
ports:
- "27017:27017"
volumes:
- "mongodb_data:/bitnami/mongodb"
- "./mongodb/scripts:/docker-entrypoint-initdb.d"
environment:
- MONGODB_INITSCRIPTS_DIR=/docker-entrypoint-initdb.d
- MONGODB_USERNAME=some_username
- MONGODB_PASSWORD=some_password
- MONGODB_DATABASE=some_db_name
networks:
backend:
restart: unless-stopped
volumes:
mongodb_data:
networks:
backend:
driver: bridge
Tệp JS JS dưới ./mongodb/scripts đường dẫn:
let db = connect["localhost:27017/some_db_name"];
db.auth["some_username", "some_password"];
let collections = db.getCollectionNames[];
let storeFound = false;
let index;
for[index=0; index