Jump to content

Create Mysql Databases From Php Script


maicol07

Recommended Posts

Well, technically it's possible. Anything is possible right? You would have to curl in to cpanel, and get a session token, and then use that session token to automate going to the mysql page and submitting a new database. It would be pretty complicated to do.

Link to comment
Share on other sites

Well, I have found some code on the web... what do you think?

  1. http://www.zubrag.com/scripts/cpanel-database-creator.php
  2. require("xmlapi.php"); // this can be downlaoded from https://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php
    $xmlapi = new xmlapi("your cpanel domain");   
    $xmlapi->set_port( 2083 );   
    $xmlapi->password_auth($opts['user'],$opts['pass']);    
    $xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false 
    
    $cpaneluser=$opts['user'];
    $databasename="something";
    $databaseuser="else";
    $databasepass=$opts['pass'];
    
    //create database    
    $createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));   
    //create user 
    $usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));   
    //add user 
    $addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array("".$cpaneluser."_".$databasename."", "".$cpaneluser."_".$databaseuser."", 'all'));
    

    Source: https://stackoverflow.com/questions/11989920/create-cpanel-database-through-php-script

     

  3. <?php  
    
    include("xmlapi.php");  
    $db_host = "localhost";  
    $cpuser = "myuser";  
    $databasename = 'mydatabasename';//do not prepend with username
    $databaseuser = 'mydatabaseuser';//api will do that for you
    $databasepass = '123456';
    
    $xmlapi = new xmlapi($db_host);  
    $xmlapi->password_auth("root","root_pass");  
    $xmlapi->set_debug(1);//this setting will put output into the error log in the directory that you are calling script from 
    $xmlapi->set_output('array');//set this for browser output
    //create database  
    $createdb = $xmlapi->api1_query($cpuser, "Mysql", "adddb", array($databasename)); 
    foreach($createdb as $v)
    {
        $result = $v['result'];
    }
    if ($result == 1)
    {
        //create user  
        $usr = $xmlapi->api1_query($cpuser, "Mysql", "adduser", array($databaseuser, $databasepass));  
    }
    foreach($usr as $v)
    {
        $result2 = $v['result'];
    }
    if ($result2 == 1)
    {
        //add user to database  
        $addusr = $xmlapi->api1_query($cpuser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));  
    
    }
    print_r($addusr);
    
    
    ?>
    
    Source: https://forums.cpanel.net/threads/create-database-without-login-to-cpanel.130825/
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...