Swoole compiling installation/database connection pool/asynchronous MySQL Client

Source: Internet
Author: User

One. Compile and install php5.6

0. Install the necessary software

Http://www.cnblogs.com/itfenqing/p/6055138.html

1. Download php5.6.30

http://php.net/downloads.php

2. Unzip

3. Compile-time specified as MYSQLNT, enable 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--with-freetype-dir-- Enable-mbstring--with-curl--with-libxml-dir--with-mysql  

Two. Compiling swoole

1. Download swoole1.9.0

2. Unzip

3. Installation

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

  

4. Copy Php.ini-production to/usr/local/php56/php.ini

5. Join under the/usr/local/php56/php.ini

[Zend opcache]zend_extension =/usr/local/php56/lib/php/extensions/debug-non-zts-20131226/ Opcache.soopcache.enable=1, #启用操作码缓存opcache enable_cli=1; #仅针对CLI环境启用操作码缓存opcache. memory_consumption=128; #共享内存大小 , Unit mbopcache.interned_strings_buffer=8; #存储临时字符串的内存大小, Unit mbopcache.max_accelerated_files=4000; #哈希表中可存储的脚本文件数量上限 [ Swoole]extension =/usr/local/php56/lib/php/extensions/debug-non-zts-20131226/swoole.so

  

Three. Swoole Database connection Pool

1. Service-Side code:

<?php$serv = new Swoole_server ("127.0.0.1", 9508), $serv->set (Array (' worker_num ' = +, ' task_worker_num ') (//mysql)), function my_onreceive ($serv, $FD, $from _id, $data) {//taskwait is the delivery of a task, where the SQL statement is passed directly//then blocked    Wait for SQL to complete $result = $serv->taskwait ($data);        if ($result!== false) {list ($status, $db _res) = Explode (': ', $result, 2); if ($status = = ' OK ') {//database operation succeeded, execute business logic code, this automatically frees up MySQL connection occupied $serv->send ($FD, Var_export (Unseria Lize ($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. Client code

<?php$link=new swoole_client (swoole_sock_tcp,swoole_sock_sync);//tcp mode, Synchronous $link->connect (' 127.0.0.1 ', 9508) ;//Connect $link->send (' SELECT * from ' test2 ');//Execute Query $res= $link->recv (); (! $res) {    echo ' failed! ';} else{    Print_r ($res);} $link->close ();

  

Three. Asynchronous MySQL Client
<?php$db = new Swoole_mysql; $server = Array (    ' host ' = ' 192.168.2.15 ',    ' port ' = ' 3306 ',    ' user ' =& Gt ' Root ',    ' password ' = ' root ',    ' database ' = ' test ',    ' charset ' = ' utf8 ',//Specify Character Set    ' timeout ' = > $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 ';

  

The above code first output OK, in the output MySQL query results

  

Swoole compiling installation/database connection pool/asynchronous MySQL Client

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.