swoole編譯安裝/數據庫鏈接池/異步mysql客戶端

一.編譯安裝php5.6

0.安裝必要軟件php

http://www.cnblogs.com/itfenqing/p/6055138.htmlhtml

1.下載php5.6.30mysql

http://php.net/downloads.phpsql

 

2.解壓數據庫

3.編譯時指定爲mysqlnt,啓用opcache緩存

./configure --prefix=/usr/local/php56  --enable-fpm --enable-mysqlnd --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --enable-debug --with-gd --with-jpeg-dir --with-png-dir --enable-mbstring --with-curl --with-libxml-dir --with-mysql --enable-sockets --enable-bcmath --enable-xml --with-bz2 --enable-zip --with-mcrypt --enable-opcache --with-openssl --with-freetype-dir=/usr/include/freetype2/freetype

  

二.編譯swoole

1.下載swoole1.9.0swoole

2.解壓curl

3.安裝異步

/usr/local/php56/bin/phpize
./configure --with-php-config=/usr/local/php56/bin/php-config
make 
make install

  

4.拷貝php.ini-production到/usr/local/php56/php.inisocket

5.在/usr/local/php56/php.ini下加入

[Zend Opcache]
zend_extension = /usr/local/php56/lib/php/extensions/debug-non-zts-20131226/opcache.so
opcache.enable=1 ;#啓用操做碼緩存
opcache.enable_cli=1 ;#僅針對CLI環境啓用操做碼緩存
opcache.memory_consumption=128 ;#共享內存大小,單位MB
opcache.interned_strings_buffer=8 ;#存儲臨時字符串的內存大小,單位MB
opcache.max_accelerated_files=4000 ;#哈希表中可存儲的腳本文件數量上限

[Swoole]
extension = /usr/local/php56/lib/php/extensions/debug-non-zts-20131226/swoole.so

  

三.swoole數據庫鏈接池

1.服務端代碼:

<?php
$serv = new swoole_server("127.0.0.1", 9508);
$serv->set(array(
    'worker_num' => 100,
    'task_worker_num' => 10, //MySQL鏈接的數量
));

function my_onReceive($serv, $fd, $from_id, $data)
{
    //taskwait就是投遞一條任務,這裏直接傳遞SQL語句了
    //而後阻塞等待SQL完成
    $result = $serv->taskwait($data);
    if ($result !== false) {
        list($status, $db_res) = explode(':', $result, 2);
        if ($status == 'OK') {
            //數據庫操做成功了,執行業務邏輯代碼,這裏就自動釋放掉MySQL鏈接的佔用
            $serv->send($fd, var_export(unserialize($db_res), true) . "\n");
        } else {
            $serv->send($fd, $db_res);
        }
        return;
    } else {
        $serv->send($fd, "Error. Task timeout\n");
    }
}

function my_onTask($serv, $task_id, $from_id, $sql)
{
    static $link = null;
    if ($link == null) {
        $link = new mysqli("192.168.2.15", "root", "root", "test");
        if (!$link) {
            $link = null;
            $serv->finish("ER:" . mysqli_error($link));
            return;
        }
    }
    $result = $link->query($sql);
    if (!$result) {
        $serv->finish("ER:" . mysqli_error($link));
        return;
    }
    $data = $result->fetch_all(MYSQLI_ASSOC);
    $serv->finish("OK:" . serialize($data));
}

function my_onFinish($serv, $data)
{
    echo "AsyncTask Finish:Connect.PID=" . posix_getpid() . PHP_EOL;
}

$serv->on('Receive', 'my_onReceive');
$serv->on('Task', 'my_onTask');
$serv->on('Finish', 'my_onFinish');
$serv->start();

  

2.客戶端代碼

<?php
$link=new swoole_client(SWOOLE_SOCK_TCP,SWOOLE_SOCK_SYNC);//TCP方式、同步
$link->connect('127.0.0.1',9508);//鏈接
$link->send('SELECT * FROM `test2`');//執行查詢
$res=$link->recv();

if(!$res){
    echo 'Failed!';
}
else{
    print_r($res);
}
$link->close();

  

三.異步mysql客戶端

<?php

$db = new swoole_mysql;
$server = array(
    'host' => '192.168.2.15',
    'port' => '3306',
    'user' => 'root',
    'password' => 'root',
    'database' => 'test',
    'charset' => 'utf8', //指定字符集
    'timeout' => 30,
);

$db->connect($server, function ($db, $r) {
    if ($r === false) {
        var_dump($db->connect_errno, $db->connect_error);
        die;
    }
    $sql = 'select *,sleep(5) from test2';
    $db->query($sql, function (swoole_mysql $db, $r) {
        if ($r === false) {
            var_dump($db->error, $db->errno);
        } elseif ($r === true) {
            var_dump($db->affected_rows, $db->insert_id);
        }
        var_dump($r);
        $db->close();
    });
});

echo 'ok';

  

以上代碼先輸出ok,在輸出mysql查詢結果

相關文章
相關標籤/搜索