Recommendation (free):swoole framework
For the traditional nginx FPM mode PHP program, every time the worker requests FPM, it will connect to mysql once, and then the request will end. Disconnect. For applications with small concurrency, this will not be a problem, but for applications with high concurrency, the database will become a bottleneck if the connection is frequently established and the connection is destroyed. I believe many people have also encountered to many connections. Mysql reports an error.
The connection pool adopts the long connection mode, which will always maintain the connection with MySQL, and will be put back into the connection pool after use, thus It saves the consumption of establishing and disconnecting connections, greatly reduces the consumption of system IO, and improves the concurrency performance of the program to a certain extent. If the connection pool is free, a connection is allocated from the connection pool, otherwise, the request will be added to the waiting queue.
We use swoole to implement mysql connection pool
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); } /** * 获取连接池长度 * @return mixed */ public function getPoolSize(){ return $this->pool->length(); }}
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; }}
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("Test
"); }); $server->handle('/test', function ($request, $response) { $response->end("Test
"); }); $server->handle('/stop', function ($request, $response) use ($server) { $response->end("Stop
"); $server->shutdown(); }); $server->start();});
Related free learning recommendations:mysql video tutorial
The above is the detailed content of Detailed explanation of mysql connection pool implementation based on swoole. For more information, please follow other related articles on the PHP Chinese website!