um teste do medium

pensei em usar o medium hoje para fazer um teste gostosinho sobre o uso da ferramenta e sem vírgulas mesmo eu vou continuar escrevendo esse texto tanto quanto for possível gostaria de por imagens na…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Definitive Notes On MongoDB and Docker Dev Setup

When a mongodb deployment starts, it starts without any user created.

Wether you create your deployment using (on the machine)

or (containerized)

the deployment starts with 3 databases: admin, config and local.

You can connect to the deployment using mongosh
and see the databases using show dbs

You are supposed to start a deployment and create an admin user in the admin database.

Every user has an authentication database, which he authenticates against when login-in, and is not limited to that database. He can have privileges in other databases.

MongoDB stores all users in the system.userscollection, in the admin database. A query to this collection shows all users and roles, regardless of the database the user belongs to!

In essence, to authenticate to a mongodb deployment, you just need to have a user, a password, and a database to authenticate against. And off course, the user created in that database.

Interesting that permissions can be cross-database. You can have a user in DB1 with permissions to DB2. You authenticate against DB1 to query DB2.
One might ask why would anyone do that? And I’d say: you never know…

To provide a mongodb instance ready to work when the application starts in development, we need to create a user to authenticate to the mongodb instance.
Off course we could manually set it up once, connecting to the DB, creating the user, but we’re senior developers for god’s sake!

So what we have to do is use an init script. A query that will run once when the deployment is created, to add our users to the database.
Notice the volume:

The mongo docker image runs everything inside the /docker-entrypoint-initdb.d directory. Notice that this directory needs to be in the container’s filesystem once the container starts!

The init script creates a user with readWrite permissions to the test DB.

And then we use the following connection string to authenticate:

using authSource to specify the database we are authenticating against. By default, it uses the admin database.

docker-compose.yaml

init-mongo.js

Sources

Add a comment

Related posts:

Buy Old Gmail Accounts

Many of us have old Gmail accounts that we no longer use. Maybe you created one when you first got started with email. But now you have a different account that you use for work or personal email…

The Power of Thank You Notes

I like to write thank you notes. Appreciation and gratitude make you happier. That’s what the research says, and that’s what made me start writing thank you notes. I’ve written thank you notes to…

How the EOS Referendum System works

The EOS Referendum system is live, and for the first time ever, token holders have the opportunity to directly influence the direction of the network. After months of hard work by members of the…