Create Mongodb database and user via shell

Asked

Viewed 96 times

0

I need to create database and Mongodb user, but all via shell.

I found something about --Eval and --shell, but I couldn’t make it work, I’m trying it that 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 a enter and then run the second will work, but the current scenario I need to do all this "at once", with an ssh lib that connects on the server and runs the command, I do this with mysql already example:

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

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 quotes, which are used in the command itself.

Try running with simple quotes like this:

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

If it doesn’t work, share the error so we can help you better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.