My docker container is running through docker-compose. The init script appears to be creating the user I want to login with because of this log line:
mongo | Successfully added user: { "user" : "newUser", "roles" : [ "readWrite" ] }
mongo | bye
mongo | Error saving history file: FileOpenFailed Unable to open() file /home/mongodb/.dbshell: No such file or directory
mongo | {"t":{"$date":"2021-01-29T13:45:03.397+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn3","msg":"Connection ended","attr":{"remote":"127.0.0.1:60066","connectionId":3,"connectionCount":0}}
However since I'm seeing the "unable to open file /home/mongodb/.dbshell", it appears I cannot write to my data directory. Any idea what I could be doing wrong here?
Here's the docker-compose for mongo:
mongo:
image: mongo:4.4.3
container_name: mongo
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root1
MONGO_INITDB_DATABASE: admin
command: mongod --port 27017
ports:
- '27017:27017'
volumes:
- ./data/mongodb:/data/db
- ./configs/mongodb/mongo-init.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
This is my mongo-init.sh file.
#!/usr/bin/env bash
set -eu
mongo -- "admin" <<EOF
var rootUser = 'root';
var rootPassword = 'root1';
var admin = db.getSiblingDB('admin');
admin.auth(rootUser, rootPassword);
var user = 'newUser';
var passwd = 'newUserPass';
db.createUser({user: user, pwd: passwd, roles: ["readWrite"]});
EOF
I found the problem. There were 2 places I was wrong in my scripts. The MONGO_INITDB_DATABASE should not have been admin in my docker-compose- it should have been the database I was trying to create. The mongo-init.sh file should also have been like so instead:
mongo -- "newUserDB" <<EOF
var admin = db.getSiblingDB('admin');
admin.auth('root', 'root1');
db.createUser({user: 'newUser', pwd: 'newUserPass', roles: [{ role: "readWrite", db: "newUserDB" }]});
EOF
I noticed my user was getting created in the wrong database. The error above was throwing me off. That and the owner of the local volume was systemd-coredump, which is entirely ok because that's just a mapping of user ids that Mongo uses in the container. Nothing to do with an actual core dump. Fewf!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.