MongoDB: Not authorized on Admin - mongodb

I'm beginning to use MongoDB and have spent several hours troubleshooting without resolve. From what I understand, there first needs to be a root user before there can be an authorized/admin user.
I installed in C:\mongodb\, then setup --dbpath in C:\mongodb\data and --logpath in C:\mongodb\logs\log.txt.
I installed then ran the MongoDB service, accessible in the services.msc
My recipe to disaster was:
1. mongo localhost,
2. use admin
3. db.createUser({ user: 'root', pwd: '123456', roles:['root']})
Then get "Error: couldn't add user: not authorized on admin to execute command { createUser ....."
I am running MongoDB shell version: 2.6.5 and greatly appreciate any help that leads to a solution.
Thank you,
Leo

I figured it out. I accidentally put the data and logs in same directory as the \mongodb\bin. The solution is to put the mongodb\data and mongodb\logs in a separate folder or drive -- I differentiated using C:\~ and D:\~ drives.

Related

Basics of using MongoDB on remote server with SSH

I'm having trouble getting started with MongoDB via SSH on a VPS. Note that MongoDB was installed by admins with the VPS service, not me. I have to presume they installed it correctly. Unfortunately, they provide zero support documentation and I can't figure it out after reading countless official MongoDB docs and even more found elsewhere.
I am able to SSH into the remote server just fine. I can check the MongoDB version, and mongosh is there, too. But I cannot figure out how to get to the point where I can create a new collection, or even start MongoDB, if that's what I need to do.
Here are some of the things I've tried in the shell for the VPS. The closest I've gotten is by running mongosh -nodb which seems to get me inside of MongoDB, but I know that option means "no database," so it's not what I need to do.
$ ssh customusername#207.196.153.34
Last login: Thu Dec 29 11:56:53 2022 from 82.204.238.49
customusername#customdomain.com [~]# mongod -version
db version v6.0.3
Build Info: {
"version": "6.0.3",
"gitVersion": "f803681c3ae19817d31958965850193de067c516",
"openSSLVersion": "OpenSSL 1.0.1e-fips 11 Feb 2013",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "rhel70",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
customusername#customdomain.com [~]# mongosh --version
1.6.1
customusername#customdomain.com [~]# mongosh -nodb
Current Mongosh Log ID: 63abf66ca872ab436a8040c8
Using Mongosh: 1.6.1
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
> use test-db
MongoshInvalidInputError: [SHAPI-10004] No connected database
> exit
customusername#customdomain.com [~]# mongosh --username customusername
Enter password: ********************************
Current Mongosh Log ID: 63abf8c15a47f0e078be3d76
Connecting to: mongodb://<credentials>#127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.1
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
customusername#customdomain.com [~]#
I am able to pull up "help" files. And I've tried multiple variations of # mongod mongodb://customusername#127.0.0.1:27017/ which fail.
customusername#customdomain.com [~]# mongod help
customusername#customdomain.com [~]# mongosh -h
customusername#customdomain.com [~]# mongod mongodb://customusername#127.0.0.1:27017/
That last command returns a few lines which indicate it might be working, then it fails with message: "Invalid command: mongodb://customusername#127.0.0.1:27017/" And then it displays a number of "General options" such as "-h [ --help ]". I can't make sense of most of them.
Note that I cannot install MongoDB/mongosh locally because my machine is too old, so I have to be able to log in through SSH and handle it that way. If I can figure it out via SSH, maybe I'll try something like MongoDB Compass next, which I am able to install locally.
Thank you for your time and assistance.
UPDATE: I think I've made progress, but am still not there yet.
I think I got MongoDB running with this, after creating a directory called "mongodb" to hold data files:
# mongod --port 28015 --dbpath ./mongodb/
A string of messages come up in my terminal, with the last being related to listening ("ctx":"listener","msg":"Waiting for connections"), and the process appears to continue running.
I then open a new terminal window, SSH in, and run this:
# mongosh --port 28015
Which appears to land me in a mongo shell in which I'm able to create a new collection:
test> use test-db
switched to db test-db
test-db>
Also, I notice that the original terminal window with the open process outputs connection-related messages as I interact with the second window (e.g. "ctx":"listener","msg":"Connection accepted").
I looked inside of the /mongodb/ directory and there are a number of mongo-related files in there now, "collection-0-5001736531146682033.wt", "index-1-5001736531146682033.wt", "storage.bson", and so forth. So, I think the db creation is working.
UPDATE TWO
Was struggling with why the show command was throwing an error, since use was already working, then figured out I needed to insert a document first:
test> db.tours.insertOne({name: "Some Name", price: 19, rating: 3.5})
The full command is show dbs and then the "test" database appears.
So, you can check out my two updates above. I was able to access and do very basic operations with MongoDB on a VPS with SSH.
Also, here are some helpful links, if you struggle with similar issues.
Mongo Shell basics:
https://www.mongodb.com/basics/get-started#get-started-with-mongodb
(skip down to the "Get Started with MongoDB" subsection, since the top of the page is about the Atlas interface)
Setting up admin and user accounts via command line:
https://medium.com/#haxzie/getting-started-with-mongodb-setting-up-admin-and-user-accounts-4fdd33687741
Good luck!

Enabling Access Control

I recently installed mongo db and ran it for the first time. I encountered this warning,
Access control is not enabled for the database.
Read and write access to data and configuration is unrestricted.
I fixed this by enabling access control using these commands
db.createUser(
{
user:"myuserName",
pwd:"myPassword",
roles:[{role: "userAdminAnyDatabase", db:"admin"}]
})
From the docs, I have to restart mongodb with this command:
mongod --auth --port 27017 --dbpath /data/db
but when I try to restart mongodb after access control, I get this error.
2017-08-17T19:11:14.337+0530 E QUERY [thread1] SyntaxError: missing ; before statement #(shell):1:9
Is the command wrong or am I missing something here. Please walk me through this. Thanks.

How to change default authentication method from SCRAM-SHA-1 back to MONGODB-CR

I just upgrade to mongodb 3.0 from 2.4 and I am getting this error when I log in:
"Failed to authenticate thisuser#admin with mechanism MONGODB-CR: AuthenticationFailed MONGODB-CR credentials missing in the user document"
Elsewhere on stackoverflow I found people who had hacks for this, but where can I specify for the whole database to use CR as the method? Isn't this a global setting?
I'm using pymongo and it has CR style instructions that work fine for me.
After reading the same answer from several sources, I found a detailed enough step-by-step fix for this that is worth sharing back here. The paths are specific to webfaction.com, but you can obvious adjust ports and paths to suit yourself.
1) Start MongoDB 3.0 without --auth enabled, so you can change how it authenticates.
from /webapps/mongo/bin/ run
./mongod --dbpath $HOME/webapps/mongo/data --port 1400
SSH login as admin
from /webapps/mongo/bin/ run
./mongo localhost:1400/admin
2) Run mongodb within SSH on the admin database:
>
> var schema = db.system.version.findOne({"_id" : "authSchema"})
> schema.currentVersion = 3
> db.system.version.save(schema)
> exit
currentVersion = 3 will make the default MONGODB-CR, the default for MongoDB version 2x. Version 3.0 uses SCRAM-SHA-1 by default instead.
3) restart MongoDB with --auth enabled.
from /webapps/mongo/bin I would run
./mongod --dbpath $HOME/webapps/mongo/data --setParameter authenticationMechanisms=MONGODB-CR --auth --port 1400
... and if I want this service to remain active it would be:
#reboot nohub nice <FULL PATH>/mongod --dbpath $HOME/webapps/mongo/data --setParameter authenticationMechanisms=MONGODB-CR --auth --port 1400
The setParameter authenticationMechanisms=MONGODB-CR may be redundant, or meaningless, but I'll leave it in there for now.
Now I can remotely connect so long as my user account is associated with that database, using db.grantRolesToUser( "marc", [ {role: "readWrite", db:"fbc"}, {role: "dbOwner", db:"fbc"}] ) ... and so forth in the admin database.

Moved Mongo Database to Different Drive: Unable to acquire lock for lockfilepath

I am in the process of moving my mongo data to a different drive. All of the data I want to move is stored in /data/db and I am moving it to a NAS (Network attached storage).
First step:
mongodump -d mydb -c mycollection -o nas/mongo-temp
This created a file tree in mongo-temp/ like so:
dump
`-- mydb
`-- mycollection.bson
1 directory, 1 file
I then stopped the mongod service and created a new /data/db directory:
/etc/init.d/mongod stop
mkdir mongo-temp/data/db
...and changed the dbpath line in /etc/mongodb.conf
dbpath=.../mongo-temp/data/db
I successfully restarted the mongo server using /etc/init.d/mongod start.
When I try to connect:
mongo
MongoDB shell version: 1.6.4
Thu May 3 09:53:23 *** warning: spider monkey build without utf8 support. consider rebuilding with utf8 support
connecting to: test
Thu May 3 09:53:24 Error: couldn't connect to server 127.0.0.1 (anon):1154
exception: connect failed
I've tried to start mongod with the command mongod --dbpath .../mongo-temp/data/db but I get an error that says:
Thu May 3 09:57:26 exception in initAndListen std::exception: Unable to acquire lock for lockfilepath: /home/dlpstats/nas-mnt/mongo-temp/data/db/mongod.lock
Removing the lockfile doesn't help. If I run the mongod command without --dbpath, the server starts fine and I am able to make queries on my old database.
First, you mentioned that you used mongodump to populate the new drive - was this just a method of backing things up or did you intend that to be the new database files? That is not how it works - mongodump output is not the same as a database file - it needs to be re-imported with mongoresore in fact. If you do a straight data file copy then the transfer will be seamless.
Then, as well as the permissions suggested by Wes in his answer, a few more things to check:
That you have shut down the old server successfully and completely - it's possible it's mis-reported error and you are getting it because it is trying to grab a port that is already open
You are using version 1.6.4 according to the mongo shell output, my guess is that you installed from the Ubuntu repo for 11.04 or similar, that is not a good option - 1.6 is very old at this point. Use the 10gen repos (http://www.mongodb.org/display/DOCS/Ubuntu+and+Debian+packages) or download the binaries and get a more recent version
Last but not least, when you start the mongod manually, make sure all the arguments are the same, like the port. When you connect via the mongo shell, specify the port you started the mongod on - don't rely on defaults when running into issues like this, be explicit.
I faced this problem and issuing following command solved my problem:
rm /var/lib/mongodb/mongod.lock
And then restart the mongod.
But I'm not sure is it a good solution or not.
Check the permissions for the directory and parent directories of mongo-temp. Presumably it's running as the mongodb user?
You need execute permissions on the directory (and parent directories) in order to create files there. Execute permissions on a directory allow you to list the files there, which is needed to be able to open the file for writing.

Creating a database in Mongo: can't connect, get "connect failed"

I want to create a new database in Mongo. However, I'm having trouble connecting:
:~$ mongo
MongoDB shell version: 1.6.5
connecting to: test
Tue Dec 21 18:16:25 Error: couldn't connect to server 127.0.0.1 (anon):1154
exception: connect failed
How can I connect to mongo in order to create a new database? Alternatively, can I create a new database from the command line?
Slightly surprisingly, the Mongo docs don't seem to cover how to create a database.
Thanks.
In order to open Mongo JavaScript shell, a Listener should be initialized first.
So, first run mongod.exe before running mongo.exe. Both are in the same location(/bin).
There is no separate commands to create a db in mongodb. Just type "use dbname;" in console. Now you have created a db of the name 'dbname'. Now, if you type 'show databases' you cannot see the db name you just created. Because, mongo will not create any db, util you create collection and insert a document into that collection.
Hope this is useful to you!
cd /var/lib/mongodb/
remove mongod.lock file from this folder
sudo start mongodb (in console)
mongo (in console)
And it runs fine.
First you'll need to run mongod on one terminal. Then fire up another terminal and type mongo. This shall open the mongo shell. You also need to create /data/db/ where mongo will store your databases.
You'll need to run mongod (the daemon) before you can use mongo (the client), it's easiest to just run it in another shell; These should be in your path if mongo is installed correctly. After that the docs should get you through creating and editing dbs and collections.
Just try following commands in given order :
sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair
sudo service mongodb start
sudo service mongodb status