Create database and MongoDB user via shell

0

I need to create a MongoDB database and user, but everything via shell.

I found something about --eval and --shell, but I could not get it to work, I'm trying this way:

mongo mongodb://11.11.123.321:27017 --eval "db.createUser({user: "teste", pwd: "teste123", roles: [{role: "readWrite", db: "novaBase"}]})"

Of course, if I run the initial command give an enter and then run the second one will work, but the current scenario I need to do all this "all at once" with an ssh lib that connects to the server and runs the command, I do this with mysql already example:

mysql -u root -pteste123 -e "CREATE USER teste@localhost"........

    
asked by anonymous 28.09.2018 / 22:44

1 answer

0

Friend, as you did not post the error returned, it is difficult to be sure, but I believe the problem is that the delimiter of your command are double quotation marks, which are used in the command itself.

Try running with single quotation marks like this:

mongo mongodb://11.11.123.321:27017 --eval 'db.createUser({user: "teste", pwd: "teste123", roles: [{role: "readWrite", db: "novaBase"}]})'

If it does not work, share the error so we can help you better.

    
11.10.2018 / 23:26