MongoDB Notes

1 minute read

My notes on installing, running and using MongoDB

Install and Basic Setup

$ brew tap mongodb/brew
$ brew update
$ brew install mongodb-community@8.0

To have launchd start mongodb now and restart at login:

$ brew services restart mongodb-community@8.0

⚠️ Mind the “re“start, it avoids some strange problems 🤷‍♂️

> use admin
switched to db admin
> db.createUser(
...   {
...     user: "admin",
...     pwd: "admin",
...     roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
...   }
... )
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}

Edit /usr/local/etc/mongod.conf and add:

security:
  authorization: enabled

Restart using:

$ launchctl stop homebrew.mxcl.mongodb
$ launchctl start homebrew.mxcl.mongodb

Create new database + user:

> use admin
switched to db admin
> db.auth("admin", "admin" )
> use project
switched to db project
> db.createUser(
...   {
...     user: "c3po",
...     pwd: "c3po",
...     roles: [ { role: "readWrite", db: "c3po" } ]
...   }
...)
Successfully added user: {
	"user" : "c3po",
	"roles" : [
		{
			"role" : "readWrite",
			"db" : "c3po"
		}
	]
}
...
> use c3po
switched to db c3po
> db.auth("c3po", "c3po")
1

References: