1

Is it possible to make persistent or permanent connection to Mysql in PHP? I mean when I make few separate requests to PHP they all will use the same connection to Mysql. I use msqli driver not sure if it is possible.

Čamo
  • 3,863
  • 13
  • 62
  • 114
  • 1
    Why would you want such a thing? – Dharman Feb 08 '23 at 12:07
  • 1
    You could possibly do it. See: https://www.php.net/manual/en/mysqli.persistconns.php But you must have a **VERY GOOD REASON** to do so. Do you have one? – KIKO Software Feb 08 '23 at 12:10
  • @KIKOSoftware pconnect allows for a php process to reuse once opened opened connection for the *consequent* requests. But by no means it allows for a few separate processes to use the same connection [in parallel] which is likely asked in this question (and which is a really bad idea) – Your Common Sense Feb 08 '23 at 12:52
  • Only if it's a single php process thet serves all requests by itself (such as a websocket deamon), then obviously this process can use a single database connection for all requests but still it would be a bad idea, and it should rather create its own pool of opened connections. – Your Common Sense Feb 08 '23 at 12:56
  • As I found out mysqli driver has by default mysqli.allow_persistent = On. Its in php.ini. But you try to tell me that every request to php app will have its own commention. Am I right? At this moment out application face a huge amouth of requests per second (thousands) and we need to optimize it. Searching possible ways. If somebody have any idea tell me. Thanks. – Čamo Feb 09 '23 at 09:07
  • I am not sure, cause persistent should mean ONE open connection per user:password. So why PHP do not allowe it? – Čamo Feb 09 '23 at 09:08
  • @KIKOSoftware The idea behind persistent connections is that a connection between a client process and a database can be reused by a client process, rather than being created and destroyed multiple times. This reduces the overhead of creating fresh connections every time one is required, as unused connections are cached and ready to be reused. – Čamo Feb 09 '23 at 09:25
  • I think you have all the information you asked for. Did you try it? What happened? – KIKO Software Feb 09 '23 at 09:36
  • 1
    Yes but it's not worth it. You should look for optimisation opportunities elsewhere. Opening connection is a very fast process. You will have hundreds of other issues if you start using persistent connections. – Dharman Feb 09 '23 at 09:37

0 Answers0