Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

root cant perform listCollections command on db

I have credentials for root user and I am using those credentials to automate db backup. Main aim is to create prototype for Automated DB backup and, for simplicity, I am using root. The script (I borrowed from article) looks like as follows:

#!/bin/bash

#Force file syncronization and lock writes
mongo admin -u "root" -p "root" --eval "printjson(db.fsyncLock())"

MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_DATABASE="mydb" #replace with your database name

TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_NAME="mydb" #replace with your bucket name on Amazon S3
S3_BUCKET_PATH="backup/mongo"

# Create backup
$MONGODUMP_PATH -d $MONGO_DATABASE

# Add timestamp to backup
mv dump mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP

# Upload to S3
s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar

#Unlock database writes
mongo admin -u "root" -p "root" --eval "printjson(db.fsyncUnlock())"
#Delete local files
#rm -rf mongodb-*

I am getting following error:

Failed: error getting collections for database mydb: error running listCollections. Database: mydb Err: not authorized on mydb to execute command { listCollections: 1, cursor: {} }

Isnt root has all the access over all the databases? I am bit scared that I might run into situation where I am thinking to supersede something with root but It doesnt have the permission. This is the root cause of posting question. I want to avoid surprises like this in the future.

like image 747
Darshan Puranik Avatar asked Oct 10 '17 12:10

Darshan Puranik


People also ask

How do I view all collections in MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.

How do I view tables in MongoDB?

If you want to check your databases list, use the command show dbs. Your created database (mydb) is not present in list. To display database, you need to insert at least one document into it. In MongoDB default database is test.

How do I list databases in MongoDB?

The show dbs Mongo Shell command returns the list of all databases running on MongoDB Server including default and user-defined databases. The Mongo Shell db. adminCommand allows you to return the list of MongoDB databases in a JSON format.


1 Answers

The error mentioned above was misleading for me. But my script was wrong or incomplete.

If you look at following line in my script:

$MONGODUMP_PATH -d $MONGO_DATABASE

I am not providing any user in Mongodump command and hence the error message. If I rewrite that line something as follows:

$MONGODUMP_PATH -d $MONGO_DATABASE --authenticationDatabase "admin" -u "dbowner" -p "pwd"

then error goes away.

like image 66
Darshan Puranik Avatar answered Sep 20 '22 17:09

Darshan Puranik