最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

PHP Mysql数据库 长链接 短链接 (连接池 ?)

LINX-SQL ajiang-tuzi 6892浏览

起因 :

一个技术大佬 问:
php mysql数据库连接池的概念知不知道
连接池是用到mysql的pconnect吗?
我:一脸懵逼
这玩意 PHP 界还真没听说过
我用过 链接共用 和长链接
这是我认为的概念 但php 貌似实现有点难度,超大并发下的mysql链接 过多导致资源浪费问题 ,实例化特定的数量的链接 (连接池)
让并发的进程使用链接池中链接进行交互 ,不在一个进程一个链接 减少链接数 但会由于(链接池中链接用完) 导致一部分进程等待 使系统同时处理的数据量减少
不确定是不是 于是谷歌之

过程

pconnect 是什么
mysql_pconnect – 打开与MySQL服务器的持久连接

mysql_pconnect 的介绍 php.net

警告
PHP 5.5.0中不推荐使用此扩展,它在PHP 7.0.0中被删除。相反,应该使用MySQLi或PDO_MySQL扩展。另请参见MySQL:选择API指南和 相关常见问题以获取更多信息。此功能的替代方法包括:
mysqli_connect()与 p:主机前缀
PDO :: __结构()与PDO::ATTR_PERSISTENT作为驱动程序选项

stackoverflow 上面的回答 Connection pooling in PHP
There is no connection pooling in php.

mysql_pconnect and connection pooling are two different things. There are many problems connected with mysql_pconnect and first you should read the manual and carefully use it, but this is not connection pooling.
Connection pooling is a technique where the application server manages the connections. When the application needs a connection it asks the application server for it and the application server returns one of the pooled connections if there is one free.
We can do connection scaling in php for that please go through following link:oracle white php part
So no connection pooling in php.
As Julio said apache releases all resources when the request ends for the current reques. You can use mysql_pconnect but you are limited with that function and you must be very careful. Other choice is to use singleton pattern, but none of this is pooling.

MySql 与PHP 官网的 表态

Mysql Mysql PHP Api 10.4.2 Connection pool (Mysql)

Copyright 1997-2014 the PHP Documentation Group.
The plugins connection pool is created when PHP initializes its modules (MINIT) and free’d when PHP shuts down the modules (MSHUTDOWN). This is the same as for persistent MySQL connections.
Depending on the deployment model, the pool is used for the duration of one or multiple web requests. Network connections are bound to the lifespan of an operating system level process. If the PHP process serves multiple web requests as it is the case for Fast-CGI or threaded web server deployments, then the pooled connections can be reused over multiple connections. Because multiplexing means sharing connections, it can even happen with a threaded deployment that two threads or two distinct web requests are linked to one pooled network connections.
A pooled connection is explicitly closed once the last reference to it is released. An implicit close happens when PHP shuts down its modules.

版权所有1997-2014 PHP文档组。
当PHP初始化其modules(MINIT)时,插件连接池是创建的,当PHP关闭modules()时,它们将被释放MSHUTDOWN。这与持久MySQL连接相同。
根据部署模式,池将用于一个或多个Web请求的持续时间。网络连接必须与操作系统级别进程的使用寿命相关联。如果PHP进程服务多个Web请求,就像Fast-CGI或线程Web服务器部署一样,则可以通过多个连接重用池连接。由于多路复用意味着共享连接,所以甚至可以通过线程部署来实现两个线程或两个不同的Web请求链接到一个池网络连接。
一旦释放了最后一次引用,就会显式关闭一个池化的连接。当PHP关闭其模块时,会发生隐式关闭。

来看看 PHP 它表示不服 虽然不自带 但我小弟 主从复制和负载均衡 有办法

里面提到连接池的是这个工具Mysqlnd 主从复制和负载均衡插件 (Mysqlnd replication and load balancing plugin ¶)
中的连接池与切换 ¶下面有这么句话

插件控制 PHP MySQL 链接,完成同步和负载均衡,他并不会改变现有的 PHP MySQL 扩展的方法使用 (mysqli, mysql, 和 PDO_MYSQL) 现有的应用并不需要 更新他们代码,或者使用新的 API,但是若操作行为改变,还是需要一些修改的。
插件结果下面这些扩展对于 MySQL 连接的控制, mysqli, mysql, 和 PDO_MYSQL。 并且为 mysqli, mysql, 和 PDO_MYSQL 提供一个本地的连接池 完成 MySQL 主从同步的 master 和 slave 通讯控制。插件会代理所有的 到达 master 和 slave 的查询请求。 在某一时刻一个连接到 master 的链接,会在稍后变更为 slave 连接,或者 依然保持 master 连接。在执行非事务处理中,控制和替换 PHP MySQL 的网络链接

结论 自带没有 可以通过插件实现 仅在超大型应用中有效

这个是来自MySQL的连接池、异步、断线重连(wiki swoole)
连接池是可以有效降低MySQL-Server负载的。原理是 连接池使用一个共享资源的模式,如并发100个请求,实际上并不是每个请求的所有时间都在执行SQL查询。这样100个请求,共享20个MySQL连接就可以满足需求了。当一个请求操作完数据库后,开始进入模板渲染等流程,这时就会释放数据库连接给其他的请求使用。
连接池仅在超大型应用中才有价值。普通的应用采用MySQL长连接方案,每个php-fpm创建一个MySQL连接,每台机器开启100个php-fpm进程。如果有10台机器,每台机器并发的请求为100。实际上只需要创建1000个MySQL连接就能满足需求,数据库的压力并不大。即使有100台机器,硬件配置好的存储服务器依然可以承受。
达到数百或者数千台应用服务器时,MySQL服务器就需要维持十万级的连接。这时数据库的压力就会非常大了。连接池技术就可以派上用场了,可以大大降低数据库连接数。

参考与引用
MySQL的连接池、异步、断线重连(wiki swoole)
Mysqlnd 主从复制和负载均衡插件 (Mysqlnd replication and load balancing plugin ¶)
Mysql PHP Api 10.4.2 Connection pool (Mysql)
Connection pooling and switching (连接池和切换¶)
持久数据库连接(Persistent Database Connections ¶)
Scaling a PHP MySQL Web Application, Part 1 (oracle 扩展PHP MySQL Web应用程序)
数据库持久连接 ¶

转载请注明:(●--●) Hello.My Weicot » PHP Mysql数据库 长链接 短链接 (连接池 ?)

蜀ICP备15020253号-1