swoole编译安装/数据库连接池/异步mysql客户端
Posted 罗夏
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swoole编译安装/数据库连接池/异步mysql客户端相关的知识,希望对你有一定的参考价值。
一.编译安装php5.6
0.安装必要软件
http://www.cnblogs.com/itfenqing/p/6055138.html
1.下载php5.6.30
http://php.net/downloads.php
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.0
2.解压
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.ini
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查询结果
以上是关于swoole编译安装/数据库连接池/异步mysql客户端的主要内容,如果未能解决你的问题,请参考以下文章