Skip to main content

MongoDB

How to run MongoDB in CGC from the official image, how to set its root credentials and how to reach it from inside your namespace.

MongoDB is listed by cgc db create --help, but it cannot be created that way: the licence does not allow CGC to serve it as a single click app. cgc db create mongodb stops with a message pointing you to a custom image and creates nothing. Run MongoDB yourself with cgc compute create custom.

How to run it

Create a volume for the data directory and start the instance from the official image with the root credentials set:

cgc volume create -s 5 -sc <storage_class> mongo01-vol
cgc compute create custom -n mongo01 -c 1 -m 2 --image mongo:7 -v mongo01-vol -fp /data/db -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=<your_password>

The image starts mongod --auth --bind_ip_all: the server listens on port 27017 and keeps its data in /data/db, which is why -fp /data/db is the mount path to use.

Always mount that volume. Without it the data sits on the pod's ephemeral disk and is lost on every restart, not only when you delete the app: after cgc resource restart mongo01 an instance created without -v comes back with an empty database.

With the volume mounted the data survives both cgc resource restart mongo01 and deleting the app and creating it again with the same volume and the same -fp /data/db. cgc compute delete removes the app together with its service, so after recreating it you have to add the port again.

Default configuration

  • MONGO_INITDB_ROOT_USERNAME: user created on the first start of an empty data directory. Set it to admin or any name you like.
  • MONGO_INITDB_ROOT_PASSWORD: password for that user. If you skip both variables, the image starts mongod without --auth and every client in the namespace is accepted without authentication.
  • JUPYTER_PORT=8888 and JUPYTER_TOKEN: CGC adds these two variables to every custom app. MongoDB ignores them.
info

The -ce app_token= flag sets the CGC app token of the app, the value shown in cgc compute list -d, not the database password. MongoDB reads its password from MONGO_INITDB_ROOT_PASSWORD, so set the password there; authenticating with the app token fails with Authentication failed.

How to connect

A custom app has no service until you publish a port, and until then its name does not resolve inside the namespace: mongosh stops with getaddrinfo ENOTFOUND mongo01. Add the MongoDB port first:

cgc compute port add mongo01 -p 27017 -n mongo

The app is then reachable from any other app in your namespace as mongo01:27017:

mongosh "mongodb://admin:<your_password>@mongo01:27017/admin"

Without credentials the server answers Command listDatabases requires authentication.

caution

Adding a port also registers a public ingress address for it, and in CLI 1.5.1 the -ni/--no-ingress flag does not prevent that: the port is still listed with ingress True. Always give MongoDB a strong root password, and check the result with cgc compute port list mongo01.