Geen cache-versie.


Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

mysqlnd _ms _set _user _pick _server

(PECL mysqlnd_ms < 1.1.0)

mysqlnd_ms_set_user_pick_serverSets a callback for user-defined read/write splitting

Description

mysqlnd_ms_set_user_pick_server ( string $function ) : bool

Sets a callback for user-defined read/write splitting. The plugin will call the callback only if pick[]=user is the default rule for server picking in the relevant section of the plugins configuration file.

The plugins built-in read/write query split mechanism decisions can be overwritten in two ways. The easiest way is to prepend the query string with the SQL hints MYSQLND_MS_MASTER_SWITCH, MYSQLND_MS_SLAVE_SWITCH or MYSQLND_MS_LAST_USED_SWITCH. Using SQL hints one can control, for example, whether a query shall be send to the MySQL replication master server or one of the slave servers. By help of SQL hints it is not possible to pick a certain slave server for query execution.

Full control on server selection can be gained using a callback function. Use of a callback is recommended to expert users only because the callback has to cover all cases otherwise handled by the plugin.

The plugin will invoke the callback function for selecting a server from the lists of configured master and slave servers. The callback function inspects the query to run and picks a server for query execution by returning the hosts URI, as found in the master and slave list.

If the lazy connections are enabled and the callback chooses a slave server for which no connection has been established so far and establishing the connection to the slave fails, the plugin will return an error upon the next action on the failed connection, for example, when running a query. It is the responsibility of the application developer to handle the error. For example, the application can re-run the query to trigger a new server selection and callback invocation. If so, the callback must make sure to select a different slave, or check slave availability, before returning to the plugin to prevent an endless loop.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

function

The function to be called. Class methods may also be invoked statically using this function by passing array($classname, $methodname) to this parameter. Additionally class methods of an object instance may be called by passing array($objectinstance, $methodname) to this parameter.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

Host to run the query on. The host URI is to be taken from the master and slave connection lists passed to the callback function. If callback returns a value neither found in the master nor in the slave connection lists the plugin will fallback to the second pick method configured via the pick[] setting in the plugin configuration file. If not second pick method is given, the plugin falls back to the build-in default pick method for server selection.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Notes

Note:

mysqlnd_ms_set_user_pick_server() is available with PECL mysqlnd_ms < 1.1.0. It has been replaced by the user filter. Please, check the Change History for upgrade notes.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #1 mysqlnd_ms_set_user_pick_server() example

[myapp]
master[] = localhost
slave[] = 192.168.2.27:3306
slave[] = 192.168.78.136:3306
pick[] = user
<?php

function pick_server($connected$query$master$slaves$last_used)
{
 static 
$slave_idx 0;
 static 
$num_slaves NULL;
 if (
is_null($num_slaves))
  
$num_slaves count($slaves);

 
/* default: fallback to the plugins build-in logic */
 
$ret NULL;

 
printf("User has connected to '%s'...\n"$connected);
 
printf("... deciding where to run '%s'\n"$query);

 
$where mysqlnd_ms_query_is_select($query);
 switch (
$where)
 {
  case 
MYSQLND_MS_QUERY_USE_MASTER:
   
printf("... using master\n");
   
$ret $master[0];
   break;
  case 
MYSQLND_MS_QUERY_USE_SLAVE:
   
/* SELECT or SQL hint for using slave */
   
if (stristr($query"FROM table_on_slave_a_only"))
   {
    
/* a table which is only on the first configured slave  */
    
printf("... access to table available only on slave A detected\n");
    
$ret $slaves[0];
   }
   else
   {
    
/* round robin */
    
printf("... some read-only query for a slave\n");
    
$ret $slaves[$slave_idx++ % $num_slaves];
   }
   break;
  case 
MYSQLND_MS_QUERY_LAST_USED:
   
printf("... using last used server\n");
   
$ret $last_used;
   break;
 }

 
printf("... ret = '%s'\n"$ret);
 return 
$ret;
}

mysqlnd_ms_set_user_pick_server("pick_server");

$mysqli = new mysqli("myapp""root""root""test");

if (!(
$res $mysqli->query("SELECT 1 FROM DUAL")))
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
else
 
$res->close();

if (!(
$res $mysqli->query("SELECT 2 FROM DUAL")))
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
else
 
$res->close();


if (!(
$res $mysqli->query("SELECT * FROM table_on_slave_a_only")))
 
printf("[%d] %s\n"$mysqli->errno$mysqli->error);
else
 
$res->close();

$mysqli->close();
?>

The above example will output:

User has connected to 'myapp'...
... deciding where to run 'SELECT 1 FROM DUAL'
... some read-only query for a slave
... ret = 'tcp://192.168.2.27:3306'
User has connected to 'myapp'...
... deciding where to run 'SELECT 2 FROM DUAL'
... some read-only query for a slave
... ret = 'tcp://192.168.78.136:3306'
User has connected to 'myapp'...
... deciding where to run 'SELECT * FROM table_on_slave_a_only'
... access to table available only on slave A detected
... ret = 'tcp://192.168.2.27:3306'

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

See Also

Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-mysqlnd-ms-set-user-pick-server.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut