【问题标题】:Creating mongodb database and populating it with some data via docker-compose创建 mongodb 数据库并通过 docker-compose 填充一些数据
【发布时间】:2023-04-10 21:12:01
【问题描述】:

我有以下脚本,其中指定了自定义数据库,但我没有看到在 GUI(指南针)中创建数据库用户。我只看到 3 个默认数据库(管理员、配置、本地)。

我已经查看了这个linked 的答案,但我需要一个具体的答案来回答我的问题。

mongo:
    image: mongo:4.0.10
    container_name: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: mypass
      MONGO_INITDB_DATABASE: mydb
    ports:
      - 27017:27017
      - 27018:27018
      - 27019:27019
  1. 期望创建用户数据库。
  2. 数据库预填充了一些记录。

编辑 - 取得了一些进展,2 个问题

增加的卷数

mongo:
  image: mongo:4.0.1r0
  container_name: mongo
  restart: always
  volumes:
    - ./assets:/docker-entrypoint-initdb.d/

1.忽略

在资产文件夹中,我有 3 个文件,我在日志中看到了这个,我的文件被忽略了。

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/file1.json

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/file2.json

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/file3.json

我所有的 JSON 文件如下所示。 (没有根数组对象?根没有[]?)

{ "_id" : { "$oid" : "5d3a9d423b881e4ca04ae8f0" }, "name" : "Human Resource" }
{ "_id" : { "$oid" : "5d3a9d483b881e4ca04ae8f1" }, "name" : "Sales" }

2。默认数据库未创建。以下行没有任何效果。

MONGO_INITDB_DATABASE: mydb

【问题讨论】:

  • 你把你的脚本放在/docker-entrypoint-initdb.d/的脚本里了吗?
  • 请看我的编辑
  • @AppDeveloper 数据应该在.js.sh 文件中,其余的将被忽略。你可以看看我提供的例子。

标签: mongodb docker docker-compose


【解决方案1】:

所有文件*.json 扩展将被忽略,它应该在*.js。查看 mongo DB 的文档docker hub

MONGO_INITDB_DATABASE

此变量允许您指定要使用的数据库的名称 /docker-entrypoint-initdb.d/*.js 中的创建脚本(参见 在下面初始化一个新实例)。 MongoDB是基础 专为“首次使用创建”而设计,所以如果您不使用 您的 JavaScript 文件,则不会创建任何数据库。

初始化一个新实例

当一个容器第一次启动时,它会执行文件 带有扩展名 .sh 和 .js 在 /docker-entrypoint-initdb.d。文件将按字母顺序执行 命令。 .js 文件将由 mongo 使用数据库执行 由MONGO_INITDB_DATABASE 变量指定(如果存在),或者 否则测试。您也可以在 .js 脚本中切换数据库。

你可以看看这个例子

创建文件夹数据并将 create_article.js 放入其中

(在示例中,我传递了您创建的数据库用户)

db = db.getSiblingDB("user");
db.article.drop();

db.article.save( {
    title : "this is my title" , 
    author : "bob" , 
    posted : new Date(1079895594000) , 
    pageViews : 5 , 
    tags : [ "fun" , "good" , "fun" ] ,
    comments : [ 
        { author :"joe" , text : "this is cool" } , 
        { author :"sam" , text : "this is bad" } 
    ],
    other : { foo : 5 }
});

db.article.save( {
    title : "this is your title" , 
    author : "dave" , 
    posted : new Date(4121381470000) , 
    pageViews : 7 , 
    tags : [ "fun" , "nasty" ] ,
    comments : [ 
        { author :"barbara" , text : "this is interesting" } , 
        { author :"jenny" , text : "i like to play pinball", votes: 10 } 
    ],
    other : { bar : 14 }
});

db.article.save( {
    title : "this is some other title" , 
    author : "jane" , 
    posted : new Date(978239834000) , 
    pageViews : 6 , 
    tags : [ "nasty" , "filthy" ] ,
    comments : [ 
        { author :"will" , text : "i don't like the color" } , 
        { author :"jenny" , text : "can i get that in green?" } 
    ],
    other : { bar : 14 }
});

挂载data目录

docker run --rm -it  --name some-mongo -v /home/data/:/docker-entrypoint-initdb.d/  -e MONGO_INITDB_DATABASE=user     -e MONGO_INITDB_ROOT_USERNAME=root     -e MONGO_INITDB_ROOT_PASSWORD=mypass  mongo:4.0.10

创建容器后,您将能够看到 DB,

【讨论】:

  • 哦,所以您必须为每条记录编写db.article.save 之类的代码?所以没有json解决方案?
  • 还有我的问题第 2 部分中的任何 cmets
  • 从文档看来,你可以覆盖入口点来处理一个json文件。
  • 只有存在/dockerentrypoint.d的文件才会创建数据库,否则不会创建数据库。查看文档
  • db = db.getSiblingDB("employeeadmin");,顺便说一句,db 似乎是全局对象,但是在这里它会从 getSiblingDB 返回的任何内容中重新分配?
猜你喜欢
  • 2012-09-05
  • 1970-01-01
  • 2012-11-03
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 2019-01-05
相关资源
最近更新 更多