In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about the method and function of workerman writing mysql connection pool, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
First of all, you need to understand why connection pooling is used and what problems connection pooling can solve for you.
The main roles of connection pooling are:
1. Reduce the overhead of establishing a three-way handshake and closing a TCP connection with the data server, thereby 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, solve the database too many connections problem caused by too many application servers
If it is to solve problem 1
Database connection pooling is not the most efficient method in workerman, but a way to ask for trouble. Because PHP is single-process and single-threaded, using PHP to implement database connection pool must be done by a separate process, then it will involve inter-process communication, which makes the original process of direct communication with mysql into communication with connection pool and then to mysql, increasing the load on the application side.
The most efficient way to solve problem 1 is to establish a database singleton for each business process (such as the DB class provided by workerman) to implement database persistent connections, so that all requests of each process use its own database persistent connection, and the whole process has only one TCP handshake and disconnection and waving overhead, and the application communicates directly with mysql, without the middle layer of inter-process IPC communication like connection pool. Performance is the highest, not one of them.
If it's for question 2,
First of all, take a look at how many application servers you have, and how many concurrent connections each server has to mysql. If you have only 10 application servers, each server has 50 processes, and each process has 1 database connection, then there are only 10-50-500 concurrent connections (not active connections) to the mysql server, which is a piece of cake for mysql. In order to solve problem 2, there is no need to use connection pooling at all.
If you have 1000 application servers, connection pooling is necessary, but this connection pool cannot be a connection pool running on a local application server, because 1000 application servers have 1000 connection pools. even if there are only 10 connections per connection pool, the number of connections to the database will easily fill up. So don't expect to solve this problem by opening connection pooling implemented by several task processes on the current server.
In a cluster of 1000 application servers, it is also unreliable to implement connection pooling with several processes on each server. The real solution to problem 2 is to set up a separate database connection pool server or cluster to manage all database links globally.
To sum up,
If you implement php's mysql connection pooling for problem 1 alone, then the database singleton is simpler and more efficient than the so-called connection pooling.
If it is to achieve problem 2, then the business must also have a certain scale. If you really want to use workerman to do a separate connection pool cluster, here is a simple way to set up some task processes, each process creates a database connection, and the task process receives the sql request and 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, you'd better add a lvs in front of it:
/ / task worker, using the Text protocol $task_worker = new Worker ('Text://0.0.0.0:1234'); $task_worker- > count = 64 leading taskworkers-> name =' MysqlTask';$task_worker- > onMessage = function ($connection, $sql) {/ / execute sql.... Get the result, omit here. $sql_result = your_mysql_query ($sql); / / send the result $connection- > send (json_encode ($sql_result));}
Call in workerman:
Use\ Workerman\ Connection\ AsyncTcpConnection;// establishes an asynchronous link with the remote connection pooling service. Ip is the ip of the remote connection pooling service. If it is a cluster, it is the ip$sql_connection = new AsyncTcpConnection ('Text://ip:1234') of lvs; / / send sql$sql_connection- > send ("SELECT... FROM. "); / / get the sql result asynchronously $sql_connection- > onMessage = function ($sql_connection, $sql_result) {/ / just print the result var_dump (json_decode ($task_result));}; / / execute the asynchronous link $sql_connection- > connect (). After reading the above, do you have any further understanding of workerman's method and function of writing mysql connection pool? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.