doushizhou4477 2015-12-10 21:40
浏览 40
已采纳

Softaculous如何在cPanel中创建MySQL数据库,用户和用户 - > db映射?

Currently, with PHP code, I'm creating MySQL databases in cPanel, then MySQL users, and then mapping them to the database, using these 4 PHP routines. The trouble is that every once in a blue moon, at a time when we think it's related to server load, this script says it's created the MySQL, user, and user->db mapping, but we can't see it in cPanel. When we go to MySQL command line, we see it there. And WordPress can't deploy to it -- says it can't reach the database. We have to go to command line MySQL to delete the database. When we manually run these routines under lower server load, the problem goes away. The problem is that I'm usually called back on the issue a day later than it actually occurred -- so I can't see what happened. And my logs just show me that WordPress couldn't deploy to the MySQL database. When I go looking, I don't see it in cPanel, but see it at command line in MySQL.

So, since these routines are problematic, I was wondering how Softaculous resolves this problem. Because even when my script fails, Softaculous still works just fine.

<?php

class cPanel {

public static function cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds = 0) {
    $sURL = ($sCpanelPort == 2083) ? 'https://' : 'http://';
    $sURL .= $sRootDomain . ':' . $sCpanelPort . '/xml-api/cpanel';
    $asHeader[0] = 'Authorization: Basic ' . base64_encode($sCpanelUser . ':' . $sCpanelPass) . "
" . 
        "Content-Type: application/x-www-form-urlencoded
" .
        "Content-Length: " . strlen($sPostData) . "
" . "
" . $sPostData;

    $hCurl = curl_init (); 
    curl_setopt($hCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
    curl_setopt($hCurl, CURLOPT_REFERER, 'https://www.google.com/?gws_rd=ssl#q=' . $sURL);
    curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($hCurl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($hCurl, CURLOPT_URL, $sURL);
    curl_setopt($hCurl, CURLOPT_HTTPHEADER, $asHeader);
    curl_setopt($hCurl, CURLOPT_POST, 1);
    if ($nTimeoutSeconds > 0) {
        @ curl_setopt($hCurl, CURLOPT_TIMEOUT, $nTimeoutSeconds); 
        @ curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 30);
    }
    $s = curl_exec($hCurl);
    return $s;
}

public static function cp_createDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName) {
global $gsCpanel_Last_Result;

    $sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName;
    $nTimeoutSeconds = 60;
    $s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds);
    $gsCpanel_Last_Result = "$s";
    if (strpos($s, '<result>1</result>') > 0) {
        $sDBName = $sCpanelUser . '_' . $sUnPrefixedDBName;
        return $sDBName;
    } else {
        return FALSE;
    }
}

public static function cp_createDBUser($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBUser, $sDBPass) {
global $gsCpanel_Last_Result;

    $sTest = $sCpanelUser . '_' . $sUnPrefixedDBUser;
    if (strlen($sTest)>16) {
        $sUnPrefixedDBUser = substr($sUnPrefixedDBUser, 0, (strlen($sUnPrefixedDBUser) - (strlen($sTest) - 16)));
    }
    $sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduser&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBUser . '&arg-1='. $sDBPass;
    $s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
    $gsCpanel_Last_Result = "$s";
    if (strpos($s, '<result>1</result>') > 0) {
        $sDBUser = $sCpanelUser . '_' . $sUnPrefixedDBUser;
        return $sDBUser;
    } else {
        return FALSE;
    }
}

public static function cp_addUserToDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName, $sUnPrefixedDBUser) {
global $gsCpanel_Last_Result;

    $sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName . '&arg-1=' . $sUnPrefixedDBUser . '&arg-2=all';

    $s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
    $gsCpanel_Last_Result = "$s";
    return (strpos($s, '<result>1</result>') > 0);
}

} // end class
  • 写回答

1条回答 默认 最新

  • doukong9316 2015-12-10 22:23
    关注

    Softaculous has a glorious SDK that's free here:

    http://www.softaculous.com/docs/File:Softaculous_Development_Kit.zip

    Then, this code should help you install a blog using it. It's blazingly fast -- much faster than cPanel APIs.

    <?php        
    
        // SOURCE: https://gist.github.com/mickaelandrieu/7271939
        require_once('sdk.php');
    
        $new = new Softaculous_API();
        $new->login = 'https://user:password@domain.com:2083/frontend/x3/softaculous/index.live.php';
    
        // Domain Name
        $data['softdomain'] = 'root-or-addon-domain.com'; // OPTIONAL - By Default the primary domain will be used
    
        // The directory is relative to your domain and should not exist. e.g. To install at http://mydomain/dir/ just type dir. To install only in http://mydomain/ leave this empty.
        $data['softdirectory'] = ''; // OPTIONAL - By default it will be installed in the /public_html folder
    
        // Admin Username
        $data['admin_username'] = 'admin';
    
        // Admin Pass
        $data['admin_pass'] = 'pass';
    
        // Admin Email
        $data['admin_email'] = 'admin@domain.com';
    
        // Database
        $data['softdb'] = 'wp887'; // make something up that MySQL likes and won't cause a collision
    
        //Database User Name
        $data['dbusername'] = 'wp887'; // ditto
    
        // DB User Pass 
        $data['dbuserpass'] = 'wp887'; // ditto
    
        // Language
        $data['language'] = 'en';
    
        // Site Name
        $data['site_name'] = 'Wordpess wp887';
    
        // Site Description
        $data['site_desc'] = 'WordPress API Test';
    
        // Response
        $res = $new->install(26, $data); // Will install WordPress(26 is its script ID)
    
        // Unserialize
        $res = unserialize($res);
    
        // Done/Error
        if(!empty($res['done'])){
            echo 'Installed';
        }else{
            echo 'Installation Failed<br/>';
            if(!empty($res['error'])){
                print_r($res['error']);
            }
        }
    

    SOURCE: https://gist.github.com/mickaelandrieu/7271939

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配