Forgot mongodb data path - mongodb

We have a mongodb database used for some years. Now the developer left. After server reboot, we cannot find out where is the data path he specified. It is not the default /data/db/. Our server is CentOS. Is there any command to search for it? Thanks.

Connect to the shell and run the following command:
> db.adminCommand('getCmdLineOpts')
You'll see something resembling:
{
"argv" : [
"mongod",
"--fork",
"--logpath",
"/data/logs/24rc2.log",
"--dbpath",
"/data/foobar"
],
"parsed" : {
"fork" : true,
"dbpath" : "/data/foobar",
"logpath" : "/data/logs/24rc2.log"
},
"ok" : 1
}

Look for the dbpath configuration line in /etc/mongodb.conf.

Related

mongoDB service on ubuntu not repairing

I have an AWS ubuntu instance where I upgraded a) ubuntu version (to 16.04.2 from 16.04) and b) mongoDB version (to 3.4.3 from 3.2). However, when I did so, mongoDB unlinked to all my databases.
I was able to copy the database files to /data/db/, use sudo mongod --repair and in the mongo shell, access all the databases.
However, the service version of mongoDB, run when I execute sudo service start mongod is still using the original /var/lib/mongodb/ (where the data still exists). When I try to repair from within the shell using db.repairDatabase(), I get only { "ok" : 1 } but no added databases. I need to have the service mongod running so clients can access it.
Here is a readout of what the service mongod outputs:
> db.adminCommand("getCmdLineOpts")
{
"argv" : [
"/usr/bin/mongod",
"--quiet",
"--config",
"/etc/mongod.conf"
],
"parsed" : {
"config" : "/etc/mongod.conf",
"net" : {
"bindIp" : "127.0.0.1",
"port" : 27017
},
"storage" : {
"dbPath" : "/var/lib/mongodb",
"journal" : {
"enabled" : true
}
},
"systemLog" : {
"destination" : "file",
"logAppend" : true,
"path" : "/var/log/mongodb/mongod.log",
"quiet" : true
}
},
"ok" : 1
}
> db.repairDatabase()
{ "ok" : 1 }
> show databases
admin 0.000GB
local 0.000GB
How can I repair the service mongod databases?
I realized that I could use mongodump --archive={path} and dump to an archive from the local database that was working. Then I turned on the service and used mongorestore --archive={path}, which loaded everything to the correct database. It is functional again.

how to output the result to a file in monogodb

I wan to list all database in Monogodb and output to a txt file, but it did not work.
mongo 127.0.0.1/test -eval 'var c= show databases;' >>db_list.txt
the error message is
MongoDB shell version: 2.6.12
connecting to: 127.0.0.1/test
2016-12-06T12:12:32.456-0700 SyntaxError: Unexpected identifier
anyone knows how to make this work. I appreciate any help.
To use eval and list databases directly on a shell, the following query should be helpful.
mongo test --eval "printjson(db.adminCommand('listDatabases'))"
MongoDB shell version: 3.2.10
connecting to: test
{
"databases" : [
{
"name" : "local",
"sizeOnDisk" : 73728,
"empty" : false
},
{
"name" : "m034",
"sizeOnDisk" : 11911168,
"empty" : false
},
{
"name" : "test",
"sizeOnDisk" : 536576,
"empty" : false
}
],
"totalSize" : 12521472,
"ok" : 1
}
This will list all the collection names in a particular DB.
mongo test --eval "printjson(db.getCollectionNames())"
MongoDB shell version: 3.2.10
connecting to: test
[
"aaa",
"areamodel",
"email",
"hex",
"key",
"mel",
"multi",
"ques",
"rich"
]
A sample execution for reference (screenshot)
Instead of test you can go simply,
mongo db_name query.js > out.json
here query.js contains any query like:
printjson( db.adminCommand('listDatabases') )

My read-only user is able to write

I'm using MongoDB 3.0.7. I have a database called bravegoat and a read-only user called bravegoat-r.
I connect via shell:
mongo localhost:27017/bravegoat -u bravegoat-r -p mypassword
I switch to my database:
use bravegoat;
And I run:
db.runCommand({connectionStatus : 1})
Which outputs:
{
"authInfo" : {
"authenticatedUsers" : [
{
"user" : "bravegoat-r",
"db" : "bravegoat"
}
],
"authenticatedUserRoles" : [
{
"role" : "read",
"db" : "bravegoat"
}
]
},
"ok" : 1
}
Only read role, so it looks fine, but when I invoke .save(), my user can insert data. I've read few pages about creating read-only users and I'm not able to see my problem. I'm starting to think it might be a bug in my version.
You have to enable client access control by doing the following:
Edit the /etc/mongod.conf file
Add the following lines
security:
authorization: enabled
Restart MongoDB:
sudo service mongodb restart

Replica Set Error Code 76

In ref to mongo dba course trying to create replica set as asked shown by instructor in El Capitano (Single machine only), I get following error. I have three members:
(mongodb was installed using homebrew)
Step I: Setting up config
cfg ={ _id :"abc", members:[{_id:0, host:"localhost:27001"}, {_id:1, host:"localhost:27002"}, {_id:2, host:"localhost:27003"}] }
{
"_id" : "abc",
"members" : [
{
"_id" : 0,
"host" : "localhost:27001"
},
{
"_id" : 1,
"host" : "localhost:27002"
},
{
"_id" : 2,
"host" : "localhost:27003"
}
]
}
STEP II: Initialize the Config.
rs.reconfig(cfg)
2015-10-05T11:34:27.082-0400 E QUERY Error: Could not retrieve replica set config: { "ok" : 0, "errmsg" : "not running with --replSet", "code" : 76 }
at Function.rs.conf (src/mongo/shell/utils.js:1017:11)
at Function.rs.reconfig (src/mongo/shell/utils.js:969:22)
at (shell):1:4 at src/mongo/shell/utils.js:1017
Make sure you have the replSetName name configured in /etc/mongod.conf
replication:
replSetName: "somename"
Then restart your mongod.
sudo service mongod stop
sudo service mongod start
sudo service mongod restart
You are not running replica set with the repl set name.The solution is to set a replication set name in the mongod config file using the paramater --replSet.
eg) --replSet=test_replica
Once changes are done in config file restart the server.

In MongoDB, can you see the commandline arguments from the shell?

I would like to be able to connect to a MongoDB instance using mongo.exe (or any shell) and somehow display the command-line / config startup options. AFAIK, this does not work, but this is what I'm thinking:
>startupOptions()
{
dbpath: d:\data\mongo,
logAppend: true,
master: false,
bindIp: 127.0.0.1,
/* ... */
}
Any ideas?
I'm not sure you can get the args for the shell you're running, but if you're looking for the args used to start the node you're connected to, you can do the following...
mongos> use admin
switched to db admin
mongos> db.runCommand("getCmdLineOpts")
This returns a BSONDocument which contains the arguments in both parsed and unparsed formats. You do not have to be connected to a mongos for this to work, but you do have to use the admin database.
Kander's answer is perfect, here is just some more info for everyone:
> use admin
switched to db admin
> db.runCommand("getCmdLineOpts")
{
"argv" : [
"D:\\MongoDB\\program\\mongod.exe",
"--config",
"d:\\MongoDB\\config\\mongodb.config",
"--service"
],
"parsed" : {
"bind_ip" : "127.0.0.1",
"config" : "d:\\MongoDB\\config\\mongodb.config",
"dbpath" : "D:\\data\\db",
"directoryperdb" : "true",
"logappend" : "true",
"logpath" : "D:\\MongoDB\\logs\\mongodb-service.log",
"rest" : "true",
"service" : true
},
"ok" : 1
}
Notice that you get both command-line arguments (argv) as well as the values from --config (parsed).