First of all, you must understand why the connection pool is used and what problems the connection pool can solve for you
The main function of the connection pool
1. Reduce the number of connections with data The server establishes a TCP connection with a three-way handshake and closes the connection with a four-way handshake, thus reducing the load on the client and the mysql server and shortening the request response time
2. Reduce the number of concurrent connections to the database, that is, solving the problem of too many application servers The database connection problem caused by too many connections
If it is to solve problem 1
, the database connection pool in Workerman is not the most efficient method, but is asking for trouble. Since PHP is a single process and single thread, using PHP to implement a database connection pool definitely requires a separate process, which will involve inter-process communication, making the original process of direct communication with mysql become a process with the connection pool and then again MySQL communication increases the load on the application side.
The most efficient way to solve problem 1 is to establish a database single instance (such as the DB class provided by Workerman) for each business process and implement a long connection to the database, so that all requests of each process use its own For a database long connection, there is only one TCP handshake and disconnection wave overhead in the entire process life cycle, and the application communicates directly with mysql. There is no intermediate layer of inter-process IPC communication like a connection pool, and the performance is the highest among them.
If it is for question 2
First check how many application servers you have and how many concurrent connections each server has to mysql. If you only have 10 application servers, each server has 50 processes, and each process has 1 database connection, then there will only be a total of 10*50=500 concurrent connections to the mysql server (not active connections), and 500 concurrent connections for mysql It's a piece of cake. In order to solve problem 2, there is no need to use a connection pool.
If you have 1,000 application servers, then a connection pool is necessary, but this connection pool cannot be a connection pool running on the local application server, because there are 1,000 connection pools for 1,000 application servers. Even if each connection pool only opens 10 connections, the number of connections in the database will be easily filled. So don't expect that opening a connection pool implemented by several task processes on the current server will solve this problem.
In a cluster of 1,000 application servers, setting up several processes on each server to implement connection pooling is also an unreliable method. The real way to solve problem 2 is to establish an independent database connection pool server or cluster to globally manage all database links.
To sum up,
If you are implementing PHP's mysql connection pool solely for question 1, then the database singleton is a simpler and more efficient approach than the so-called connection pool.
If it is to realize question 2, then the business must have a certain scale. If you really want to use Workerman to create a separate connection pool cluster, the following is a simple method, create some task processes, each The process creates a database connection. After the task process receives the SQL request, it sends it to the mysql server. After the mysql server returns, the task process sends the result to the SQL initiator.
The connection pool code is similar to the following. If it is a connection pool cluster composed of multiple servers, it is best to add a lvs in front
// task worker,使用Text协议 $task_worker = new Worker('Text://0.0.0.0:1234'); $task_worker->count = 64; $task_worker->name = 'MysqlTask'; $task_worker->onMessage = function($connection, $sql) { // 执行sql.... 得到结果,这里省略.... $sql_result = your_mysql_query($sql); // 发送结果 $connection->send(json_encode($sql_result)); };
Call in workerman
use \Workerman\Connection\AsyncTcpConnection; // 与远程连接池服务建立异步链接,ip为远程连接池服务的ip,如果是集群就是lvs的ip $sql_connection = new AsyncTcpConnection('Text://ip:1234'); // 发送sql $sql_connection->send("SELECT ... FROM ....."); // 异步获得sql结果 $sql_connection->onMessage = function($sql_connection, $sql_result) { // 这里只是打印结果 var_dump(json_decode($task_result)); }; // 执行异步链接 $sql_connection->connect();
More workerman knowledge Please pay attention to the workerman tutorial column.
The above is the detailed content of workerman writes mysql connection pool. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Linux new version
SublimeText3 Linux latest version
