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.

PHP create DB and user

Discussion in 'cPanel Developers' started by deros, Oct 3, 2009.

  1. deros

    deros Registered

    Joined:
    Oct 1, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    I know about postwwwacct scripts, I'd like to have mine create a default DB and assign a user based on the username from the account creation form, how do I connect to mysql in order to create the DB and user. is the default username root and password blank? or is it something else?

    My ultimmate goal is to have accounts created with a cms pre-installed.
     
  2. UNIXy

    UNIXy Well-Known Member

    Joined:
    Sep 21, 2009
    Messages:
    75
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    Houston, Texas, USA
    cPanel Access Level:
    DataCenter Provider
    Hi,

    The postwwwacct script can be and kind of script that invokes an installed interpreter (perl, bash, php, etc). The new user account information is passed to the script as arguments. It's a simple three-liner to create the database as you described. By the way, the root password is not blank but stored under /root/.my.cnf. It's perceived to be blank because the mysql command checks for /root/.my.cnf and extracts the password from there for you as a convenience.

    Feel free to get in touch if you have any question / comment.

    Best
     
  3. MattDees

    MattDees cPanel Product Owner
    Staff Member

    Joined:
    Apr 29, 2005
    Messages:
    417
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Well, you have a couple of options here. The easiest way would probably be using the XMLAPI PHP class, (see the first sticky in this forum). You can take the data passed into postwwwacct (see: ScriptHooks < DevHooks < TWiki ) then use the XMLAPI class to connect to local host to create your mysql user and database.

    If you need more information on how the specific API calls to make please see:

    MysqlRef < DeveloperResources/ApiRef < TWiki

    Specifically the adddb adduser and adduserdb calls.

    If you need help any further assistance please respond to this thread and I (or someone else) should be able to help you.
     
  4. deros

    deros Registered

    Joined:
    Oct 1, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    OK I got my hands on the xmlapi class I looked through the examples in the archive but Im not exactly sure how to use the MySQL ref's with the api, can you give me an example of how to use it?

    I assume it would look something like this:
    PHP:
    $opts = array();
    $argv0 array_shift($argv);
    while(
    count($argv)) {
    $key array_shift($argv);
    $value array_shift($argv);
    $opts[$key] = $value;
    }

    include(
    "xmlapi.php.inc");

    $ip "127.0.0.1";
    $root_pass "password";

    $account $opts['user'];
    $username "someusername";
    $userpassword $opts['pass'];
    $dbname "dbname";
    $perms "grant all";
    $xmlapi = new xmlapi($ip);
    $xmlapi->password_auth("username",$root_pass);

    $xmlapi->set_debug(1);
    print 
    $xmlapi->api1_query($account"Mysql""adddb"$dbname );
    print 
    $xmlapi->api1_query($account"Mysql""adduser", array($username$userpassword) );
    print 
    $xmlapi->api1_query($account"Mysql""adduserdb", array($dbname$username$perms) );
    am I close?
     
    #4 deros, Oct 6, 2009
    Last edited: Oct 6, 2009
  5. deros

    deros Registered

    Joined:
    Oct 1, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Ok I did some more research in order to get a DB and user made on account creation but still nothing. This is what I have in my postwwwacct script

    PHP:
    #!/usr/local/bin/php -q
    <?php
    $opts 
    = array();
    $argv0 array_shift($argv);
    while(
    count($argv)) {
    $key array_shift($argv);
    $value array_shift($argv);
    $opts[$key] = $value;
    }

    include(
    "xmlapi.php.inc");

    $db_host "127.0.0.1";
    $cpuser $opts['user'];
    $cppass $opts['pass'];

    $username "name";
    $userpass "password".$cpuser;
    $dbname "dbname";
    $perms "grant all";

    $xmlapi = new xmlapi($db_host);
    $xmlapi->set_port(2083);
    $xmlapi->password_auth($cpuser,$cppass);
    $xmlapi->set_debug(1);

    print 
    $xmlapi->api1_query($cpuser"Mysql""adddb", array($dbname) );
    print 
    $xmlapi->api1_query($cpuser"Mysql""adduser", array($username$userpass) );
    print 
    $xmlapi->api1_query($cpuser"Mysql""adduserdb", array($dbname$username$perms) );  
    ?>
     
  6. cPanelDavidG

    cPanelDavidG Technical Product Specialist

    Joined:
    Nov 29, 2006
    Messages:
    11,279
    Likes Received:
    8
    Trophy Points:
    38
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    When you add a user to a database, be sure to prefix both the database name and the user name with the cPanel user's username.

    For example, if you use the API to create a db named "data1" and a user named "user1" on a cPanel account "cpuser," that would result in a database named cpuser_data1 and a user named cpuser_user1.

    Therefore, to add user1 to data1, you need to use cpuser_data1 and cpuser_user1 as your user name and database name when calling that API function.

    The permissions parameter is not the same as "grant all" and similar SQL commands. Instead, this is a space delimited list of parameters to issue to the user. These parameters are usually all uppercase. For example, $perms = 'ALL'; in your script would grant this user all permissions to the database.
     
Loading...

Share This Page