Initializing with new users via a js initdb script

355 Views Asked by At

I created a mongo docker container with an additional js script:

ADD graylog.js /docker-entrypoint-initdb.d/graylog.js

The script creates two users:

root = db.getSiblingDB('root');
root.createUser(
  {
    user: "root",
    pwd: "pass",
    roles: [
      { role: "dbOwner", db: "admin" }
    ]
  }
);

graylog = db.getSiblingDB('graylog');
graylog.createUser(
  {
    user: "graylog",
    pwd: "vWGzncmBe9",
    roles: [
      { role: "dbOwner", db: "graylog" }
    ]
  }
);

Unfortunately, even with just one user (the first one), connecting the db and listing the available databases doesn't work:

client = MongoClient('mongodb://root:pass@localhost:27017/')
client.list_database_names()

Authentication failed

And indeed in the mongo server logs, I see that root could not log in. graylog doesn't either with graylog server.

I did this with a brand new/clean volume, and I can confirm that setting MONGO_INITDB_ROOT_USERNAME/MONGO_INITDB_ROOT_PASSWORD does allow listing the databases.

The documentation on how to do this is not very clear. mongoldb 3 could allow bth environment variables and a script, but now, only one of them works, and only if mongo is not initialized.

So how can I set users from this script?

Edit:

Adding the mongo starting logs:

2019-03-13T20:01:03.891+0000 I SHARDING [initandlisten] Marking collection local.system.replset as collection version: <unsharded>
2019-03-13T20:01:03.892+0000 I SHARDING [initandlisten] Marking collection admin.system.roles as collection version: <unsharded>
2019-03-13T20:01:03.892+0000 I SHARDING [initandlisten] Marking collection admin.system.version as collection version: <unsharded>
2019-03-13T20:01:03.893+0000 I SHARDING [initandlisten] Marking collection local.startup_log as collection version: <unsharded>
2019-03-13T20:01:03.893+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-03-13T20:01:03.894+0000 I SHARDING [initandlisten] Marking collection admin.system.users as collection version: <unsharded>
2019-03-13T20:01:03.894+0000 I SHARDING [initandlisten] Marking collection config.system.sessions as collection version: <unsharded>

No indication of the additional users :( I wonder if I should do these manually afterwards!

0

There are 0 best solutions below