PHP-MySQLi connection randomly fails with “Cannot assign requested address”

前端 未结 5 1659

Since about 2 weeks I\'m dealing with one of the weirdest problems in LAMP stack. Long story short randomly connection to MySQL server is failing with error message:

<         


        
5条回答
  •  清酒与你
    2021-02-04 14:17

    I had this problem and solved it using persistent connection mode, which can be activated in mysqli by pre-fixing the database hostname with a 'p:'

    $link = mysqli_connect('p:localhost', 'fake_user', 'my_password', 'my_db');
    

    From: http://php.net/manual/en/mysqli.persistconns.php :

    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. ...

    To open a persistent connection you must prepend p: to the hostname when connecting.

提交回复
热议问题