The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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

Discussion in 'cPanel Developers' started by anuphariharan, Jul 15, 2011.

  1. anuphariharan

    anuphariharan Registered

    Joined:
    Jul 15, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    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...
     
  2. donnacha

    donnacha Registered

    Joined:
    Jun 8, 2003
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    1
    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?
     
  3. sirdopes

    sirdopes Well-Known Member
    PartnerNOC

    Joined:
    Sep 25, 2007
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    16
    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);
    
    
    }
    
    
     
Loading...

Share This Page