(base) rootyx@rootyx-Default-string:~$ sudo docker pull mongo:4.4
4.4: Pulling from library/mongo
99803d4b97f3: Pull complete
6653ceb22977: Pull complete
e067c5774dde: Pull complete
3f2512535c9b: Pull complete
95823dd64119: Pull complete
77aa83f4fcd4: Pull complete
7db43fff6317: Pull complete
72d65367ffef: Pull complete
45c0eedeea8c: Pull complete
Digest: sha256:967fecde146a0d1ac5ec5805a2c759066b6b94d6fb3977e22292c1d58df244e3
Status: Downloaded newer image for mongo:4.4
docker.io/library/mongo:4.4
rootyx@rootyx-Default-string:~$ sudo mkdir -p /docker_volume/mongodb/data
(base) rootyx@rootyx-Default-string:~$
rootyx@rootyx-Default-string:~$ sudo docker run -itd --name mongo -v /docker_volume/mongodb/data:/data/db -p 27017:27017 mongo:4.4 --auth
4d460feed6b0b98e0dae2847a3f4be44bbc1def61380b7ee104077d6edb496e8
(base) rootyx@rootyx-Default-string:~$ sudo docker exec -it mongo mongo admin
MongoDB shell version v4.4.22
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“a8ef194a-478a-4114-a1c2-e336e38d1b03”) }
MongoDB server version: 4.4.22
Welcome to the MongoDB shell.
For interactive help, type “help”.
For more comprehensive documentation, see
https://docs.mongodb.com/ Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},'readWriteAnyDatabase']});
Successfully added user: {
“user” : “root”,
“roles” : [
{
“role” : “userAdminAnyDatabase”,
“db” : “admin”
},
“readWriteAnyDatabase”
]
}
db.auth('root', '123456')
1
db.user.insert({"name":"zhangsan","age":18})
WriteResult({ “nInserted” : 1 })
db.user.find()
{ “_id” : ObjectId(“647839b321ee2ac55df3a589”), “name” : “zhangsan”, “age” : 18 }
^C
bye
(base) rootyx@rootyx-Default-st