Creating database and users automatically

Hi!

am taking rocketchat example (Running Rocket.Chat with Percona Server for MongoDB on Kubernetes - Percona Database Performance Blog) as base for my current question.
In the rocketchat example, databases and users are created by connecting to the mongo pod and executing mongo-statements manually:

use rocketchat
db.createUser({
  user: "rocketChat",
  pwd: passwordPrompt(),
  roles: [
    { role: "readWrite", db: "rocketchat" }
  ]
})

use admin
db.createUser({
  user: "oplogger",
  pwd: passwordPrompt(),
  roles: [
    { role: "read", db: "local" }
  ]
})

Is there any automatism possibility which can take care of this?
Something like providing client-specific databases and users via YAML (feeding the operator with instructions like creating a database, create users etc) or at least from command line (e.g. having a one-liner for kubectl would also be sufficient. something like providing the mongo statements as arguments to the percona-mongo-client pod)?

Background is, that we are using CICD pipelines and need to automate things like prepopulating databases and users.

Great job btw.

1 Like

Hi @aydink,

The operator doesn’t support user management, it just manages the infrastructure.

Here’s a oneliner to get your started:

kubectl exec ${client_container} -- \
		bash -c "printf 'db.createUser({user: "myApp", pwd: "myPass", roles: [{ db: "myApp", role: "readWrite" }]})\n' | mongo mongodb://${MONGODB_USER_ADMIN_USER}:${MONGODB_USER_ADMIN_PASSWORD}@${cluster}.${namespace}/admin?ssl=false&replicaSet=rs0"
2 Likes

Thanks a lot!

Based on your suggestion, I shortened it to (without the need of an existing client container):

kubectl run -i --rm --tty percona-client1 --image=percona/percona-server-mongodb:4.4.10-11 --restart=Never -- bash -c "printf 'db.createUser({user: \"admin\", pwd: \"secret\", roles: [{ db: \"messages\", role: \"readWrite\" }]})\n' | mongo mongodb://mongoadmin:secret@my-mongo-db/admin"

2 Likes