Default Automatically create mysql user and database for new WHM accounts?

anuphariharan

Registered
Jul 15, 2011
1
0
51
Is there a way to set up WHM so when a new account is created it automatically creates a MySQL account too, and ideally a new database for the user?


In the following thread http://forums.cpanel.net/f5/automatically-create-mysql-user-database-new-whm-accounts-121961.html
The user told us that we need to create and place the script inside "postwwwacct" folder.

I have few question :
1) Is this going to be a PERL script.
2) Can someone please pass a simple code sample which creates the Database,DB user, Password. DB user should correspond to the current user account.

Thanks in advance...
 

donnacha

Registered
Jun 8, 2003
2
0
151
Re: Default Automatically create mysql user and database for new WHM accoun

This would be the holy grail for me too, I've wasted hours searching for a simple code sample.

Why on Earth isn't this a standard option in WHM?
 

sirdopes

Well-Known Member
PartnerNOC
Sep 25, 2007
141
0
66
Re: Default Automatically create mysql user and database for new WHM accoun

The following is an old example I wrote that does what you are asking. It would be better to change it to call the api calls directly instead of make a GET request to localhost though.

Code:
#!/usr/bin/perl

use LWP::UserAgent;

if ($#ARGV > 1) {
        open(AHASH, "/root/.accesshash");
        my @access = <AHASH> ;
        close AHASH;
        my $hash = join("\n", @access);
        $hash =~ s/\n//g;
        my $dbuser = "cpanel1";
        my $dbpass = "somesecurepass";
        my $dbname = "cpanel1";
        my %OPTS = @ARGV;
        my $user = $OPTS{'user'};
        my $auth = "WHM root:" . $hash;
        my $ua = LWP::UserAgent->new;

        #add database
        my $request = HTTP::Request->new( GET => "http://localhost:2086/xml-api/cpanel?user=$user&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&cpanel_xmlapi_apiversion=1&arg-0=$dbname" );
        $request->header( Authorization => $auth );
        my $response = $ua->request($request);

        #add database user
        $request = HTTP::Request->new( GET => "http://localhost:2086/xml-api/cpanel?user=$user&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduser&cpanel_xmlapi_apiversion=1&arg-0=$dbuser&arg-1=$dbpass" );
        $request->header( Authorization => $auth );
        $response = $ua->request($request);

        #add user to database
        $request = HTTP::Request->new( GET => "http://localhost:2086/xml-api/cpanel?user=$user&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&cpanel_xmlapi_apiversion=1&arg-0=$user\_$dbname&arg-1=$user\_$dbuser&arg-2=ALL" );
        $request->header( Authorization => $auth );
        $response = $ua->request($request);


}