创建mongodb数据库并通过docker-compose填充一些数据 [英] Creating mongodb database and populating it with some data via docker-compose

查看:286
本文介绍了创建mongodb数据库并通过docker-compose填充一些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经研究了这个


I've the following script with a custom database specified but I don't see the database user getting created within the GUI (compass). I only see 3 default databases (admin, config, local).

I've looked into this linked answer but I need a specific answer for my question, please.

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. The expectation for a user database to be created.
  2. Database prefilled with some records.


Edit - made some progress, 2 Problems

Added volumes

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

1. Ignore

Within assets folder, I've 3 files and I see this in the logs, my files are getting ignored.

/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

all my JSON files look like following. (no root array object? no [] at the root?)

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

2. Default Database not getting created. following line is not having any effect.

MONGO_INITDB_DATABASE: mydb

解决方案

All files *.json extension will be ignored, it should in *.js. Look into the documentation of mongo DB docker hub

MONGO_INITDB_DATABASE

This variable allows you to specify the name of a database to be used for creation scripts in /docker-entrypoint-initdb.d/*.js (see Initializing a fresh instance below). MongoDB is fundamental designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.

Initializing a fresh instance

When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.

you can look into this example

create folder data and place create_article.js in it

( in the example I am passing your created DB user)

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 }
});

mount the data directory

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

once container created you will be able to see the DBs,

这篇关于创建mongodb数据库并通过docker-compose填充一些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆