PHP explore MySQL long connection, connection pool

The way php connects to mysql is mostly mysql extension, mysqli extension, and pdo_mysql extension, which are officially provided. The operating mechanism of php is that all resources in the php process will be released after the page is executed. If there are multiple concurrent accesses to the local test page http://127.0.0.1/1.php, depending on the difference between php and the web server, it will Open the corresponding thread or process to handle the request, and the result will be released after the request. That is, php cannot transfer some data from page to page from the language level, but mysql_pconnect and ATTR in pdo, setting array (PDO::ATTR_PERSISTENT => true) as follows can achieve long connection.

$conn = new PDO($dsn, DB_USER, DB_PASSWORD,
    array(PDO::ATTR_PERSISTENT => true)
);

I think the role of long connections is that under high load conditions, by reusing long connections, the time to establish a database connection for each page is reduced, and the time to establish a mysql connection is on my machine.

  • In the case of database connnections <10, the connection time of mysql pdo is 0.003ms, and the connection time of mysqli is 0.14ms

  • When the database connection is nearly full, the connection time for mysql pdo is 0.13ms, and the connection time for mysqli is 0.13ms.

The above samples are all estimated time, too small to estimate. In fact, the time to establish a connection is not long, so why do you need such things as mysql long connection and connection pool? That is under high load. For example, the mysql concurrency that a single server can accept is around 200, and the single concurrency of a web server is around 700. Then when a large number of 500 concurrent connections are overwhelmed, the web server has not reached full load and mysql has arrived early. Full load will cause all pages to fail to respond, or pages that have established database connections perform very slowly.

MySQL long connection in php Because there are many operating modes of PHP, there are also many long connection implementations. Web server support is needed to achieve long connections, because PHP does not have the concept of process pool and connection pool. In most cases, the PHP application itself is not an application server (the rising star swoole, is an excellent PHP application server, but it is Done at level c). Therefore, the long connection of php is actually a webserver equipped with an mpm module such as apache. Under linux, apache will maintain a process pool. After the apache mpm function is turned on, apache will maintain a process pool by default. The connection after mysql long connection, and It is not closed as a socet connection, but as a non-released thing, put into the process pool/thread pool. When a connection is needed, apache takes out mysql socket connnection from the process pool/thread pool it maintains, and then the connection can be reused.

Let's test it here. First of all, the native environment is archlinux, and the mysql httpd php used in the following are source codes compiled by ourselves, all in the /home/dengpan/opt directory. httpd's mpm model here is worker, httpd's mpm (apache used for parallel functions, commonly known as multi-processing module) actually has three types: perfork, worker, and event. The advantage of mpm is that apache has some spare spare or idle child processes (server thread pool) at any time, waiting for new requests at any time, so that the client does not need to wait for the generation of child processes before requesting services.

What mpm to use, you need to specify separately to compile into apache, such as compiling work mpm into apache, for example, my most simplified httpd compilation parameter is

./configure \
--with-apr=/home/dengpan/opt/apr-1.5.2 \
--with-apr-util=/home/dengpan/opt/apr-util-1.5.4 \
--prefix=/home/dengpan/opt/httpd-2.4.16 \
--with-mpm=worker

View the modules loaded by httpd,

Insert picture description here

Seeing that worker.c has been compiled,

The configuration parameters of mpm are

<IfModule mpm_worker_module>
	StartServers			 15
	MinSpareThreads		 75
	MaxSpareThreads		250
	ThreadsPerChild		 10
	MaxRequestWorkers	  400
	MaxConnectionsPerChild   0
</IfModule>

Start apache and use pstree to see |-httpd—15*[httpd—11*[{httpd}]], indicating that 15 server processes have started, and each server has 10 child threads. The minimum number of idle threads to be maintained for the entire mpm is 75, and the maximum number of idle threads is 250. The largest fully loaded worker thread is 400. Next, prepare a shell script to output the current number of mysql active connections every 1 second. There are two ways to check the number of mysql current connections.

  • Enter the mysql shell, execute SHOW STATUS WHERE variable_name='Threads_connected'; but this method requires the mysql shell to enter. When there are a lot of connections, the mysql shell cannot enter and cannot be queried.

  • Shell direct query, find /proc/ pidof mysqld/fd/ -follow -type s | wc -l, root permission is required. The advantage is that even if mysql cannot enter the shell because of too many connections, it can still be connected.

Method 2 is used here, because the mysql back to the machine is fully loaded later. Write a shell as follows:

#!/bin/bash
while(true)
do
    find /proc/`pidof mysqld`/fd/ -follow -type s | wc -l
	sleep 1
done

After executing the shell continuously output the current number of connections, the test can be obtained

  1. Execute php under cli, long connection is invalid, the connection will be released as soon as the script exits under cli

  2. If apche+mod_php does not open the mpm module, regardless of the mysql mysql_pconnect or pdo_mysql long connection, the mysql connection will be released after the page is accessed.

  3. If apche+mod_php turns on the mpm module (worker mode), no matter the mysql mysql_pconnect, pdo_mysql long connection, the page is accessed, the mysql connection +1, until the maximum number of mysql connections is reached, it does not increase, but the access page can still reuse the connection query to the corresponding data.

  4. MySQL long connection under nginx+php-fpm basically has no effect.

The reason why apache can reuse mysql connection means that apache must have implemented some functions and modules for mysql itself, otherwise it is impossible to save an unknown type of socket pointer. View with ldd,

➜  mysql_persist  ldd /home/dengpan/opt/httpd-2.4.16/bin/httpd
        linux-vdso.so.1 (0x00007ffffcbde000)
        libpcre.so.1 => /usr/lib/libpcre.so.1 (0x00007f8e8d17c000)
        libaprutil-1.so.0 => /home/dengpan/opt/apr-util-1.5.4/lib/libaprutil-1.so.0 (0x00007f8e8cf57000)
        libexpat.so.1 => /usr/lib/libexpat.so.1 (0x00007f8e8cd2d000)
        libapr-1.so.0 => /home/dengpan/opt/apr-1.5.2/lib/libapr-1.so.0 (0x00007f8e8cafb000)
        libuuid.so.1 => /usr/lib/libuuid.so.1 (0x00007f8e8c8f6000)
        librt.so.1 => /usr/lib/librt.so.1 (0x00007f8e8c6ee000)
        libcrypt.so.1 => /usr/lib/libcrypt.so.1 (0x00007f8e8c4b6000)
        libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00007f8e8c299000)
        libdl.so.2 => /usr/lib/libdl.so.2 (0x00007f8e8c095000)
        libc.so.6 => /usr/lib/libc.so.6 (0x00007f8e8bcf3000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f8e8d3ec000)

You can guess /home/dengpan/opt/apr-util-1.5.4/lib/libaprutil-1.so.0 and /home/dengpan/opt/apr-1.5.2/lib/libapr-1.so.0 The code segment related to mysql should be implemented. Since I compiled it locally, it is easy to find the function entry. The file /home/dengpan/github/apache-httpd/apr-util-1.5.4/dbd/apr_dbd_mysql.c is actually the mod_dbd of apache for common databases. Do long connection support. The relationship between nginx and php-fpm is not like that between php and apache, so nginx+php-fpm cannot achieve the corresponding long connection. Probably php-fpm does not do mysql process and thread pool.

Finally, I give my advice. Generally, small PHP applications have no performance problems. PHP itself connects to mysql very quickly, and many of them are in excess performance. As Apache is gradually replaced by nginx, PHP’s mysql long connection will only increase. Chicken ribs. In the case of a stand-alone machine, if you are afraid that mysql creates connections under pressure, it is best to use singleton mode for mysql creation, so that a page will only create one mysql connection instance. Such as the following singleton example code, and this is more suitable for writing in the framework to achieve singleton.

<?php
/**
 * Created by PhpStorm.
 * User: dengpan
 * Date: 15-7-24
 * Time: 下午2:56
 */
include "./db.php";
class DB {
    
    
	private static $_instance;
	private $db;
	private function __construct()
	{
    
    
		$this->db =  new mysqli(DB_HOST, DB_USER, DB_PASSWORD, 'my', 3307);
	}
	public static function getInstance()
	{
    
    
		if (!(self::$_instance instanceof DB)) {
    
    
			self::$_instance = new self();
		}
		return self::$_instance;
	}
	private function __clone()
	{
    
    
	}
	public function getConn()
	{
    
    
		return $this->db;
	}
}
$conn = DB::getInstance()->getConn();

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/108940141