docker-compose 安装MongoDB


本文演示使用docker-compose安装MongoDB。

1. docker-compose.yaml

version: "3.8"
services:
  mongodb:
    container_name: mongodb
    image: mongo:6.0
    ports:
      - 27017:27017
    restart: "no"
    volumes:
      - $PWD/data:/data/db
      - $PWD/logs:/var/log/mongodb

使用上面的文件,使用如下的指令,即可启动一个MongoDB的环境。

docker-compose up -d 

启动完之后,便可以进入到容器内部,登录进入使用数据库。

docker exec -it mongodb bash
root@b2dab6bc8278:/# mongosh
Current Mongosh Log ID:	637100eaec11c38ba97a410b
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:		6.0.2
Using Mongosh:		1.6.0

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

------
   The server generated these startup warnings when booting
   2022-11-13T14:36:17.294+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2022-11-13T14:36:17.294+00:00: vm.max_map_count is too low
------

------
   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()
------

test> dbs;
ReferenceError: dbs is not defined
test> show dbs;
admin   180.00 KiB
config   72.00 KiB
local    72.00 KiB
test>

可以看到,我没有输入用户名和密码已经进入到数据库中来了。而且也提示我们没有enable访问控制。
一般来说,我们在生产环境中使用MongoDB,会自定义配置文件,然后指定配置文件即可。可以在配置文件中设置很多不同内容。也可以在启动指令中通过–auth来激活访问控制。下面我使用另一种方式,即在配置用户名和密码在docker-compose.yaml文件中。

2.带用户名和密码的docker-compose.yaml

version: "3.8"
services:
  mongodb:
    container_name: mongodb
    image: mongo:6.0
    ports:
      - 27017:27017
    restart: "no"
    volumes:
      - $PWD/data:/data/db
      - $PWD/logs:/var/log/mongodb
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: 123456

由于环境变量中有MONGO_INITDB_ROOT_USERNAME和MONGO_INITDB_ROOT_PASSWORD两个值,因此便会自动启用访问控制,即–auth。这个时候可以使用用户名和密码进行登录。

docker exec -it mongodb bash
root@3e619f91d664:/# mongosh -u root -p 123456
Current Mongosh Log ID:	63710cd85b75cc2b28a6bec3
Connecting to:		mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
Using MongoDB:		6.0.2
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.

------
   The server generated these startup warnings when booting
   2022-11-13T15:27:06.865+00:00: vm.max_map_count is too low
------

------
   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()
------

test> show dbs;
admin   100.00 KiB
config   12.00 KiB
local    40.00 KiB
test>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值