一:创建挂载目录

在这里插入图片描述

	[root@VM-12-7-centos local]# cd dev/
	[root@VM-12-7-centos dev]# ls 
	elasticsearch  gitlab  java  jenkins
	[root@VM-12-7-centos dev]# mkdir mongodata
	[root@VM-12-7-centos dev]# pwd

二:拉取镜像

 	docker pull mongo:latest

三:运行容器

	docker run -itd --name mongo-data-one -p 8087:27017 -v /usr/local/dev/mongodata:/data/db mongo:latest

	-p: 映射端口;
	-v  MongoDB数据挂载目录;
	镜像名;

四:进入容器创建用户

[root@VM-12-7-centos dev]# docker exec -it  6adae0bedd5d 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("acb05551-0bf9-4d50-be77-73adaf245c0a") }
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
---
The server generated these startup warnings when booting: 
        2022-01-20T09:48:00.025+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2022-01-20T09:48:00.643+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2022-01-20T09:48:00.643+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2022-01-20T09:48:00.643+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()
---
> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'root', db: 'admin'},"readWriteAnyDatabase"]});
Successfully added user: {
	"user" : "admin",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		},
		"readWriteAnyDatabase"
	]
}
> db.auth('admin', '123456')
1
> ^C
bye

进入容器输入mongo命令,创建可操作数据库权限的用户账号和密码

5:Robo3T 连接mongo

除非权限不够,一般都能直接连接,创建数据库和collection
Logo

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

更多推荐