创建mongodb数据库并通过docker-compose填充一些数据

时间:2019-07-26 03:15:49

标签: mongodb docker docker-compose

我有以下指定了自定义数据库的脚本,但是我看不到在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

1 个答案:

答案 0 :(得分:0)

所有文件*.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

创建容器后,您将可以看到数据库,

enter image description here

相关问题