mongodump error connecting to db server in localhost - mongodb

It may be a very simple/obvious answer but I've tried running the following variations of the same command to backup a local mongo database and they all fail by returning:
Failed: error connecting to db server: no reachable servers
These are the commands:
mongodump --host localhost --port 27017 --db mydbname --collection mycollection
mongodump -h localhost:27017 --db mydbname --collection mycollection -u user -p passw --out <folder path>
mongodump -h localhost:27017 --db mydbname --collection mycollection --out <folder path>
mongodump --port 27017 --db mydbname --collection mycollection --out <folder path>
mongod instance was running on a separate terminal window while I tried them all
I JOURNAL [initandlisten] journal dir=/data/db/journal
I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
I JOURNAL [durability] Durability thread started
I CONTROL [initandlisten] MongoDB starting : pid=88451 port=27017 dbpath=/data/db 64-bit host=Diegos-MBP.home
I CONTROL [initandlisten]
I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
I JOURNAL [journal writer] Journal writer thread started
I CONTROL [initandlisten] db version v3.0.6
I CONTROL [initandlisten] git version: nogitversion
I CONTROL [initandlisten] build info: Darwin yosemitevm.local 14.5.0 Darwin Kernel Version 14.5.0: Wed Jul 29 02:26:53 PDT 2015; root:xnu-2782.40.9~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
I CONTROL [initandlisten] allocator: system
I CONTROL [initandlisten] options: {}
I NETWORK [initandlisten] waiting for connections on port 27017
I NETWORK [initandlisten] connection accepted from 127.0.0.1:55910 #1 (1 connection now open)
I cannot find mongodb.conf in /etc/mongod.conf. I installed mongo with homebrew. Maybe it is related to an authentication issue?

I just ran into the same issue and it appears that using 127.0.0.1 instead of localhost in the host setting worked...
Like :
mongodump -h 127.0.0.1:27017 -d demo
If you are using a replicat set (like "myset"), use this instead :
mongodump -h myset/127.0.0.1:27017 -d demo
Hope this helps...

Related

Mongo Shell from local is not getting connected to mongo server running in docker

I ran mongo server in docker and the logs came up just fine.
2019-04-12T10:39:51.334+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=8a03346e57d7
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] db version v3.2.22
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] git version: 105acca0d443f9a47c1a5bd608fd7133840a58dd
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] allocator: tcmalloc
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] modules: none
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] build environment:
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] distmod: rhel70
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] distarch: x86_64
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] target_arch: x86_64
2019-04-12T10:39:51.335+0000 I CONTROL [initandlisten] options: { net: { port: 27017 }, processManagement: { pidFilePath: "/var/run/mongodb/mongod.pid" }, storage: { dbPath: "/data/db" } }
2019-04-12T10:39:51.339+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),verbose=(recovery_progress),
2019-04-12T10:39:51.389+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-04-12T10:39:51.389+0000 I CONTROL [initandlisten]
2019-04-12T10:39:51.406+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2019-04-12T10:39:51.406+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-04-12T10:39:51.406+0000 I NETWORK [initandlisten] waiting for connections on port 27017
But, while connecting the mongodb server from local shell, the connection is getting timedout.
~/mongodb/bin/mongo --host 172.17.0.2
MongoDB shell version v3.6.11
connecting to: mongodb://172.17.0.2:27017/?gssapiServiceName=mongodb
2019-04-12T16:04:45.750+0530 W NETWORK [thread1] Failed to connect to 172.17.0.2:27017 after 5000ms milliseconds, giving up.
2019-04-12T16:04:45.753+0530 E QUERY [thread1] Error: couldn't connect to server 172.17.0.2:27017, connection attempt failed :
connect#src/mongo/shell/mongo.js:263:13
#(connect):1:6
exception: connect failed
where 172.17.0.2 is the IP obtained by running docker inspect <container-id
I was able to connect mongo shell with in the docker using the command docker exec --it <container-id> bash.
Here's the Docker file for refrence.
RUN echo -e "\
[mongodb]\n\
name=MongoDB Repository\n\
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/3.2/x86_64/\n\
gpgcheck=0\n\
enabled=1\n" >> /etc/yum.repos.d/mongodb.repo
# Install mongodb
RUN yum update -y && yum install -y mongodb-org
# Set up directory requirements
RUN mkdir -p /data/db /var/log/mongodb /var/run/mongodb
VOLUME ["/data/db", "/var/log/mongodb"]
# Expose port 27017 from the container to the host
EXPOSE 27017
# Start mongodb
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--port", "27017", "--dbpath", "/data/db", "--pidfilepath", "/var/run/mongodb/mongod.pid"]
Let me know if I am missing something.
Using MacOS

ERROR: dbpath (/data/db) does not exist. while trying to create a replica set in onrder to use mongoconnector for elastic search

while trying to create a replica set of db i get an error that dbpath /data/db does not exist.i am currently running mongo in docker as root.
version(mongo) 2.6..10.
i start using the service mongodb start command after which the mongo shell appears.log below
root#5936a72e744f:/dbex# mongod --replSet myDevReplSet
2017-07-31T05:13:30.946+0000 [initandlisten] MongoDB starting : pid=679 port=27017 dbpath=/data/db 64-bit host=5936a72e744f
2017-07-31T05:13:30.947+0000 [initandlisten] db version v2.6.10
2017-07-31T05:13:30.947+0000 [initandlisten] git version: nogitversion
2017-07-31T05:13:30.947+0000 [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-07-31T05:13:30.947+0000 [initandlisten] build info: Linux lgw01-12 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 BOOST_LIB_VERSION=1_58
2017-07-31T05:13:30.948+0000 [initandlisten] allocator: tcmalloc
2017-07-31T05:13:30.949+0000 [initandlisten] options: { replication: { replSet: "myDevReplSet" } }
2017-07-31T05:13:30.950+0000 [initandlisten] exception in initAndListen: 10296
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
, terminating
If you have a look into your /etc/mongod.conf you will see
storage:
dbPath: /data/db
So create directory /data/db and ensure proper permission.
ok guys..sometimes the data is stored in var/lib/mongod..so to start a replica set use
mongod --port 27017 --dbpath "/var/lib/mongodb" --replSet rs0 so check appropriate location and change dbpath location

MongoDB Self-signed SSL connection: SSL peer certificate validation failed

I have followed this guide Self-signed SSL connection using PyMongo, by Wan Bachtiar to create three .pem files; server.pem, client.pem and ca.pem.
I am using Ubuntu 16.04 and MongoDB v3.2.11.
The purpose is to secure the MongoDB before opening it to the public internet.
lets start the mongod:
$ mongod --auth --port 27017 --dbpath /data/db1
--sslMode requireSSL --sslPEMKeyFile /etc/ssl/server.pem
--sslCAFile /etc/ssl/ca.pem --sslAllowInvalidHostnames &
Output:
root#tim:/etc/ssl# 2017-01-13T12:58:55.150+0000 I CONTROL [initandlisten] MongoDB starting : pid=19058 port=27017 dbpath=/data/db1 64-bit host=tim
2017-01-13T12:58:55.150+0000 I CONTROL [initandlisten] db version v3.2.11
2017-01-13T12:58:55.151+0000 I CONTROL [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2017-01-13T12:58:55.151+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2g 1 Mar 2016
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] allocator: tcmalloc
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] modules: none
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] build environment:
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] distmod: ubuntu1604
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] distarch: x86_64
2017-01-13T12:58:55.152+0000 I CONTROL [initandlisten] target_arch: x86_64
2017-01-13T12:58:55.153+0000 I CONTROL [initandlisten] options: { net: { port: 27017, ssl: { CAFile: "/etc/ssl/ca.pem", PEMKeyFile: "/etc/ssl/server.pem", allowInvalidHostnames: true, mode: "requireSSL" }
}, security: { authorization: "enabled" }, storage: { dbPath: "/data/db1" } }
2017-01-13T12:58:55.211+0000 I - [initandlisten] Detected data files in /data/db1 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-01-13T12:58:55.212+0000 W - [initandlisten] Detected unclean shutdown - /data/db1/mongod.lock is not empty.
2017-01-13T12:58:55.212+0000 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
2017-01-13T12:58:55.212+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4)
,config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-01-13T12:58:55.886+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-01-13T12:58:55.886+0000 I CONTROL [initandlisten]
2017-01-13T12:58:55.895+0000 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db1/diagnostic.data'
2017-01-13T12:58:55.897+0000 I NETWORK [initandlisten] waiting for connections on port 27017 ssl
2017-01-13T12:58:55.897+0000 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-01-13T12:58:56.026+0000 I FTDC [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
After running the mongod, I start the mongo shell:
$ mongo --port 27017 -u "my username" -p "my password"
--authenticationDatabase "" --ssl --sslPEMKeyFile /etc/ssl/client.pem
--sslCAFile /etc/ssl/ca.pem --host tim
The output is similar to the question by Marshall Farrier; lets have a look.
MongoDB shell version: 3.2.11
connecting to: 127.0.0.1:27017/datatest
2017-01-13T12:35:58.247+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:38902 #8 (1 connection now open)
2017-01-13T12:35:58.259+0000 E NETWORK [thread1] SSL peer certificate validation failed: self signed certificate
2017-01-13T12:35:58.259+0000 E QUERY [thread1] Error: socket exception [CONNECT_ERROR] for SSL peer certificate validation failed: self signed certificate :
connect#src/mongo/shell/mongo.js:231:14
#(connect):1:6
2017-01-13T12:35:58.263+0000 E NETWORK [conn8] SSL peer certificate validation failed: self signed certificate
2017-01-13T12:35:58.263+0000 I NETWORK [conn8] end connection 127.0.0.1:38902 (0 connections now open)
What am I doing wrong?
After some searching, it seems like this error is due to the fact that the hostname "CN" was incorrect.
From digitalocean:
Whenever you generate a CSR, you will be prompted to provide information regarding the certificate. This information is known as a Distinguised Name (DN). An important field in the DN is the Common Name (CN), which should be the exact Fully Qualified Domain Name (FQDN) of the host that you intend to use the certificate with.
Also from MongoDB documentation:
If your MongoDB deployment uses SSL, you must also specify the --host option. mongo verifies that the hostname of the mongod or mongos to which you are connecting matches the CN or SAN of the mongod or mongos‘s --sslPEMKeyFile certificate. If the hostname does not match the CN/SAN, mongo will fail to connect.
SOLUTION:
I regenerated the keys, replaced localhost with any other hostname in the CN = <hostname> and completed the guide by Wan Bachtiar.
Running the following command after completion worked:
$ mongo --port 27017 -u '<_username_>' -p '<_password_>'
--authenticationDatabase "<_my db_>" --ssl --sslPEMKeyFile
/etc/ssl/client.pem --sslCAFile /etc/ssl/ca.pem --host localhost
Note:
The MongoDB folows a strict ruling of who has access to what db, a quick test in the mongo shell:
> show dbs
return an error. However, my user actually only have access to the db specified in "<my db>", so looping through the rows in "<my db>" works perfectly.

How to create a Mongo Docker Image with default collections and data?

I need support here to build my own mongo docker image.
I have a list of scripts to create and insert data into the MongoDB that shall be called in my Dockerfile to deliver a docker image with default collections and data.
Here is how my Dockerfile looks like currently:
FROM mongo:latest
RUN mkdir -p /data/scripts
COPY . /data/scripts
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db/
RUN FILES=scripts/*-create.js
RUN for f in $FILES; do mongo mydb $f; done
RUN FILES=scripts/*-insert.js
RUN for f in $FILES; do mongo mydb $f; done
RUN mongod --shutdown
I've tried different options to start and stop mongod and always one of the two fail, the current script raise the following error:
There doesn't seem to be a server running with dbpath: /data/db
Update
After #Matt answer I could run successfully the command chain, but can't still see my database (called my-db), collections and data there.
The current Dockerfile:
FROM mongo:latest
RUN mkdir -p /data/db/scripts
COPY . /data/db
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db \
&& CREATE_FILES=/data/db/scripts/*-create.js \
&& for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done \
&& INSERT_FILES=/data/db/scripts/*-insert.js \
&& for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done \
&& mongod --shutdown
The output from the docker build command:
Sending build context to Docker daemon 10.24 kB
Step 1 : FROM mongo:latest
---> c08c92f4cb13
Step 2 : RUN mkdir -p /data/db/scripts
---> Running in a7088943bb57
---> 373c7319927d
Removing intermediate container a7088943bb57
Step 3 : COPY . /data/db
---> 8fa84884edb7
Removing intermediate container ae43e2c24fee
Step 4 : RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db && CREATE_FILES=/data/db/scripts/*-create.js && for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done && INSERT_FILES=/data/db/scripts/*-insert.js && for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done && mongod --shutdown
---> Running in 33970b6865ee
about to fork child process, waiting until server is ready for connections.
forked process: 10
child process started successfully, parent exiting
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
killing process with pid: 10
---> 8451e43b7749
Removing intermediate container 33970b6865ee
Successfully built 8451e43b7749
But as I said, I still can't see the database, collections and data in my database using mongo shell.
Also I connected to the running container and got the mongodb.log:
2015-11-06T16:15:14.562+0000 I JOURNAL [initandlisten] journal dir=/data/db/journal
2015-11-06T16:15:14.562+0000 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-11-06T16:15:14.698+0000 I JOURNAL [initandlisten] preallocateIsFaster=true 2.36
2015-11-06T16:15:14.746+0000 I JOURNAL [durability] Durability thread started
2015-11-06T16:15:14.746+0000 I JOURNAL [journal writer] Journal writer thread started
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] MongoDB starting : pid=10 port=27017 dbpath=/data/db 64-bit host=9c05d483673a
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten]
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] db version v3.0.7
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] git version: 6ce7cbe8c6b899552dadd907604559806aa2e9bd
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] build info: Linux ip-10-183-78-195 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1 x86_64 BOOST_LIB_VERSION=1_49
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] allocator: tcmalloc
2015-11-06T16:15:14.747+0000 I CONTROL [initandlisten] options: { processManagement: { fork: true }, storage: { dbPath: "/data/db" }, systemLog: { destination: "file", path: "/var/log/mongodb.log" } }
2015-11-06T16:15:14.748+0000 I INDEX [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-11-06T16:15:14.802+0000 I STORAGE [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-11-06T16:15:14.802+0000 I STORAGE [FileAllocator] creating directory /data/db/_tmp
2015-11-06T16:15:14.804+0000 I STORAGE [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0 secs
2015-11-06T16:15:14.807+0000 I NETWORK [initandlisten] waiting for connections on port 27017
2015-11-06T16:15:14.830+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:49641 #1 (1 connection now open)
2015-11-06T16:15:14.832+0000 I INDEX [conn1] allocating new ns file /data/db/my-db.ns, filling with zeroes...
2015-11-06T16:15:14.897+0000 I STORAGE [FileAllocator] allocating new datafile /data/db/my-db.0, filling with zeroes...
2015-11-06T16:15:14.898+0000 I STORAGE [FileAllocator] done allocating datafile /data/db/my-db.0, size: 64MB, took 0 secs
2015-11-06T16:15:14.904+0000 I NETWORK [conn1] end connection 127.0.0.1:49641 (0 connections now open)
2015-11-06T16:15:14.945+0000 I NETWORK [initandlisten] connection accepted from 127.0.0.1:49642 #2 (1 connection now open)
2015-11-06T16:15:14.958+0000 I NETWORK [conn2] end connection 127.0.0.1:49642 (0 connections now open)
2015-11-06T16:15:14.982+0000 I CONTROL [signalProcessingThread] got signal 15 (Terminated), will terminate after current cmd ends
2015-11-06T16:15:14.982+0000 I CONTROL [signalProcessingThread] now exiting
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] closing listening socket: 6
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] closing listening socket: 7
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog...
2015-11-06T16:15:14.982+0000 I NETWORK [signalProcessingThread] shutdown: going to close sockets...
2015-11-06T16:15:14.982+0000 I STORAGE [signalProcessingThread] shutdown: waiting for fs preallocator...
2015-11-06T16:15:14.982+0000 I STORAGE [signalProcessingThread] shutdown: final commit...
2015-11-06T16:15:15.008+0000 I JOURNAL [signalProcessingThread] journalCleanup...
2015-11-06T16:15:15.008+0000 I JOURNAL [signalProcessingThread] removeJournalFiles
2015-11-06T16:15:15.009+0000 I JOURNAL [signalProcessingThread] Terminating durability thread ...
2015-11-06T16:15:15.088+0000 I JOURNAL [journal writer] Journal writer thread stopped
2015-11-06T16:15:15.088+0000 I JOURNAL [durability] Durability thread stopped
2015-11-06T16:15:15.088+0000 I STORAGE [signalProcessingThread] shutdown: closing all files...
2015-11-06T16:15:15.090+0000 I STORAGE [signalProcessingThread] closeAllFiles() finished
2015-11-06T16:15:15.090+0000 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2015-11-06T16:15:15.090+0000 I CONTROL [signalProcessingThread] dbexit: rc: 0
I also checked the folder /data/db content:
root#fbaf17233182:/data/db# ls -al
total 16
drwxr-xr-x 3 mongodb mongodb 4096 Nov 6 16:15 .
drwxr-xr-x 4 root root 4096 Nov 6 16:15 ..
drwxr-xr-x 2 root root 4096 Nov 5 18:55 scripts
May help:
Parent Dockerfile
Parent Dockerfile ENTRYPOINT
The problem was that information could not be saved on /db/data, so I've created a solution creating my own data directory.
# Parent Dockerfile https://github.com/docker-library/mongo/blob/982328582c74dd2f0a9c8c77b84006f291f974c3/3.0/Dockerfile
FROM mongo:latest
# Modify child mongo to use /data/db2 as dbpath (because /data/db wont persist the build)
RUN mkdir -p /data/db2 \
&& echo "dbpath = /data/db2" > /etc/mongodb.conf \
&& chown -R mongodb:mongodb /data/db2
COPY . /data/db2
RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2 --smallfiles \
&& CREATE_FILES=/data/db2/scripts/*-create.js \
&& for f in $CREATE_FILES; do mongo 127.0.0.1:27017 $f; done \
&& INSERT_FILES=/data/db2/scripts/*-insert.js \
&& for f in $INSERT_FILES; do mongo 127.0.0.1:27017 $f; done \
&& mongod --dbpath /data/db2 --shutdown \
&& chown -R mongodb /data/db2
# Make the new dir a VOLUME to persists it
VOLUME /data/db2
CMD ["mongod", "--config", "/etc/mongodb.conf", "--smallfiles"]
Thanks to #yosifkit from the docker-library/mongo Github project for pointing that the volume would store the data in the resulting image. I missed that on the documentation.
During a docker image build, each build command like RUN is launched in it's own docker container and then when the command completes the data is committed as an image. If you run dockviz images --tree while doing a build you will get the idea.
In your case mongod has started and stopped long before you need it. You need to start mongo and run your scripts all in the one RUN step. You can achieve that by using a shell script that launches mongod and inserts your data.
Your Dockerfile will run:
RUN mongo_create_insert.sh
Then mongo_create_insert.sh contains all your mongo dependent steps:
#!/usr/bin/env bash
mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db/
FILES=scripts/*-create.js
for f in $FILES; do mongo mydb $f; done
FILES=scripts/*-insert.js
for f in $FILES; do mongo mydb $f; done
mongod --shutdown
As a side note, I tend to install Ansible in my base image and use that to provision Docker images in single RUN command rather than doing lots of shell RUN steps in a Dockerfile (which is just a glorified shell script in the end). You lose some of the build caching niceness but we've moved on from provisioning with shell scripts for a reason.
According to the description of the image on DockerHub, there is a much cleaner and simpler solution for this.
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.
First, the Dockerfile is as simple as
FROM mongo:4
COPY setup.sh /docker-entrypoint-initdb.d/
COPY scripts /
Then, in the setup.sh, add your user/collection creation script, for example
mongo=( mongo --host 127.0.0.1 --port 27017 --quiet )
mongo+=(
--username="$MONGO_INITDB_ROOT_USERNAME"
--password="$MONGO_INITDB_ROOT_PASSWORD"
--authenticationDatabase="$rootAuthDatabase"
)
CREATE_FILES=/scripts/*-create.js
for f in $CREATE_FILES; do "${mongo[#]}" "$MONGO_INITDB_DATABASE" $f; done
INSERT_FILES=/scripts/*-insert.js
for f in $INSERT_FILES; do "${mongo[#]}" "$MONGO_INITDB_DATABASE" $f; done

Mongodb how to connect and import data

I installed mongodb in Mac by homebrew. Then I tried to start it by mongodb. Failed. Then I start it by mongod --dbpath /data/db. Then it is waiting forever. How to start mongodb?
2015-08-22T16:08:52.619+0100 I CONTROL [initandlisten] build info: Darwin mimac 14.4.0 Darwin Kernel Version 14.4.0: Thu May 28 11:35:04 PDT 2015; root:xnu-2782.30.5~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-08-22T16:08:52.619+0100 I CONTROL [initandlisten] allocator: system
2015-08-22T16:08:52.619+0100 I CONTROL [initandlisten] options: { storage: { dbPath: "/data/db/" } }
2015-08-22T16:08:52.628+0100 I NETWORK [initandlisten] waiting for connections on port 27017
To start mongodb automatically you can use command:
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist;
mongod is the daemon (i.e the mongoDB server itself). you should run it, as you did.
after there is a running server, you can:
run the mongo shell, with the mongo command. this would give you an interface to the databases you have, such as querying and inserting data.
to import an external file into mongo database, you should use the mongoimport command line tool, which is provided with mongodb.
mongoimport -d database_name -c collection_name < filename.json
mongoimport can import json, csv and tsv files.