這篇文章給大家分享的是有關(guān)swoole的MySQL連接池怎么弄的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)專(zhuān)注于西充企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站開(kāi)發(fā)。西充網(wǎng)站建設(shè)公司,為西充等地區(qū)提供建站服務(wù)。全流程定制設(shè)計(jì),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
傳統(tǒng)的nginx+FPM模式的PHP程序而言,每次請(qǐng)求FPM的worker都會(huì)連接一次mysql,然后請(qǐng)求結(jié)束便會(huì)斷開(kāi)連接。對(duì)于并發(fā)小的應(yīng)用來(lái)說(shuō)這不會(huì)有什么問(wèn)題,但是對(duì)于高并發(fā)的應(yīng)用來(lái)說(shuō),頻繁建立連接Connect和銷(xiāo)毀連接Close,數(shù)據(jù)庫(kù)便會(huì)成為瓶頸,相信不少人也遇到過(guò)to many connection的mysql報(bào)錯(cuò)吧。
連接池采用的是長(zhǎng)連接模式,會(huì)一直保持與MySQL的連接,用完后會(huì)重新放回連接池,從而節(jié)省了建立連接和斷開(kāi)連接的消耗,大大降低了系統(tǒng)IO的消耗,一定程度上提高了程序的并發(fā)性能。如果連接池空閑,就從連接池分配一個(gè)連接,否則,請(qǐng)求將被加入到等待隊(duì)列中。
我們采用swoole實(shí)現(xiàn)mysql連接池
<?php require_once "MysqlDB.php";class MysqlPool{ private static $instance; private $pool; private $config; private $pool_get_timeout; /** * 獲取mysql進(jìn)程池單例 * @param null $config * @return MysqlPool */ public static function getInstance($config = null) { if (empty(self::$instance)) { if (empty($config)) { throw new RuntimeException("mysql config is empty"); } self::$instance = new static($config); } return self::$instance; } public function __construct($config) { if (empty($this->pool)) { $this->config = $config; $this->pool = new \Swoole\Coroutine\Channel($config['pool_size']); for ($i = 0; $i < $config['pool_size']; $i++) { \go(function() use ($config) { $mysql = new MysqlDB(); $res = $mysql->connect($config['mysql']); if ($res === false) { throw new RuntimeException("Failed to connect mysql server"); } else { $this->pool->push($mysql); } }); } } } public function get() { if ($this->pool->length() > 0) { $mysql = $this->pool->pop($this->config['pool_get_timeout']); if (false === $mysql) { throw new RuntimeException("Pop mysql timeout"); } return $mysql; } else { throw new RuntimeException("Pool length <= 0"); } } public function recycle(MysqlDB $mysql){ $this->pool->push($mysql); } /** * 獲取連接池長(zhǎng)度 * @return mixed */ public function getPoolSize(){ return $this->pool->length(); }}
<?phpclass MysqlDB{ private $connection; public function connect($config) { $connection = new \Swoole\Coroutine\MySQL(); $res = $connection->connect($config); if ($res === false) { throw new RuntimeException($connection->connect_error, $connection->errno); } else { $this->connection = $connection; } return $res; } public function query($sql){ $result = $this->connection->query($sql); return $result; }}
<?php require_once "MysqlPool.php";\Co\run(function () { $server = new \Co\Http\Server("0.0.0.0", 9501, false); $pool = MysqlPool::getInstance([ 'pool_size'=>5, 'pool_get_timeout'=>1, 'timeout'=>1, 'charset'=>'utf8', 'strict_type'=>false, 'fetch_mode'=>true, 'mysql'=>[ 'host'=>'127.0.0.1', 'port'=>'3306', 'user'=>'homestead', 'password'=>'secret', 'database'=>'blog', ] ]); $server->handle('/', function ($request, $response) use ($pool){ $mysql = $pool->get(); $res = $mysql->query("select id,phone,username from user limit 1"); var_dump($res); $pool->recycle($mysql); $response->end("<h2>Test</h2>"); }); $server->handle('/test', function ($request, $response) { $response->end("<h2>Test</h2>"); }); $server->handle('/stop', function ($request, $response) use ($server) { $response->end("<h2>Stop</h2>"); $server->shutdown(); }); $server->start();});
感謝各位的閱讀!關(guān)于“swoole的mysql連接池怎么弄”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
文章名稱(chēng):swoole的mysql連接池怎么弄
網(wǎng)站鏈接:http://chinadenli.net/article46/pddpeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、靜態(tài)網(wǎng)站、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站設(shè)計(jì)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)