Oracle 5.0 Reference Manual page 2606

Table of Contents

Advertisement

Mysqlnd replication and load balancing plugin (mysqlnd_ms)
If using lazy connections, which is the default, connections are not opened until they are needed for
query execution. Therefore, an API call for a statement execution may return a connection error. In
the example below, an error is provoked when trying to run a statement on a slave. Opening a slave
connection fails because the plugin configuration file lists an invalid host name for the slave.
Example 20.254. Provoking a connection error
{
"myapp": {
"master": {
"master_0": {
"host": "localhost",
"socket": "\/tmp\/mysql.sock"
}
},
"slave": {
"slave_0": {
"host": "invalid_host_name",
}
},
"lazy_connections": 1
}
}
The explicit activation of lazy connections is for demonstration purpose only.
Example 20.255. Connection error on query execution
<?php
$mysqli = new mysqli("myapp", "username", "password", "database");
if (mysqli_connect_errno())
/* Of course, your error handling is nicer... */
die(sprintf("[%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
/* Connection 1, connection bound SQL user variable, no SELECT thus run on master */
if (!$mysqli->query("SET @myrole='master'")) {
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
}
/* Connection 2, run on slave because SELECT, provoke connection error */
if (!($res = $mysqli->query("SELECT @myrole AS _role"))) {
printf("[%d] %s\n", $mysqli->errno, $mysqli->error);
} else {
$row = $res->fetch_assoc();
$res->close();
printf("@myrole = '%s'\n", $row['_role']);
}
$mysqli->close();
?>
The above example will output something similar to:
PHP Warning:
mysqli::query(): php_network_getaddresses: getaddrinfo failed: Name or service not known in %
PHP Warning:
mysqli::query(): [2002] php_network_getaddresses: getaddrinfo failed: Name or service not kno
[2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
Applications are expected to handle possible connection errors by implementing proper error handling.
Depending on the use case, applications may want to handle connection errors differently from
other errors. Typical connection errors are
to local MySQL server through socket '%s'
2002 (CR_CONNECTION_ERROR) - Can't connect
(%d),
2003 (CR_CONN_HOST_ERROR)
2586

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents