首页 » 网站建设 » php衔接池mysql技巧_PHP进阶教程实现一个简单的MySQL连接池

php衔接池mysql技巧_PHP进阶教程实现一个简单的MySQL连接池

访客 2024-10-30 0

扫一扫用手机浏览

文章目录 [+]

从图可以看到想要实行一条SQL语句每次都要走 图:3.5-1都过程,至少要7步才可以成功。
MySQL会有连接数上限的限定,而且每次都实行那么多都步骤,频繁把韶光摧残浪费蹂躏在了网络IO上。

没有连接池的做法类似我们买菜做饭,比如我们要做十个菜,每做一个菜就跑一趟菜市场,挑菜、讨价还价、回家、洗菜、下锅、起锅、洗锅;这样是不是很摧残浪费蹂躏韶光?那我们想要做十个菜,提前把这十个菜的材料都买回来,都洗好备用,然后每次炒都时候直接下锅炒就好了。
连接池便是提前买好菜,洗好菜(创建连接、验证账号密码),在要炒菜的时候直接下锅(实行SQL)炒。

php衔接池mysql技巧_PHP进阶教程实现一个简单的MySQL连接池

利用连接池之后,只有在连接池初始化的时候就进行连接然后存到一个容器里面。
每次要实行SQL语句的时候先来这个池获取连接工具,然后再发送SQL语句,当SQL语句实行完之后,再把这个连接归还给连接池。

php衔接池mysql技巧_PHP进阶教程实现一个简单的MySQL连接池
(图片来自网络侵删)

利用连接池每次实行SQL语句只须要实行 图:3.5-1 的第6步就行了,复用了MySQL连接,在高并发情形下,节省了每次连接带来的其他开销。

连接池有什么?最小连接数最大连接数当前连接数连接池工具获取连接池超时时间康健度检讨实战:Swoole实现连接池MysqlPool.php

/ 1、设置配置信息 2、创建连接工具 3、获取连接工具 4、获取连接工具,空闲连接不足创建到最大连接数 5、实行sql语句 6、归还连接 /use Swoole\Coroutine\Channel;class MysqlPool{ // 最小连接数 private $min; // 最大连接数 private $max; // 当前连接数 private $count = 0; // 获取超时时间 private $timeOut = 0.2; // 连接池工具容器 private $connections; // 配置信息 private $config = []; // 连接池工具 private static $instance; public function __construct(array $config){ $this->config = $config; $this->min = $this->config['min'] ?? 2; $this->max = $this->config['max'] ?? 4; $this->timeOut = $this->config['time_out'] ?? 0.2; $this->connections = new Channel($this->max); } / 获取连接池工具 @param null $config @return MysqlPool / public static function getInstance($config = null){ if (empty(self::$instance)) { if (empty($config)) { throw new RuntimeException("mysql config empty"); } self::$instance = new static($config); } return self::$instance; } / 初始化连接池 @throws Exception / public function init(){ for ($i = 0; $i < $this->min; $i++) { $this->count++; $mysql = $this->createDb(); $this->connections->push($mysql); } } / 创建数据库连接工具 @return PDO @throws Exception / private function createDb(){ $dsn = "mysql:dbname={$this->config['database']};host={$this->config['db_host']}"; try { $mysql = new PDO($dsn, $this->config['db_user'], $this->config['db_passwd']); return $mysql; } catch (\PDOException $e) { throw new \Exception($e->getMessage()); } } / 获取数据库连接 @return mixed|null|PDO @throws Exception / public function getConnection(){ $mysql = null; // 判断是否为空,如果池空了,判断当前连接数是否下于最大连接数 // 如果小于最大连接数创建新连接数 if ($this->connections->isEmpty()) { if ($this->count < $this->max) { $this->count++; $mysql = $this->createDb(); } else { $mysql = $this->connections->pop($this->timeOut); } } else { $mysql = $this->connections->pop($this->timeOut); } // 获取不到数据库连接抛出非常 if (!$mysql) { throw new \Exception('没有连接了'); } // 当协程结束之后归还连接池 defer(function () use ($mysql) { $this->connections->push($mysql); }); return $mysql; } / 调试打印连接池的容量,非紧张代码 @param $str / public function printLenth($str){ echo $str . $this->connections->length() . "\n"; }}server.php

include './MysqlPool.php';//创建http server$http = new Swoole\Http\Server("0.0.0.0", 9501);$http->set(["worker_num" => 2]);$http->on('WorkerStart', function ($serv, $worker_id) { $config = [ 'min' => 3, 'max' => 5, 'time_out' => 1, 'db_host' => '127.0.0.1', 'db_user' => 'root', 'db_passwd' => 'sunny123', 'database' => 'lv' ]; MysqlPool::getInstance($config)->init();});$http->on('request', function ($request, $response) { try { MysqlPool::getInstance()->printLenth(Swoole\Coroutine::getCid() . '获取前:'); $mysql = MysqlPool::getInstance()->getConnection(); MysqlPool::getInstance()->printLenth(Swoole\Coroutine::getCid() . '归还前:'); $result = $mysql->query("select from sunny_member"); $row = $result->fetch(MYSQLI_ASSOC); MysqlPool::getInstance()->printLenth(Swoole\Coroutine::getCid() . '归还后:'); $response->end($row['content']); } catch (\Exception $e) { $response->end($e->getMessage()); }});$http->start();

本案例实现:

最小连接数最大连接数当前连接数连接池工具获取连接池超时时间

思考:怎么实现康健度检讨?

标签:

相关文章

php为无色透明技巧_水货钻石其实也还行

从各种钻石中,可以看到大大小小的“包裹体” 图片来源:参考文献包裹体的种类多样。比钻石形成更早的包裹体,叫“原生包裹体”;与钻石同...

网站建设 2024-12-19 阅读0 评论0

phpstudy发送gbk技巧_php的文件上传

这里首先声明一下这一章的内容比较多,比较难,你要抱着和自己去世磕的态度。细微之处不放过,多敲多练是王道。 学习就像爬山,得一步一步...

网站建设 2024-12-19 阅读0 评论0