Prerequisite: docker is installed on mac
1. Pull the latest MongoDB mirror
docker pull mongo:latest
2. View the mirror
docker images
3. Run the mongo container
docker run -itd --name mongo -p 27017:27017 mongo --auth
prompt:
- docker run: Create a new container and run a command
- -i: Run the container in interactive mode, usually used together with -t;
- -t: Reallocate a pseudo input terminal for the container, usually used at the same time as -i;
- -d: Run the container in the background and return the container ID;
- --Name="nginx-lb": Specify a name for the container;
- -p: Specify port mapping, the format is: host (host) port: container port
- --Auth: enable mongo container password authentication
mongodb installed successfully
4. Enter the mongo container
docker exec -it mongo mongo admin
prompt:
- docker exec: execute commands in the running container
- -i: Keep STDIN open even if it is not attached
- -t: allocate a pseudo terminal
5. Create an administrator account and password
The account and password here are set to admin
db.createUser({ user:'admin',pwd:'admin',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]});
The user in mongodb is based on the identity role, and the role of the administrator account is userAdminAnyDatabase. 'userAdmin' stands for user management identity,'AnyDatabase' stands for can manage any database.
6. Log in with the created account
db.auth('admin', 'admin')
After the administrator account is logged in, you can use this account to create other database administrator accounts
use yourdatabase
db.createUser({ user: "youruser", pwd: "yourpassword", roles: [{ role: "dbOwner", db: "yourdatabase" }] })
The built-in roles of role are as follows:
- Read: Allow users to read the specified database
- readWrite: Allow users to read and write the specified database
- dbAdmin: Allows users to perform management functions in the specified database, such as index creation, deletion, viewing statistics or accessing system.profile
- userAdmin: Allow users to write to the system.users collection, and create, delete and manage users in the specified database
- clusterAdmin: It is only available in the admin database, giving users the management authority of all shards and replication set related functions.
- readAnyDatabase: only available in the admin database, giving users read permissions for all databases
- readWriteAnyDatabase: only available in the admin database, giving the user read and write permissions for all databases
- userAdminAnyDatabase: only available in the admin database, giving the user userAdmin permissions for all databases
- dbAdminAnyDatabase: Only available in the admin database, grants the user dbAdmin permissions for all databases.
- root: Only available in the admin database. Super account, super authority
MongoDB GUI management tool: Robo 3T
https://robomongo.org/download
MongoDB backup and recovery
- Backup method:
docker cp
mongodump
- Recovery method:
docker cp
mongorestore
https://www.runoob.com/mongodb/mongodb-mongodump-mongorestore.html