跳转到内容

MongoDB Atlas

此内容尚不支持你的语言。

Everything this server cannot lose lives in MongoDB: end-user accounts, projects, clients, sessions, the append-only audit trail, and the signing keys. A managed instance is probably the shortest path to having that backed up. The one thing that must not live there is the keyring that encrypts the stored secrets.

  1. Create a cluster in Atlas, in the region closest to where the server runs.

  2. Create a database user with read/write access, and copy the connection string Atlas offers for it. It looks like:

    mongodb+srv://user:password@cluster.example.mongodb.net/?retryWrites=true&w=majority
  3. Allow the application’s egress addresses under Network Access. See the note below.

  4. Set the two variables. Put the connection string in MONGODB_URI and the database name, separately, in DATABASE_NAME:

    Terminal window
    fly secrets set MONGODB_URI='mongodb+srv://user:password@cluster.example.mongodb.net/?retryWrites=true&w=majority'
    DATABASE_NAME=OAuth
  5. Provision the schema against the cluster, once, before the server serves traffic:

    Terminal window
    MONGODB_URI='mongodb+srv://…' DATABASE_NAME=OAuth ISSUER=https://auth.example.com bun run db:setup

    On an upgrade, follow it with bun run db:migrate: the server refuses to start against a database whose migrations have not run. On Fly both are the release_command and you do not run them by hand; on Kubernetes they are the provisioning Job; under Compose they are the setup service.

Atlas denies every address by default. Add the addresses the server’s outbound traffic comes from:

  • Fly.io: Machines egress from shared addresses that are not stable per app. Either use Atlas Private Endpoints / peering, or allow the outbound addresses Fly documents for your region. If you fall back to 0.0.0.0/0 (every address), the database user’s password is then all that protects the data, so it must be long, unique, and rotated when anyone with a copy leaves.
  • Kubernetes: allow the cluster’s NAT gateway addresses.
  • A single VM: allow its public address.

Prefer a private connection over an allow-list wherever the platform offers one.

Nothing but bun run db:setup should be creating structure in this database:

  • one collection per storage area the server writes to;
  • the TTL indexes that expire tokens, authorization codes, sessions, interactions and verification challenges. Atlas runs the TTL monitor, so expiry is the database’s job once the index exists. A deployment whose indexes were never created accumulates dead documents forever;
  • the unique e-mail index on each project’s end-user collection;
  • the initial RS256 signing key;
  • the admin panel seed (the reserved “Administrators” project, the admin-panel and admin-mcp clients, the System team, the default redfox project).

It also drops stale expiry indexes on collections that no longer write the field they indexed. It is idempotent, and it exits non-zero when a constraint it declares is not in force, for example a unique index it could not build because the existing data violates it. Treat that exit code as a failed deploy.

The server encrypts the secrets it stores, such as application secrets and private signing keys, with a keyring that you hold outside the database. The keyring covers what it encrypts, how to supply it, how to rotate it and what to do if a key is lost.

For Atlas, the point is where the keyring lives. Atlas holds the database and its snapshots, so the keyring must live somewhere else: in your platform’s secrets, supplied as KEYRING or as a keyring file at KEYRING_FILE. Never store it in the cluster. Someone who reads the cluster or a snapshot then sees the encrypted secrets, not the secrets themselves.

Atlas takes scheduled snapshots. Enable them, set a retention period, and then do the part people usually skip: restore one into a throwaway cluster once, so you know the restore works and how long it takes.

Three things about a restore are worth knowing. The signing keys come back with the snapshot, so tokens issued before it still verify; restoring a database without them would invalidate every live token they signed. The keyring does not come back with it, because it is not in the database. The restored server needs a keyring that holds every key in use when the snapshot was taken, or the secrets in it cannot be opened. And the audit trail is append-only by design, with no update or delete exposed anywhere in the API. A restore that rolls it back is the only way an entry disappears, which is probably worth stating in whatever your compliance story is.