Mongo db getting error while creating replica set - mongodb

I am getting a syntax error while using this command.
Practise> mongosh --replSet r0 --dbpath C:\MongoDB --port 27021 --quiet
Uncaught:
SyntaxError: Missing semicolon. (1:10)
1 | mongosh --replSet r0 --dbpath C:\MongoDB --port 27021 --quiet
| ^
2 |
I have given the semicolon at last but I don't think that's a command

Related

Is it safe to close the mongod process with docker stop?

When I use docker-compose to create MongoDB containers below, this image is my MongoDB image based on centos. Can using docker stop safely shut down the mongod process?
I learned that the docker stop command will send a SIGTERM signal to processes with PID 1, but I'm not sure whether the process of mongod can receive it in the current situation, because from the following situation, the PID of mongod is 9.
mongodb:
image: mongo:centos
command: sh /home/mongo/scripts/compose/noauth.sh 27017
hostname: mongodb
ports:
- 27017:27017
volumes:
- /home/mongo/mongo_data:/home/mongo/mongo_data
- /home/mongo/mongo_log:/home/mongo/mongo_log
This is the script of noauth.sh
#!/bin/bash
if [ ! -d /home/mongo/mongo_data/data_$2/ ];then
mkdir -p /home/mongo/mongo_data/
fi
/home/mongo/mongo_server/bin/mongod --shardsvr --fork --dbpath /home/mongo/mongo_data/ --bind_ip 0.0.0.0 --port $1
This is the process in the container.
UID PID PPID C STIME TTY TIME CMD
mongo 1 0 0 Jun16 ? 00:00:58 sh /home/mongo/scripts/compose/noauth.sh 27017
mongo 9 1 0 Jun16 ? 01:03:58 /home/mongo/mongo_server/bin/mongod --shardsvr --fork --dbpath /home/mongo/mongo_data/ --bind_ip 0.0.0.0
You want to avoid the shell script wrapper that's pid 1 in your docker exec ps output. The easiest way is to use the shell exec built-in, which replaces the script with the program you're running.
#!/bin/sh
# ^^ prefer plain `sh` when an option
# v make sure to quote strings v
if [ ! -d "/home/mongo/mongo_data/data_$2" ]; then
mkdir -p /home/mongo/mongo_data
fi
#vvv this `exec` removes the shell wrapper
exec /home/mongo/mongo_server/bin/mongod \
--shardsvr \
--fork \
--dbpath /home/mongo/mongo_data \
--bind_ip 0.0.0.0 \
--port "$1"
You shouldn't need to explicitly name the sh interpreter in your Compose command:. (And these paths don't seem like they're the paths the Docker Hub mongo image uses; if this is a custom image, make this be your image's CMD and not a Compose override.)
Once you do this you should see the database as pid 1 when you docker-compose exec mongodb ps -ef, and then it will receive the docker stop signal as normal.

How to display line # in Mongodb Shell- ReplicaSet config Error

I'm testing a replicaSet on my local machine. It's only for testing.
I started my local mongodb instance bin/mongod
Then started three instances with the following configuration:
mongod --replSet rstest --logpath \data\rs2\2.log --dbpath \data\rs2 --port 27018 --smallfiles --oplogSize 64
mongod --replSet rstest --logpath \data\rs3\3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64
mongod --replSet rstest --logpath \data\rs3\3.log --dbpath \data\rs3 --port 27019 --smallfiles --oplogSize 64
Then I started mongo --port 27017 and type the following configuration:
config = {_id:“rstest”, members:[
{_id:0,host:“localhost:27017”},
{_id:1,host:“localhost:27018”},
{_id:2,host:“localhost:27019”}
]};
When I do type the above code and hit enter, I receive the following error message:
E QUERY [js] SyntaxError: missing : after property id #(shell):1:101
E QUERY [js] SyntaxError: missing : after property id #(shell):1:101
I'm unable to figure out where is the miss :
Is there a way to get the shell screen to display line #s? Or where is that 1:101
Any idea why I'm receiving this error? Where is the missing : should go?
The problem is that you have smart quotes (aka curly quotes) around values instead of the normal " characters. If you replace “ and ” with straight " there are no syntax errors.
I'm unable to figure out where is the miss : Is there a way to get the shell screen to display line #s? Or where is that 1:101
The error message indicates line 1, column 101 but the syntax error isn't particularly helpful because smart quotes have confused the JavaScript interpreter.
Unfortunately there isn't an option to turn on line numbers in the mongo shell so you'd have to use an external editor or count the lines & character offset in the error message. Ideally you should use an editor that includes JavaScript syntax checking and line numbering.
There are a few ways to conveniently work with an external editor in the mongo shell:
1) Set the EDITOR environment variable before starting the mongo shell and use the edit command to modify a shell variable using external editor.
For example:
export EDITOR=vim
mongo
> var cfg = {}
> edit cfg
The edit command in the shell creates a temporary file that is eval'd in the mongo shell when you quit your external editor. If there are any syntax errors your change will not be saved, so this is better for quick changes than extended coding.
2) Save your JavaScript in a file using an external editor and use the load() command in the mongo shell:
load("/path/to/myfile.js")
This approach is more convenient for working with larger snippets of JavaScript since you do not have to worry about syntax errors preventing your changes from being saved.

there is no response when I install mongo

mongo version: mongodb-win32-x86_64-2008plus-ssl-3.2.6-signed
It succeeded in another computer.
I do as README but nothing happend..
What should I do ?
PS D:\MongoDB\Server\3.2\bin> mongod --config D:\mongodb\Server\3.2\mongo.config
PS D:\MongoDB\Server\3.2\bin>
PS D:\MongoDB\Server\3.2\bin> mongod --dbpath D:\mongodb\Server\3.2\data\db
PS D:\MongoDB\Server\3.2\bin>
Try mongod -vvvv ... for verbose output during sturtup.

Running one bat file in command line in order to setup a MongoDB replica set

How to cause the following commands present in bat file to open in different cmd windows and be run there?
mongod.exe --port 61123 --dbpath D:\data\rs0 --replSet test
mongod.exe --port 61124 --dbpath D:\data\rs1 --replSet test
mongod.exe --port 61125 --dbpath D:\data\rs2 --replSet test
Thanks.
start "" mongod.exe --port 61123 --dbpath D:\data\rs0 --replSet test
start "" mongod.exe --port 61124 --dbpath D:\data\rs1 --replSet test
start "" mongod.exe --port 61125 --dbpath D:\data\rs2 --replSet test

How to set permernent dbpath for mongodb

I understand that mongo db need to be started before I can interact with it. But what I don't understand why do I set the dbpath every time? I thought we only need to configure that path once. Am I correct?
You can solve this two ways:
change your dbpath to the hard coded one which will point to /data/db/
Or make a startup script that will actually call the MongoDB instance for you
You could make a few scripts, as I said in my last point, to do this for you, as an example:
=== rnMongo.sh ===
./mongod --dbpath
Then with a single command:
./rnMongo.sh
Or as an upstart job:
# mongodb - Mongo Starter
author "lol"
description "Starts the MongoDB servers"
start on started network-services
#expect fork
exec /home/ubuntu/mongodb/bin/mongod --auth
#echo "Mongodb is now running";
#exit 0;
#stop
stop on runlevel [016]
#pre-stop
Something along those lines
Just add mongod --dbpath /home/user/mongodb to your startup applications ;)
sudo mongod --port portnumber --dbpath /path to your folder
By default it is set to
sudo mongod --port 27017 --dbpath /var/lib/mongodb