So back in the shell where I did create one user already, let's create another user
and please remember I am logged in with my user we created earlier.
Now I will log out or simply quit the mongo server because I want to show you an alternative way
of logging in and that is that you type mongo then -u, then your username -p and your password
and now don't hit enter because this would actually fail
and the reason for this is this you create a user in a database part I mentioned
and we created our single user we have right now in the admin database.
Now we need to specify which database we did create a user in and we can do this with the help of
let's see, with --help, with this command, with the authentication database here.
So we need to add this command,
so let's repeat this.
I have -u Max -p max and then --authenticationDatabase and now it's admin
and now if you hit enter, this should succeed and you are now already logged in
so you don't need to run db auth again.
So now this is another way of authenticating and the authentication database part here is really important.
Now we want to create more users and I will first of all switch to the shop database and create a new user
on this shop database with create user, then pass a document, define a username, I'll name it appdev,
define a password with pwd and I'll name that dev,
of course these values are totally up to you
and then add roles and you can add multiple roles,
I'll only add one
and that will be readWrite. Now
please note I'm in the shop database because I switch to it.
Now if I hit enter, this works and I have a new user.
We can now authenticate into that user with db auth, appdev is the username
and dev is the password. If I hit enter, the one signals that
this works
and now let's try working with a collection.
So here in the products collection, let's try to insert one element with the name of
book.
If I hit enter, I get an error that
too many users are authenticated because actually, I did authenticate into my new user but I was still
authenticated in my old user. Now to mitigate this, we should have run log out before switching the user.
Now one of the quickest ways to fix this is simply to quit the
mongo shell and then restart the mongo shell.
Now important though, I want to log in with a different user right from the start
and I can do this by of course switching my username here, appdev
and also here, the password
and now the authentication database also has to change because we created the new user on the shop, so
I have to authenticate against shop.
If I would try admin here, it would actually fail, against shop
it will succeed.
So now in there, let's try accessing our products and insert one new product with name, a book.
If I hit enter, I get an error
and do you know why? Well I'm in the wrong database,
I did not switch to my shop database first, so we need to use the shop first before we try this command
and now it will actually work as you can see.
So this is important,
the readWrite role was assigned to this user but since the user is part or was created on the shop
database, the readWrite role by default also only gave us readWrite access to that shop database.
Now this is one thing that assigning a user to a database does to you out of the box, you, any role you
assign just like that to the user is then scoped to that database except for any database roles you
saw on the previous slides
and this of course gives us as an administrator a lot of flexibility.
We can really create the users who are assigned to the databases they need to work with and we can assign
them the roles they need, so that the users have to log in against the databases they should work with
and they only have the role to work with that database then and this is often exactly what you want and
the users don't even know the other databases even and in a big corporation with a lot of databases,
this is typically the set up you want to have.
Now of course you can still have the scenario that a user needs to be able to work with two databases
and not just the one he authenticates against.
And let's see how we can do this in the next lecture.