SyntaxError when I create User

Hello! Please, help me. I can’t find a SyntaxError

mongo admin --host localhost:27000 --eval 'db.createUser({ user: “m103-admin”, pwd: “m103-pass”, ro mongo admin --host localhost:27000 --eval ‘db.createUser({ user: “m103-admin”, pwd: “m103-pass”, roles: [{role: “root”, db: “admin”}]})’
2019-02-21T18:39:43.802+0000 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:6
image

Thanks !!

The command ‘mongo …’ starts the mongo shell.

When you see the prompt ‘MongoDB Enterprise >’ you are in the mongo shell. You do not enter the ‘mongo …’ command. You are already there.

The command ‘mongo …’ with --eval is to allow you to send command to mongo without entering the mongo shell. When you are in the mongo shell you just type whats in the quotes of --eval. In this case, db.createUser(…).

The following might help you:

https://docs.mongodb.com/manual/mongo/

3 Likes

ok. Trying to do it from Vagrant and get such error
image

This is my Mongod
image

Your mongod instance already has a user. The localhost exception is closed. You must authenticate to be able to create a new user.

1 Like

“You must authenticate to be able to create a new user” … sorry, but how?
My steps:

  1. Create a data database in Vagrant ssh:
    vagrant@m103:~$ mongod --port 27000 --dbpath /data/db --bind_ip 192.168.103.100,localhost --auth
  2. In another window in Vagrant ssh:
    vagrant@m103:~$ mongo admin --host localhost:27000 --eval ‘db.createUser({user: “m103-admin”, pwd: “m103-pass”, roles:
    [{role: “root”, db: “admin”}]})’
  3. Error: couldn’t add user: there are no users authenticated :
    _getErrorWithCode@src/mongo/shell/utils.js:25:13
    DB.prototype.createUser@src/mongo/shell/db.js:1437:15
    @(shell eval):1:1

Where is a problem?

And if I entered in Mongo (port 27000) and type: show dbs - there are any database. Is it normal?

I cannot write better than what’s written at the link I already provided:

https://docs.mongodb.com/manual/mongo/

Specially at the section ** MongoDB Instance with Authentication**.

1 Like

Ok. thanks. I’ll be trying))

I did it in that way:
load MongoD on Vagrant ssh only with --port 27000
load Mongo on Vagrant ssh only with --port 27000
create new User
load MongoD on Vagrant ssh with: mongod --port 27000 --dbpath /data/db --bind_ip 192.168.103.100,localhost --auth
in Vagrant ssh: mongo --username m103-admin --password m103-pass --authenticationDatabase admin --port 27000
and I’ve got positive results.

Thanks for advice!

1 Like