Docker19.03.13安装MongoDB:laster
一、拉取镜像
[root@localhost ~]# sudo docker pull mongo:latest
latest: Pulling from library/mongo
171857c49d0f: Pull complete
419640447d26: Pull complete
61e52f862619: Pull complete
892787ca4521: Pull complete
06e2d54757a5: Pull complete
e2f7d90822f3: Pull complete
f518d3776320: Pull complete
feb8e9d469d8: Pull complete
69705b632494: Pull complete
c7daea26376d: Pull complete
13d1f9e1fc77: Pull complete
f87e65fe7ffd: Pull complete
Digest: sha256:a4448eb5f6e6097353d0ab97eb50aeb0238bb4e60c37e401920d3c2c4fc73eb9
Status: Downloaded newer image for mongo:latest
docker.io/library/mongo:latest
二、创建容器
[root@localhost ~]# sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mongo latest ba0c2ff8d362 11 days ago 492MB
[root@localhost ~]# docker run -itd --name mongo -p 27017:27017 mongo --auth
72edb188bdb4748f656b1d689eca7d6e94adcafad453f778f021dd0df1ad1e14
在上面的命令中,几个命令参数的详细解释如下:
- -p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机 ip:27017 访问到 mongo 的服务。
- --auth:需要密码才能访问容器服务
三、创建用户
1、进入mongodb容器
[root@localhost ~]# docker exec -it mongo mongo admin
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e6f2fd2b-0d9f-46e9-9d65-062ed487ed88") }
MongoDB server version: 4.4.1
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
---
The server generated these startup warnings when booting:
2020-10-07T16:01:12.931+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2020-10-07T16:01:12.931+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
2020-10-07T16:01:12.931+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>
2、创建用户信息
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
"readWriteAnyDatabase"
]
}
3、切换用户信息
> db.auth('admin', '123456')
1
四、开放端口号
进入宿主机,开放端口号
[root@localhost ~]# firewall-cmd --zone=public --add-port=27017/tcp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
五、远程连接测试
使用客户端工具Studio 3T连接mongo,下载地址https://studio3t.com/download/
六、删除镜像
[root@localhost ~]# docker stop mongo
mongodb
[root@localhost ~]# docker rm mongo
mongodb
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES