mongoDB service on ubuntu not repairing - mongodb

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.

Related

MongoDB - Not Authorized to add replica set member, but Mongo Auth is disabled

My planned steps (my goal):
Create 3x Mongo Hosts
Initiate Replication (rs.initiate())
Add hosts to replication set
Enable auth on primary (which should sync user/role/auth settings across cluster??)
Auth is disabled during the replica set config, however I'm getting "not authorized on local to execute command" when attempting to add a member.
Here are the steps I'm running on a fresh brand new Mongo install without auth enabled yet ---
Check that --auth is not enabled:
root#3106f5453c95:/# ps -ef |grep mongod
mongodb 1 0 1 16:34 ? 00:00:01 mongod --bind_ip_all --config /etc/mongod.conf
mongod.conf
systemLog:
destination: file
path: "/var/log/mongodb/mongod.log"
logAppend: true
replication:
replSetName: rsprod
security:
keyFile: /data/db/replicaSetKey.key
The following commands are ran on one of the hosts in the 3 node set:
Initiate replica set without issue:
root#b902fd176bdd:/# mongo --eval 'rs.initiate()'
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "b902fd176bdd:27017",
"ok" : 1,
"operationTime" : Timestamp(1526490381, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1526490381, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Then adding a host (it's denied):
root#b902fd176bdd:/# mongo --eval 'rs.add("myhost.example.com:27017")'
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
2018-05-16T17:07:20.860+0000 E QUERY [thread1] Error: count failed: {
"operationTime" : Timestamp(1526490432, 1),
"ok" : 0,
"errmsg" : "not authorized on local to execute command { count: \"system.replset\", query: {}, fields: {}, $clusterTime: { clusterTime: Timestamp(1526490432, 1), signature: { hash: BinData(0, 5DC7E35270B286518353EDADEBF474074AD1140A), keyId: 6556226268348547073 } }, $db: \"local\" }",
"code" : 13,
"codeName" : "Unauthorized",
"$clusterTime" : {
"clusterTime" : Timestamp(1526490432, 1),
"signature" : {
"hash" : BinData(0,"XcfjUnCyhlGDU+2t6/R0B0rRFAo="),
"keyId" : NumberLong("6556226268348547073")
}
}
} :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
DBQuery.prototype.count#src/mongo/shell/query.js:383:11
DBCollection.prototype.count#src/mongo/shell/collection.js:1584:12
rs.add#src/mongo/shell/utils.js:1274:1
#(shell eval):1:1
My understanding is that auth roles were not a requirement of replica sets, so what am I doing wrong here?
Found the problem by searching around and finally found it.
Here is the cause of the whole problem:
keyFile implies security.authorization.
https://docs.mongodb.com/manual/reference/configuration-options/#security.keyFile
So, if you use a KeyFile for the cluster, you MUST use authorization as well. This is pretty hidden in the documentation but clearly it's very important.
So the order of operations should be:
Initialize databases
Create Users/Roles
Restart DB with auth enabled
Configure replication
It's either that or don't use a keyFile for the cluster.

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).

Forgot mongodb data path

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.