Community Forums
Connect with us on LinkedIn
+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    7

    Default How to automate SQL database creation upon new user creation

    I would like to set up whm so that whenever a user is created a certain database with tables will be installed automatically on to the users domain, how can that be done? I guess I have to write some code, but I don't know where..

  2. #2
    Member
    Join Date
    May 2007
    Posts
    19

    Default

    New cPanel accounts can be created through the following cPanel script:

    /scripts/wwwacct

    Its most basic syntax is:

    /scripts/wwwacct primarydomainname.com cpaneluser cpanelpassword

    You can modify it to establish a mysql connection, create a new database and import the database tables in it after the account creation.
    SiteGround Hosting Team Member
    We know cPanel because our customers love it!
    Check out our Friends Special: One year hosting for $9.95 only.

  3. #3
    Member
    Join Date
    Feb 2010
    Posts
    7

    Default

    Code:
    #!/usr/bin/perl
    # cpanel - wwwacct                                Copyright(c) 2008 cPanel, Inc.
    #                                                           All rights Reserved.
    # copyright@cpanel.net                                         cPanel Inc.
    # This code is subject to the cPanel license. Unauthorized copying is prohibited
    
    my $wwwacct = -x '/usr/local/cpanel/bin/wwwacct.pl' ? '/usr/local/cpanel/bin/wwwacct.pl' : '/usr/local/cpanel/bin/wwwacct';
    exec $wwwacct, @ARGV;
    Is in that file, what do I do with that?
    Where do I find the syntax that I can use?

  4. #4
    cPanel Staff cPanelMatt's Avatar
    Join Date
    Apr 2005
    Location
    Houston, TX
    Posts
    409

    Default

    So, in order to do this, I would use a script hook by creating a shell script at

    /scripts/postwwwacct

    all information used for account creation is passed to this script as a hash. ( see: cPanel/WHM Script Hooks for more information)

    In conjunction with this, I would use the Mysql::adddb ( ApiMysql < ApiDocs < TWiki )call to create the database via the attached shell script.
    Attached Files
    Matt Dees
    Integration Developer
    cPanel, Inc.
    cPanel Integration Blog

  5. #5
    Member
    Join Date
    Feb 2010
    Posts
    7

    Default Step by step

    Is it possible to describe it step by step and in more detial, I'm new to WHM server administration.

  6. #6
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default a little more verbose

    Crinte,

    I'd do like cPanelMatt has suggested (and provided example script for).

    The idea is that immediately after a new user is created, the backend code looks for a shell script called postwwwacct in the /scripts directory. This pre/post shell script hook action is made into WHM and is documented here:
    cPanel/WHM Script Hooks

    By utilizing this hook, you could automate your database creation. You could write your own db creation script that uses the MySQL CLI (or other similar methods), however it's easier and more stable to use a cPanel/WHM API query.

    ApiMysql < ApiDocs < TWiki documents the database creation API1 call. cPanelMatt's file is an example of a postwwwacct script that does just that: gets the arguments (as passed to it by the backend code) and formats a query string, then performs the query on the localhost (using it's own hash access).

    Cheers,
    -David

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Default Is it possible to Hire somebody to do this?

    After having read this thread with great interest, I would gladly pay a developer who can initiate this on our server.

    Specifics for what I am looking for:

    USERNAME = the Account Username created during Account Setup/creation
    PASSWORD = the Account Password created during Account Setup/creation

    All new accounts created will have a database with the name:
    _subsite

    So when the account is created the database will be named:

    USERNAME_subsite

    Then it will need a Master User Set up

    _aDm1N

    Which will become:

    USERNAME_aDm1N

    and the password will be the same as the account password which was entered when the account was created in WHM.

  8. #8
    Member
    Join Date
    Feb 2010
    Posts
    7

    Default I got it sorted out!

    Modified a little bit to create user and add it to the database.
    NB! Make sure that postwwwacct has 755 permissions, otherwise it won't start at all
    Code:
    #!/usr/bin/perl
    
    ##
    # This script is for making queries against WHM for testing purposes.
    # it uses /root/.accesshash for grabbing authentication information
    ##
    
    use strict;
    use warnings;
    
    use LWP::UserAgent;
    use MIME::Base64;
    
    #convert input data to hash
    my %OPTS = @ARGV;
    my $dbname = "db";
    my $dbuser = int(rand(10000));
    my $dbpassword = int(rand(100000));
    my $perms = "select insert update delete create";
    my $create_db = '/xml-api/cpanel?user=' . $OPTS{'user'} . '&cpanel_xmlapi_apiversion=1&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&arg-0=' . $dbname;
    my $create_db_user = '/xml-api/cpanel?user=' . $OPTS{'user'} . '&cpanel_xmlapi_apiversion=1&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduser&arg-0=' . $dbuser . '&arg-1=' . $dbpassword;
    my $add_db_user = '/xml-api/cpanel?user=' . $OPTS{'user'} . '&cpanel_xmlapi_apiversion=1&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&arg-0=' . $dbname . '&arg-1=' . $dbuser . '&arg-2=' . $perms;
    
    query($create_db);
    query($create_db_user);
    query($add_db_user);
    
    sub query {
    	my ($call) = @_;
    	
    	# grab hash data
    	my $accesshash;
    	open(my $hash_fh, "<", "/root/.accesshash");
    	while (my $line = readline($hash_fh ) ) {
    		$accesshash .= $line;
    	}
    	close ($hash_fh);
    	
    	# clean of new lines
    	$accesshash =~ s/\n//g;
    	# create auth string
    	my $auth = "WHM root:$accesshash";
    	
    	#setup query object
    	my $ua = LWP::UserAgent->new;
    
    	# perform query
    	my $res = $ua->get("http://127.0.0.1:2086" . $call, Authorization =>$auth);
    
    	# print response from query
    	print $res->content;
    }
    Thanks cPanelMatt!

  9. #9
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default

    A lot of what this thread discusses is covered in a post on the Integration Blog. It's geared towards PHP developers, so it differs in that aspect from the original first few posts of this thread.

    Frequently Asked Scripts: "How do I automate XYZ after creating an account?" - cPanel Integration

    Enjoy,
    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

  10. #10
    Member
    Join Date
    Oct 2001
    Posts
    88

    Default Re: How to automate SQL database creation upon new user creation

    Hi,
    Thanks to crinte for posting this. I have added it to my postwwwacct and everything works great except setting the privileges.

    When I check the domain's cpanel nothing is set. I checked /usr/local/cpanel/logs/error_log and found "mysqladmin: You do not have access to that database " which is what WHM also spits out during the account creation.

    I understand what the error is but I'm not sure how to properly auth the function when calling it. I assume the line of my $auth = "WHM root:$accesshash"; should do that.

    Any ideas? I'm using WHM 11.28.60

    Thanks in advance!
    Last edited by asuservice; 01-20-2011 at 10:30 AM.
    Best Regards,
    Mike
    ASU Service
    http://www.NetBizHelpers.com

  11. #11
    Integration Developer cPanelDavidN's Avatar
    Join Date
    Dec 2009
    Location
    Houston, TX
    Posts
    525

    Default Re: How to automate SQL database creation upon new user creation

    Hi Mike,

    With you using 11.28.60 (and reviewing crinte's code), you're likely hitting a bug. There's a whole thread on the issue and a work around here: http://forums.cpanel.net/f42/case-45...se-182011.html

    You should see RELEASE and STABLE release tiers with the bug patched fairly soon. If you're updating based on the EDGE or CURRENT release tiers, then the fix is already there (they are at 11.28.73 as of this post).

    That being said, I'll advocate here (as I did in the other thread I linked above) to do the "work around" in your script. ie, alter your code to use literal values, that include a prefix (if prefixing is "on" [the default]), for the $dbname and $dbuser when performing the "adduserdb" call.

    Regards,
    -DavidN
    David Neimeyer
    Integration Developer

    sdk.cpanel.net
    APIs: XML-API API1 & API2
    Check Out: Developer Downloads Integration Blog
    Need Support? Support Ticket Developer Forum Feature Request

Similar Threads & Tags
Similar threads

  1. Automate subdomain creation for new accounts
    By Xavior82 in forum cPanel Developers
    Replies: 0
    Last Post: 10-15-2009, 02:21 AM
  2. automate pop account creation
    By josemv666 in forum cPanel and WHM Discussions
    Replies: 2
    Last Post: 03-20-2008, 09:18 AM
  3. Automate account creation
    By EstebanC in forum cPanel and WHM Discussions
    Replies: 0
    Last Post: 03-16-2008, 04:07 AM
  4. Need to automate creation of 400+ account
    By dochallam in forum cPanel and WHM Discussions
    Replies: 2
    Last Post: 06-08-2007, 10:37 AM
  5. How to automate new email account creation?
    By dinosm in forum cPanel and WHM Discussions
    Replies: 1
    Last Post: 07-22-2004, 04:59 PM
Linkedin       Facebook       Twitter       RSS       Flickr       YouTube