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.
-
Create a cluster in Atlas, in the region closest to where the server runs.
-
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 -
Allow the application’s egress addresses under Network Access. See the note below.
-
Set the two variables. Put the connection string in
MONGODB_URIand the database name, separately, inDATABASE_NAME:Terminal window fly secrets set MONGODB_URI='mongodb+srv://user:password@cluster.example.mongodb.net/?retryWrites=true&w=majority'DATABASE_NAME=OAuth -
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:setupOn 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 therelease_commandand you do not run them by hand; on Kubernetes they are the provisioning Job; under Compose they are thesetupservice.
Network access
Section titled “Network access”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.
What provisioning creates
Section titled “What provisioning creates”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-panelandadmin-mcpclients, the System team, the defaultredfoxproject).
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.
Keyring
Section titled “Keyring”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.
Backups, and what a restore brings back
Section titled “Backups, and what a restore brings back”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.