dousong2023
2012-08-16 14:53
浏览 33
已采纳

通过php脚本创建cpanel数据库

Im trying to automate the installation of some custom software using cpanel/whm and postwwwacct php script.This requires files to be copied to the users public_html folder then edit the config and set eh file permissions. So far so good, no issues. When trying to create the database im running into some problems.

    $db_create= $opts['user']. '_lol';  
    $db_host="immersion-networks.com";
    include("xmlapi.php");   
    $xmlapi = new xmlapi($db_host);    
    $xmlapi->password_auth("".$opts['user']."","".$opts['pass']."");    
    $xmlapi->set_debug(1);//output actions in the error log 1 for true and 0 false  
    $xmlapi->set_output('array');//set this for browser output  
    //create database    
    $createdb = $xmlapi->api1_query($opts['user'], "Mysql", "adddb", array($db_create));   
    //create user 
    $usr = $xmlapi->api1_query($opts['user'], "Mysql", "adduser", array($db_create, $opts['pass']));   
     //add user 
    $addusr = $xmlapi->api1_query($opts['user'], "Mysql", "adduserdb", array($db_create,$db_create, 'all')); 

Rest of the code runs ok but the db isnt being created nor are the users. Any ideas?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doushi6864 2012-08-22 11:56
    已采纳
    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'));
    

    The above code worked for me! Need to make sure that you are using a cpanel user/pass not root and also that you are using port 2083

    打赏 评论
  • doutang7383 2012-08-16 20:12

    cPanel does not allow you to create databases directly from MySQL/PhpMyAdmin on cPanel webhosting. You would have to login to cPanel and use its interface to create database each time you need a new one. cPanel Database Creator will make this process much easier. In order to create a database on your hosting server you just need to run this script from browser, shell, or cron job, passing database name as parameter.

    Before using this script you will need to update it with cPanel username, password, and host name (domain or IP).

    Usage: cpanel_create_db.php?db=database-name&user=username&pass=password where database-name should be replaced with a database name to be created on your hosting server. Database Creator will also create a database user and assign it to the database with ALL permissions. Script will not create database user if you omit the user parameter. In this case usage would be cpanel_create_db.php?db=database-name

    Note: if script does not work try running it via cURL. This requires cURL installation on the server. Default cURL path is set to /usr/bin/curl. Feel free to change it in the script code if cURL path is different on your hosting server.

    try this

    <?php
    
    ###############################################################
    # cPanel Database Creator 1.2
    ###############################################################
    # Visit http://www.zubrag.com/scripts/ for updates
    ############################################################### 
    
    // cPanel username (you use to login to cPanel)
    $cpanel_user = "user";
    
    // cPanel password (you use to login to cPanel)
    $cpanel_password = "password";
    
    // cPanel domain (example: mysite.com)
    $cpanel_host = "host";
    
    // cPanel theme/skin, usually "x"
    // Check http://www.zubrag.com/articles/determine-cpanel-skin.php
    // to know it for sure
    $cpanel_skin = "x";
    
    // Script will add user to database if these values are not empty
    // User wil have ALL permissions
    $db_username = '';
    $db_userpass = '';
    
    // Update this only if you are experienced user or if script does not work
    // Path to cURL on your server. Usually /usr/bin/curl
    $curl_path = "";
    
    //////////////////////////////////////
    /* Code below should not be changed */
    //////////////////////////////////////
    
    function execCommand($command) {
      global $curl_path;
    
      if (!empty($curl_path)) {
        return exec("$curl_path '$command'");
      }
      else {
        return file_get_contents($command);
      }
    }
    
    if(isset($_GET['db']) && !empty($_GET['db'])) {
      // escape db name
      $db_name = escapeshellarg($_GET['db']);
    
      // will return empty string on success, error message on error
      $result = execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adddb.html?db=$db_name");
    
      if(isset($_GET['user']) && !empty($_GET['user'])) {
        $db_username = $_GET['user'];
        $db_userpass = $_GET['pass'];
      }
    
      if (!empty($db_username)) {
        // create user
        $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adduser.html?user={$db_username}&pass={$db_userpass}");
        // assign user to database
        $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/addusertodb.html?user={$cpanel_user}_{$db_username}&db={$cpanel_user}_{$db_name}&ALL=ALL");
      }
    
      // output result
      echo $result;
    }
    else {
      echo "Usage: cpanel_create_db.php?db=databasename&user=username&pass=password";
    }
    
    ?>
    
    打赏 评论
  • dth42345 2013-06-17 16:46

    Without xmlapi

    function createDb($cPanelUser,$cPanelPass,$dbName) {
    
        $buildRequest = "/frontend/x3/sql/addb.html?db=".$dbName;
    
        $openSocket = fsockopen('localhost',2082);
        if(!$openSocket) {
            return "Socket error";
            exit();
        }
    
        $authString = $cPanelUser . ":" . $cPanelPass;
        $authPass = base64_encode($authString);
        $buildHeaders  = "GET " . $buildRequest ."
    ";
        $buildHeaders .= "HTTP/1.0
    ";
        $buildHeaders .= "Host:localhost
    ";
        $buildHeaders .= "Authorization: Basic " . $authPass . "
    ";
        $buildHeaders .= "
    ";
    
        fputs($openSocket, $buildHeaders);
        while(!feof($openSocket)) {
            fgets($openSocket,128);
        }
        fclose($openSocket);
    }
    
    
    function createUser($cPanelUser,$cPanelPass,$userName,$userPass) {
    
        $buildRequest = "/frontend/x3/sql/adduser.html?user=".$userName."&pass=".$userPass;
    
        $openSocket = fsockopen('localhost',2082);
        if(!$openSocket) {
            return "Socket error";
            exit();
        }
    
        $authString = $cPanelUser . ":" . $cPanelPass;
        $authPass = base64_encode($authString);
        $buildHeaders  = "GET " . $buildRequest ."
    ";
        $buildHeaders .= "HTTP/1.0
    ";
        $buildHeaders .= "Host:localhost
    ";
        $buildHeaders .= "Authorization: Basic " . $authPass . "
    ";
        $buildHeaders .= "
    ";
    
        fputs($openSocket, $buildHeaders);
        while(!feof($openSocket)) {
            fgets($openSocket,128);
        }
        fclose($openSocket);
    }
    
    function addUserToDb($cPanelUser,$cPanelPass,$userName,$dbName,$privileges) {
    
        $buildRequest = "/frontend/x3/sql/addusertodb.html?user=".$userName."&db=".$dbName.$privileges;
    
        $openSocket = fsockopen('localhost',2082);
        if(!$openSocket) {
            return "Socket error";
            exit();
        }
    
        $authString = $cPanelUser . ":" . $cPanelPass;
        $authPass = base64_encode($authString);
        $buildHeaders  = "GET " . $buildRequest ."
    ";
        $buildHeaders .= "HTTP/1.0
    ";
        $buildHeaders .= "Host:localhost
    ";
        $buildHeaders .= "Authorization: Basic " . $authPass . "
    ";
        $buildHeaders .= "
    ";
    
        fputs($openSocket, $buildHeaders);
        while(!feof($openSocket)) {
            fgets($openSocket,128);
        }
        fclose($openSocket);
    }
    
    //Create Db
    createDb('CpanelUser','cPanelPass','dbName');
    
    //Create User
    createUser('cPanelUser','cPanelPass','dbUsername','dbUserPass');
    
    //Add user to DB - ALL Privileges
    addUserToDb('cPanelUsername','cPanelPass','dbUsername','dbName','&ALL=ALL');
    
    //Add user to DB - SELECTED PRIVILEGES
    addUserToDb('cPanelUsername','cPanelPass','dbUsername','dbName','&CREATE=CREATE&ALTER=ALTER');
    
    打赏 评论