MySQL Persistent Connections In PHP

Costas

Administrator
Staff member
As of PHP v7.1.9, the mysqli and pdo_mysql extension supports persistent database connections, which are a special kind of pooled connections. By default, every database connection opened by a script is either explicitly closed by the user during runtime or released automatically at the end of the script. A persistent connection is not. Instead it does not close when the execution of the script ends. When a persistent connection is requested, PHP checks if there's already an identical persistent connection (that remained open from earlier) - and if it exists, it uses it. If it does not exist, it creates a new connection. An 'identical' connection is a connection that was opened to the same host, with the same username and the same password (where applicable). Reuse saves connection overhead.

JavaScript:
;Allow or prevent persistent links
mysqli.allow_persistent = On
 
;The maximum number of MySQL connections per process. -1 means no limit.
mysqli.max_links = -1
 
;Maximum of persistent connections that can be made. -1 means no limit.
mysqli.max_persistent = -1

--

<?php
 
//mysqli persistent connections
$instance_mysqli = new mysqli('p:fs_host', 'fs_user', 'fs_password', 'fs_db');
 
//pdo_mysql persistent connections
$instance_pdo = new PDO('mysql:host=fs_host;dbname=fs_db', $fs_user, $fs_password, [
    PDO::ATTR_PERSISTENT => true
]);


src - https://www.valinv.com/dev/mysql-mysql-persistent-connections-in-php
 
Top