1.使用docker 进行拉取
docker pull mongo:latest
2.进行启动
docker run --name mongo -p 27017:27017 -v /data/home/hfg/mongodb/data:/data/db -v /data/home/hfg/mongodb/backup:/data/backup -d mongo --auth
- 包含两个映射地址,一个是数据,一个是备份
- 如图所示为启动成功
hfg@hfg-PC:~$ docker exec -it mongo mongo admin
MongoDB shell version v5.0.5
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("908bc9c5-1cae-4a04-a379-66f8599fd944") }
MongoDB server version: 5.0.5
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
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
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
docker exec -it mongo mongo admin # 进入容器
db.createUser({ user:'root',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]}); # 创建一个名为 root,密码为 123456 的用户
db.auth('root', '123456') # 并且测试链接
## 一定要先选则库在进行创建用户
use PersonInfo
db.createUser({user:'test',pwd:'123456',roles:[{role:'readWrite',db:'PersonInfo'}]})
spring boot集成时候用到
spring:
data:
mongodb:
uri: mongodb://test:123456@localhost/PersonInfo
3下载工具进行链接
Liunx下载地址:点击下载
其他版本点击这里:跳转