Get output of mongo shell script - mongodb

as a part of my mongoDB maintenance I'm running mongo shell and make it to load 2 scripts. The command I'm running looks like follows:
$MONGO_HOME/bin/mongo --verbose --port 27017 replSetConfig.js initializeReplicaSet.js
The output I got is:
MongoDB shell version: 2.2.3
Thu Mar 7 03:00:00 versionCmpTest passed
Thu Mar 7 03:00:00 versionArrayTest passed connecting to: 127.0.0.1:27017/test
Thu Mar 7 03:00:01 creating new connection to:127.0.0.1:27017
Thu Mar 7 03:00:01 BackgroundJob starting: ConnectBG
Thu Mar 7 03:00:01 connected connection!
loading file: js/replSet.config.js
loading file: js/initializeReplicaSet.js
I'm redirecting the output to a log file but I would like to see some output of the loaded scripts as well. I.e. the output which I see in the shell if I start it and call load("...") for the very same scripts. Is there a way how to capture the output?
Thanks

To add output of scripts you must use print() or printjson() statements otherwise MongoDB will remain quiet about any output of a script.

Related

The command '/bin/sh -c service mongod start' return a non-zero code: 1 in docker

i'm trying to create a docker image run Mongodb on my Raspberry Pi 3+ with Raspbian buster. But when i build image and run container, mongodb doesn't auto start, so i add command RUN service mongod start then build image. Mongodb seems to be installed, i checked by service --status-all and mongodb listed but not started. Error come when docker try to start mongodb serive: The command '/bin/sh -c service mongod start' return a non-zero code: 1.
This is myDockerfile:
FROM cretzel/rpi-mongodb
WORKDIR /usr/src/mongodb
RUN chmod +x /var/lib/mongodb
RUN service mongod start
EXPOSE 27017
CMD ["mongod"]
I tried to fix this and be told that it doesn't have permission, so i add RUN chmod +x /var/lib/mongodb to my dockerfile but it don't work.
Can someone help?
---------------- Update from #Adiii's answer -----------------------------------
My new dockerfile:
FROM cretzel/rpi-mongodb
WORKDIR /usr/src/mongodb
COPY ./entrypoint.sh .
RUN chmod +x ./entrypoint.sh
VOLUME ./database /data/db
EXPOSE 27017
CMD ["mongod"]
entrypoint.sh
#!/bin/bash
service mongod start
exec "$#"
The change solved my error but mongodb service still cannot start. This is log file:
Starting database: mongodb failed!
db level locking enabled: 1
mongod --help for help and startup options
Mon Nov 11 16:31:15
Mon Nov 11 16:31:15 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.
Mon Nov 11 16:31:15
warning: some regex utf8 things will not work. pcre build doesn't have --enable-unicode-properties
Mon Nov 11 16:31:15 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db/ 32-bit host=38271f34412b
Mon Nov 11 16:31:15 [initandlisten]
Mon Nov 11 16:31:15 [initandlisten] ** NOTE: This is a development version (2.1.1-pre-) of MongoDB.
Mon Nov 11 16:31:15 [initandlisten] ** Not recommended for production.
Mon Nov 11 16:31:15 [initandlisten]
Mon Nov 11 16:31:15 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
Mon Nov 11 16:31:15 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitations
Mon Nov 11 16:31:15 [initandlisten] ** with --journal, the limit is lower
Mon Nov 11 16:31:15 [initandlisten]
Mon Nov 11 16:31:15 [initandlisten] db version v2.1.1-pre-, pdfile version 4.5
Mon Nov 11 16:31:15 [initandlisten] git version: 47fbbdceb21fc2b791d22db7f01792500647daa9
Mon Nov 11 16:31:15 [initandlisten] build info: Linux raspberrypi 3.2.27+ #102 PREEMPT Sat Sep 1 01:00:50 BST 2012 armv6l BOOST_LIB_VERSION=1_49
Mon Nov 11 16:31:15 [initandlisten] options: {}
Mon Nov 11 16:31:15 [initandlisten] waiting for connections on port 27017
Mon Nov 11 16:31:15 [websvr] admin web console waiting for connections on port 28017
Mon Nov 11 16:32:15 [clientcursormon] mem (MB) res:20 virt:83 mapped:0
I don't understand Docker so much, it don't show any error. Please help!
You should not start the process at RUN command, each run command run at a separate shell and RUN is for installation and configuration not to start the process. To start the process in the container you need to start the process at entrypoint or CMD.
also, the container needs a process to run in foreground so service will not work in case of the container. so
EXPOSE 27017
CMD ["mongod"]
This is enough to start the mongo process, if the container is up and running it's mean mongod process is running, you do not need to check the status using service --status-all. as soon as MongoDB process dies container will die automatically.
Will suggest to use offical mongo docker image.

Mongodump getting blank folders

I am trying to run this command from a remote ssh
mongodump --host mongodb1.example.net --port 27017 --username user --password pass --out /opt/backup/mongodump-2013-10-24
However, all I am getting is blank folders for each collections.
What am I doing wrong?
Info (if it matters):
Source Mongodb is on Windows Server
And SSH is of a remote Ubuntu Machine
Here is the terminal output:
connected to: ip.ip.ip.ip:27017
Thu Feb 18 00:46:01.757 all dbs
Thu Feb 18 00:46:01.809 DATABASE: admin to /opt/backup/mongodump-2013-10-24/admin
Thu Feb 18 00:46:01.903 DATABASE: anthony_data to /opt/backup/mongodump-2013-10-24/anthony_data
Thu Feb 18 00:46:02.004 DATABASE: temp_data to /opt/backup/mongodump-2013-10-24/temp_data
Thu Feb 18 00:46:02.104 DATABASE: zoomy to /opt/backup/mongodump-2013-10-24/zoomy
So it creates empty folder for all collections. But there is no bson in it
Check that your copy of mongodump and the database itself is the same version.
I had the exact same problem (complete with no terminal output or logs) when I tried to do a dump of a mongo 3.2.4 server with a 2.4.1 mongodump client. Removing the older client and installing the newer one resolved it immediately.
I was using the default mongodb-client deb package with Ubuntu. I removed those and installed the mongodb-org-tools package from mongodb.com https://docs.mongodb.com/master/tutorial/install-mongodb-on-ubuntu/?_ga=2.36209632.1945690590.1499275806-1594815486.1499275806
They have other install instructions for your specific OS if you are not on Ubuntu https://www.mongodb.com/download-center?jmp=nav#community

Mongo shell closes immediately

When I launch the mongo shell it closes immediately. There is no error. It was working fine (yesterday). My MongoDB server is running perfectly, and my application is connecting to it fine. I can't even launch the shell without connecting to a database:
C:\mongodb\bin> mongo --verbose
MongoDB shell version: 2.4.7
Fri Mar 27 10:35:38.425 versionArrayTest passed
connecting to: test
Fri Mar 27 10:35:38.472 creating new connection to:127.0.0.1:27017
Fri Mar 27 10:35:38.472 BackgroundJob starting: ConnectBG
Fri Mar 27 10:35:38.487 connected connection!
bye
Fri Mar 27 10:35:38.487 freeing 1 uncollected class mongo::DBClientCursor objects
Fri Mar 27 10:35:38.487 freeing 1 uncollected class mongo::DBClientWithCommands objects
Fri Mar 27 10:35:38.487 freeing 1 uncollected class mongo::BSONHolder objects
Similarly when connecting with no db:
C:\mongodb\bin> mongo --nodb --norc --verbose
MongoDB shell version: 2.4.7
Fri Mar 27 10:37:00.513 versionArrayTest passed
bye
Any and all thoughts on how to connect gratefully received.
As, mongod service has stopped; you need to go in the task manager-> Services Tab
-> search for MongoDB
-> Right click and click start.
You will find mongod shell is working as it was working previously.
I hope this helped!

Executing a JavaScript in MongoDB

I am trying to execute a Java script file in MongoDB at port 5930. Mongo can listen to the Database on that port:
./mongo localhost:5930
connecting to: localhost:5930/test
Server has startup warnings:
Wed Dec 18 13:15:58.906 [initandlisten]
Wed Dec 18 13:15:58.906 [initandlisten] ** WARNING: /proc/sys/vm/overcommit_memory is 2
Wed Dec 18 13:15:58.906 [initandlisten] ** Journaling works best with it set to 0 or 1
Now when I execute test.js at port 5930 but I get the following error:
./mongo test.js
connecting to: test
Wed Dec 18 13:22:56.011 JavaScript execution failed: Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112
exception: connect failed
The test.js has the following line:
conn = new Mongo("localhost:5930);
How can I run the script at port 5930? and Why did the script always try to execute from the default port (27017)?
Thanks
Mongo tries to connect itself to the local database automatically, before it looks at your script.
Connect to a local Mongo database running on an alternate port with this mongo command line:
mongo --nodb test.js
And have your code open the connection (which you already do).

Starting mongodb cluster

I'm starting 3 mongod processes on 3 different machines and try to run the mongos process on the another machine that runs also the application server.
I'm getting this message:
~$ mongos --configdb mongo1:27017,mongo2:27017,mongo3:27017
Mon Sep 24 10:34:05 mongos db version v2.0.4, pdfile version 4.5 starting (--help for usage)
Mon Sep 24 10:34:05 git version: nogitversion
Mon Sep 24 10:34:05 build info: Linux yellow 2.6.24-29-server #1 SMP Tue Oct 11 15:57:27 UTC 2011 x86_64 BOOST_LIB_VERSION=1_46_1
Mon Sep 24 10:34:09 ERROR: config servers mongo1:27017 and mongo2:27017 differconfig servers mongo1:27017 and mongo2:27017 differconfig servers mongo1:27017 and mongo2:27017 differconfig servers mongo1:27017 and mongo2:27017 differconfig servers not in sync! config servers mongo1:27017 and mongo2:27017 differ
chunks: "d41d8cd98f00b204e9800998ecf8427e" EOO
EOO EOO
configServer startup check failed
I've deleted mongod.lock from all config servers and restarted mongod.
That solved the problem.
There are 2 ways it can be done,
1) clear the data folder and restart the server, it will start as usual without any error,
2) delete the mongod lock file and Use the --repair option.