This article doesn’t have any deep technical points, but it’s just a note for me as a beginner of Mongo (I did it for 2 days), hoping to help other beginners quickly deploy Mongo.

If you don’t want to read the text of this article, follow the instructions in order to complete the installation and deployment, and finally achieve the “root” account in Mongo.

Install the mongo

docker pull mongo
Copy the code

Initialize the container

docker run -d -p 27017:27017 -v /data/mongo/database:/data --name mongo mongo --auth
Copy the code

Parameter Description:

-p: maps the docker port in the container to the real server. Syntax: Server port: port in the container

-v: maps the docker storage directory in the container to the real server directory. The syntax is as follows: server real folder: directory in the container

–name: create a name for the container, so that docker exec can use this name directly.

— Auth: by default, mongo can be logged in without a user password. Only when auth is enabled can you ensure that only authorized accounts can be logged in.

Enter the Mongo container

docker exec -it mongo /bin/bash
Copy the code

Create account

  1. First open the mongo
mongo
Copy the code
  1. After entering the mongo command line, you first switch to the admin library, which exists by default.
use admin
Copy the code
  1. After entering the admin library, you can start creating accounts.
db.createUser({
   user: "root".pwd: "xxxxxx".roles: [{role: "userAdminAnyDatabase".db: "admin"}]})Copy the code

Create user root, password ‘XXXXXX ‘, role’ can ‘manage’ all database administrator ‘, note that ‘manage’ does not include ‘add, delete, change, query ‘, so please continue to read.

Authorize other libraries

We have just given user “root” the permission to manage all databases, but not “add, delete, alter, query “. We have given user “root” the permission to control “xxxDB”, which is corresponding to “dbOwner”.

db.grantRolesToUser('root'[{role:'dbOwner'.db:'xxxDB'}])
Copy the code

Exit command (exit)

The commands to exit mongo and exit the container are:

exit
Copy the code