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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)