1. 前置条件:

  • vmware centos 环境下
  • docker 已经安装完成
  • docker 服务已经启动

2. 拉取mongo的docker镜像

[root@laoliu home]# docker pull mongo
Using default tag: latest
Trying to pull repository docker.io/library/mongo ... 
latest: Pulling from docker.io/library/mongo
35b42117c431: Pull complete 
ad9c569a8d98: Pull complete 
293b44f45162: Pull complete 
0c175077525d: Pull complete 
4e73525b52ba: Pull complete 
a22695a3f5e9: Pull complete 
420eb4b7be5d: Pull complete 
017ec49b70bf: Pull complete 
26470656e2db: Pull complete 
6fec56c7382a: Pull complete 
12f574d1345c: Pull complete 
7102859c924d: Pull complete 
555c1275dd6f: Pull complete 
Digest: sha256:01dc9fb0b7aae875678047e2d8550beb6fc34b7e76c60a1e7d7048f6700dead0
Status: Downloaded newer image for docker.io/mongo:latest
[root@laoliu home]# 

3. 预先配置

[root@laoliu ~]# mkdir -p /home/project/mongo/{data,conf}
[root@laoliu ~]# cd /home/project/mongo/
[root@laoliu mongo]# ll
total 0
drwxr-xr-x. 2 root root 6 Mar 10 10:21 conf
drwxr-xr-x. 2 root root 6 Mar 10 10:21 data

4. 直接跑起mongo

[root@laoliu ~]# docker run -idt --name mongo --privileged=true -p 27017:27017 -v /home/mongo/data:/data/db -v /home/mongo/conf:/data/configdb docker.io/mongo:latest --auth
0a90039fa6520f500596b361adc2411914dc68dcb777871666ee0168b6e66525
[root@laoliu ~]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                               NAMES
0a90039fa652        docker.io/mongo:latest   "docker-entrypoint..."   7 seconds ago       Up 5 seconds        0.0.0.0:27017->27017/tcp            mongo
[root@laoliu ~]# 

5. 配置用户名和密码

[root@laoliu ~]# docker exec -it 0a90039fa652 mongo admin
MongoDB shell version v4.0.10
connecting to: mongodb://127.0.0.1:27017/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c813771d-bf81-4b43-84b5-6b6726ea0b9a") }
MongoDB server version: 4.0.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
> use admin
switched to db admin

> db.createUser({user: 'admin', pwd: 'admin123456', roles: [{role: "userAdminAnyDatabase", db: "admin" }]});
# 以下为结果输出
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		}
	]
}
> db.auth('admin', 'admin123456')
1
>

6. 验证远程登录

6.1 服务器开放端口

  • 本次安装验证是在华为云服务器上, 在华为云控制台开放27017端口即可, 过程略过.

6.2 使用Robo 3T作为可视化界面验证登录

robo3T 官网下载
在这里插入图片描述
在这里插入图片描述
验证登录成功.

后期学习怎么使用mongodb的使用

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐