Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP-MySQLi connection randomly fails with "Cannot assign requested address"

Tags:

php

mysql

debian

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:

Warning:  mysqli::real_connect(): (HY000/2002): Cannot assign requested address in ..

The MySQL is on different "box", hosted at Rackspace Cloud Today we downgraded it's version to

Ver 14.14 Distrib 5.1.42, for debian-linux-gnu (x86_64).

The DB server is pretty busy dealing with Queries per second avg: 5327.957 according to it's status variable.

MySQL is in log-warnings=9 but no warring for connection refused are logged. Both site and gearman workers scripts fail with that error at let's say 1% probability. No server load DO NOT seems to be a factor as we monitor. (CPU load, IO load or MySQL load) The maximum DB connections (max_connections) are setted to 200 but we have never dealed with more than 100 simultaneous connections to the database

It happens with and without the firewall software.

I suspect TCP Networking problem rather than PHP/MySQL configurationn problem.

Can anyone give me clue how to find it?

UPDATE:

The connection code is:

$this->_mysqli = mysqli_init(); 
$this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 120); 
$this->_mysqli->real_connect($dbHost,$dbUserName, $dbPassword, $dbName); 

if (!is_null($this->_mysqli->connect_error)) {
    $ping = $this->_mysqli->ping(); 

    if(!$ping){
        $error = 'HOST: {'.$dbHost.'};MESSAGE: '. $this->_mysqli->connect_error ."\n"; 
        DataStoreException::raiseHostUnreachable($error);
    }
} 
like image 678
Nenko Ivanov Avatar asked Jul 09 '10 06:07

Nenko Ivanov


People also ask

Can't connect to Cannot assign requested address?

This error is not specifically for macOS, it tells you that the provided IP is not available for your network or it's already in use. You should provide an assigned IP from your networks, you could execute hostname -I (or equivalent) to check them.

What is MySQLi -> Real_connect?

Definition and Usage. The real_connect() / mysqli_real_connect() function opens a new connection to the MySQL server. This function differs from connect() in the following ways: real_connect() requires a valid object created by init() real_connect() can be used with options() to set different options for the connection.

Can I use MySQLi in MySQL?

Yes – MySQLi has support for the multiple statements in the underlying MySQL database. This support is provided through the multi_query method of the MySQLi connection object. MySQL database has prepared statements. A prepared statement is used to execute the same query multiple times with higher efficiency.


1 Answers

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.

like image 83
yeeking Avatar answered Oct 09 '22 04:10

yeeking