docker 部署MongoDB

1.Docker搭建Mongodb

docker pull mongo

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

参数说明:

-p 27017:27017 :映射容器服务的 27017 端口到宿主机的 27017 端口。外部可以直接通过 宿主机 ip:27017 访问到 mongo 的服务。
–auth:需要密码才能访问容器服务。

2.创建用户

[root@bogon ~]# docker exec -it mongo mongosh admin
Current Mongosh Log ID: 63773f1fdc7ccad4c2494295
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:          6.0.3
Using Mongosh:          1.6.0

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

test> use admin
switched to db admin
admin> db.createUser({ user:'admin',pwd:'123456',roles:[ { role:'userAdminAnyDatabase', db: 'admin'},"readWriteAnyDatabase"]});
{ ok: 1 }
admin> db.auth('admin', '123456')
{ ok: 1 }

超级管理员用户

db.createUser(
	{
		user:"root",
		pwd:"123456",
		roles:[{role:"root",db:"admin"}]
	}
);

3.创建数据库

MongoDB 创建数据库的语法格式如下:use DATABASE_NAME
如果数据库不存在,则创建数据库,否则切换到指定数据库。
可以看到,我们刚创建的数据库 并不在数据库的列表中, 要显示它,我们需要向 数据库插入一些数据。

admin> show dbs
admin   132.00 KiB
config  108.00 KiB
local    40.00 KiB
mongo    40.00 KiB
admin> use db01
switched to db db01
db01> db.db01.insert({"name":"mongodb中文网"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("637741c1cfced0251ff923a5") }
}
db01> show dbs
admin   132.00 KiB
config  108.00 KiB
db01      8.00 KiB
local    40.00 KiB
mongo    40.00 KiB

参考

https://www.mongodb.org.cn/tutorial/8.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liuyunshengsir

微信:lys20191020

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值