Docker安装Mongo

本文介绍了如何使用Docker下载并运行MongoDB容器,然后在容器内创建用户和设置权限,确保数据库的安全访问。通过`docker pull mongo`拉取镜像,`docker run`启动容器,并使用`docker exec`进入容器设置账号和密码。最后,演示了两种方式(docker命令和mongo命令)来验证用户身份并访问数据库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、下载镜像

% docker pull mongo

二、创建容器

% docker run -itd --name mongo -p 27017:27017 mongo --auth

三、设置账号和密码

% docker exec -it mongo mongo admin
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("6db25575-f0fc-4312-90b3-d46971d9bd37") }
MongoDB server version: 5.0.3
================
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.
We recommend you begin using "mongosh".
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:'pony',pwd:'666',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
Successfully added user: {
	"user" : "pony",
	"roles" : [
		{
			"role" : "userAdminAnyDatabase",
			"db" : "admin"
		},
		"readWriteAnyDatabase"
	]
}

四、访问容器

1 docker命令形式

% docker exec -it mongo mongo pony
> db.auth('pony', '666')

2 mongo命令形式

% mongo 127.0.0.1:27017/admin -u pony -p 666 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值