Adduser thru a perl script

54 views
Skip to first unread message

Marcelo Correa

unread,
Apr 4, 2014, 11:46:54 PM4/4/14
to mongod...@googlegroups.com
Hi There,

There is any way to add a user to a database from a perl script (using the mongodb module)

Thanks a lot..
db.addUser( "guest", "pass") in a perl script??!! (use MongoDB)

Asya Kamsky

unread,
Apr 5, 2014, 8:53:12 PM4/5/14
to mongodb-user
Sure. One trick you can use to see what the shell is doing when you
run a helper is leave off the parens and see the source (in JS) then
you should be able to do the equivalent in your language of choice:

> db.addUser
function () {
if (arguments.length == 0) {
throw Error("No arguments provided to addUser");
}
if (typeof arguments[0] == "object") {
this._addUser.apply(this, arguments);
} else {
this._addUserV22.apply(this, arguments);
> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user"
> group.
>
> For other MongoDB technical support options, see:
> http://www.mongodb.org/about/support/.
> ---
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongodb-user...@googlegroups.com.
> To post to this group, send email to mongod...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongodb-user.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mongodb-user/334e5abb-468f-4355-8a2c-ba203d63b59b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Marcelo Correa

unread,
Apr 6, 2014, 4:07:42 PM4/6/14
to mongod...@googlegroups.com
Hi there,

Thanks for your help.

I am unable to replicate the situation above..

> use admin
switched to db admin
>
> db.adduser
admin.adduser
>
> admin.adduser("admin","admin")  //
Sun Apr  6 20:42:18.700 ReferenceError: admin is not defined
>
> show dbs
local    0.078125GB
>
.... Using db..adduser("admin","admin")  works nice :)

There is any way to call "this._addUser.apply(this, arguments); " outside the shell..

in perl we got:
my $result = $db->run_command({ismaster => 1});
We can run it like the previous?

best regards,

Marcelo Correa

unread,
Apr 12, 2014, 4:18:22 PM4/12/14
to mongod...@googlegroups.com
The answer to my question!!

Not being able to find any fancy way to add a user to a database using the MongoDB module.
I manage to do-it with a simple pipe to manage the mongo shell (code below) not very elegant but......

 
 
 
1 #!/usr/bin/perl
 
2
 
3 use strict;
 
4 use warnings;
 
5 #use MongoDB; # not needed :)
 
6
 
7 my $cmd = "/usr/local/bin/mongo --host localhost --port 27017";
 
8 my @cmds = ( "use admin", "db.addUser('admin','admin')", "db.auth('admin','admin')" );
 
9
 
10 open(CHILD, "| $cmd 2>&1") or die "Can't open: $!";
 
11 foreach my $line (@cmds){ print CHILD $line."\n"; }
 
12 close(CHILD);
 13


Reply all
Reply to author
Forward
0 new messages