日期:2014-05-16  浏览次数:20957 次

用php安装mysql服务
function __MysqlStart(){
    $cmd	= "net start mysql";
    exec($cmd, $o, $r);
    $o	= join("\n", $o);
    if($this->debug){
        $this->log(__METHOD__, __LINE__, "$cmd ==> \n".$o);
    }
    if( empty($r)  ){
        //preg_match("/服务已经启动成功/", $o)
        return true;
    }else{
        //throw new Exception($r);
        return false;
    }
}
    
function __MysqlStop(){
    $cmd	= "net stop mysql";
    exec($cmd, $o, $r);
    $o	= join("\n", $o);
    if($this->debug){
        $this->log(__METHOD__, __LINE__, "$cmd ==> \n".$o);
    }
    if( empty($r)  ){
        //preg_match("/服务已经启动成功/", $o)
        return true;
    }else{
        return false;
        //throw new Exception($r);
    }

}
    
function __MysqlRemove(){
    $stat	= $this->__query('mysql');
    if($this->debug){
        $this->log(__METHOD__, __LINE__, "mysql status = $stat");
    }
    if( $stat === 'start' ){
        $this->__MysqlStop();
    }
    
    $cmd	= MysqlDir.'\bin\mysqld-nt.exe --remove mysql';
    exec($cmd, $r);
    $r	= join("\n",$r);
    if($this->debug){
        $this->log(__METHOD__, __LINE__, "$cmd ==> \n".$r);
    }
    if( preg_match("/successfully/", $r) ){
        unset($this->ports['mysql']);
        return true;
    }else{
        return false;
    }
}

function __MysqlInstall(){

    $this->__MysqlRemove();
    
    $port	= $this->__getFreePort(3306, 'mysql');
    $this->ports['mysql']	= $port;

    
    $this->__etc( 'mysql.ini', MysqlDir.'\my.ini' , array(
            '%port%' 	=> $port,
            '%root%' 	=> uRootDir
        ));

    $cmd	= MysqlDir.'\bin\mysqld-nt.exe  --install mysql  --defaults-file="'.MysqlDir.'\my.ini"';
    exec($cmd, $r);
    $r	= join("\n",$r);
    if($this->debug){
        $this->log(__METHOD__, __LINE__, "$cmd ==> \n".$r);
    }
    if( !preg_match("/successfully/", $r) ){
        throw new Exception("安装mysql失败");
    }
    
}