File: quickstart.xml

package info (click to toggle)
php-doc 20140201-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 74,084 kB
  • ctags: 4,040
  • sloc: xml: 998,137; php: 20,812; cpp: 500; sh: 177; makefile: 63; awk: 28
file content (424 lines) | stat: -rw-r--r-- 15,944 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 325471 $ -->
<chapter xml:id="mysqlnd-uh.quickstart" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <title>Quickstart and Examples</title>
 <para>
  The mysqlnd user handler plugin can be understood as a client-side proxy
  for all PHP MySQL extensions (<link linkend="ref.mysqli">mysqli</link>,
  <link linkend="ref.mysql">mysql</link>,
  <link linkend="ref.pdo-mysql">PDO_MYSQL</link>), if they are compiled to
  use the <link linkend="book.mysqlnd">mysqlnd</link> library. The
  extensions use the <literal>mysqlnd</literal> library internally, at the C
  level, to communicate with the MySQL server. PECL/mysqlnd_uh
  allows it to hook many <literal>mysqlnd</literal> calls. Therefore,
  most activities of the PHP MySQL extensions can be monitored.
 </para>
 <para>
  Because monitoring happens at the level of the library, at a layer below the
  application, it is possible to monitor applications without changing them.
 </para>
 <para>
  On the C level, the <literal>mysqlnd</literal> library is structured in modules
  or classes. The extension hooks almost all methods of the <literal>mysqlnd</literal>
  internal <literal>connection</literal> class and exposes them through the
  user space class <classname>MysqlndUhConnection</classname>. Some few methods of
  the mysqlnd internal <literal>statement</literal> class are made available
  to the PHP user with the class <classname>MysqlndUhPreparedStatement</classname>.
  By subclassing the classes <classname>MysqlndUhConnection</classname> and
  <classname>MysqlndUhPreparedStatement</classname> users get access to
  <literal>mysqlnd</literal> internal function calls.
 </para>
 <note>
  <para>
   The internal <literal>mysqlnd</literal> function calls are not designed
   to be exposed to the PHP user. Manipulating their activities may
   cause PHP to crash or leak memory. Often, this is not considered a bug. Please,
   keep in mind that you are accessing C library functions through
   PHP which are expected to take certain actions, which you may not be able to
   emulate in user space. Therefore, it is strongly recommended to always call
   the parent method implementation when subclassing <classname>MysqlndUhConnection</classname>
   or <classname>MysqlndUhPreparedStatement</classname>. To prevent the worst
   case, the extension performs some sanity checks. Please, see also the
   <link linkend="mysqlnd-uh.configuration">Mysqlnd_uh &ConfigureOptions;</link>.
  </para>
 </note>


 <section xml:id="mysqlnd-uh.quickstart.configuration">
  <title>Setup</title>
  <para>
   The plugin is implemented as a PHP extension. See the
   <link linkend="mysqlnd-uh.installation">installation instructions</link> to
   install the
   <link xlink:href="&url.pecl.package;mysqlnd_ms">PECL/mysqlnd_uh</link> extension.
   Then, load the extension into PHP and activate the plugin in the PHP configuration
   file using the PHP configuration directive named
   <link linkend="ini.mysqlnd-uh.enable">mysqlnd_uh.enable</link>.
   The below example shows the default settings of the extension.
  </para>
  <para>
   <example>
    <title>Enabling the plugin (php.ini)</title>
    <programlisting role="ini">
<![CDATA[
mysqlnd_uh.enable=1
mysqlnd_uh.report_wrong_types=1
]]>
    </programlisting>
    </example>
  </para>
 </section>

 <section xml:id="mysqlnd-uh.quickstart.how-it-works">
  <title>How it works</title>
  <para>
   This describes the background and inner workings of the mysqlnd_uh
   extension.
  </para>
  <para>
   Two classes are provided by the extension: <classname>MysqlndUhConnection</classname>
   and <classname>MysqlndUhPreparedStatement</classname>. <classname>MysqlndUhConnection</classname> lets
   you access almost all methods of the <literal>mysqlnd</literal>
   internal <literal>connection</literal> class. The latter exposes some selected
   methods of the  <literal>mysqlnd</literal> internal <literal>statement</literal> class.
   For example, <methodname>MysqlndUhConnection::connect</methodname> maps to
   the <literal>mysqlnd</literal> library C function
   <literal>mysqlnd_conn__connect</literal>.
  </para>
  <para>
   As a mysqlnd plugin, the PECL/mysqlnd_uh extension replaces <literal>mysqlnd</literal>
   library C functions with its own functions. Whenever a
   PHP MySQL extension compiled to use <literal>mysqlnd</literal> calls
   a mysqlnd function, the functions installed by the plugin are executed
   instead of the original <literal>mysqlnd</literal> ones. For example,
   <function>mysqli_connect</function> invokes <literal>mysqlnd_conn__connect</literal>,
   so the connect function installed by PECL/mysqlnd_uh will be called.
   The functions installed by PECL/mysqlnd_uh are the methods of the built-in classes.
  </para>
  <para>
   The built-in PHP classes and their methods do nothing but call their
   <literal>mysqlnd</literal> C library counterparts, to behave exactly
   like the original <literal>mysqlnd</literal> function they replace.
   The code below illustrates in pseudo-code what the extension does.
  </para>
  <para>
   <example>
    <title>Pseudo-code: what a built-in class does</title>
    <programlisting>
<![CDATA[
class MysqlndUhConnection {
  public function connect(($conn, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {
    MYSQLND* c_mysqlnd_connection = convert_from_php_to_c($conn);
    ...
    return call_c_function(mysqlnd_conn__connect(c_mysqlnd_connection, ...));
  }
}
]]>
    </programlisting>
    </example>
  </para>
  <para>
   The build-in classes behave like a transparent proxy. It is possible for
   you to replace the proxy with your own. This is done by subclassing
   <classname>MysqlndUhConnection</classname> or
   <classname>MysqlndUhPreparedStatement</classname> to extend the functionality
   of the proxy, followed by registering a new proxy object.
   Proxy objects are installed by
   <function>mysqlnd_uh_set_connection_proxy</function>
   and
   <function>mysqlnd_uh_set_statement_proxy</function>.
  </para>
  <para>
   <example>
    <title>Installing a proxy</title>
        <programlisting role="php">
<![CDATA[
<?php
class proxy extends MysqlndUhConnection {
 public function connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {
   printf("%s(%s)\n", __METHOD__, var_export(func_get_args(), true));
   $ret = parent::connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags);
   printf("%s returns %s\n", __METHOD__, var_export($ret, true));
   return $ret;
 }
}
mysqlnd_uh_set_connection_proxy(new proxy());

$mysqli = new mysqli("localhost", "root", "", "test");
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
proxy::connect(array (
  0 => NULL,
  1 => 'localhost',
  2 => 'root',
  3 => '',
  4 => 'test',
  5 => 3306,
  6 => NULL,
  7 => 131072,
))
proxy::connect returns true
]]>
    </screen>
   </example>
  </para>
 </section>

 <section xml:id="mysqlnd-uh.quickstart.proxy-installation">
  <title>Installing a proxy</title>
  <para>
   The extension provides two built-in classes: <classname>MysqlndUhConnection</classname>
   and <classname>MysqlndUhPreparedStatement</classname>. The classes are used
   for hooking <literal>mysqlnd</literal> library calls. Their methods correspond
   to <literal>mysqlnd</literal> internal functions. By default they act like
   a transparent proxy and do nothing but call their <literal>mysqlnd</literal> counterparts.
   By subclassing the classes you can install your own proxy to monitor <literal>mysqlnd</literal>.
  </para>
  <para>
   See also the <link linkend="mysqlnd-uh.quickstart.how-it-works">How it works</link>
   guide to learn about the inner workings of this extension.
  </para>
  <para>
   Connection proxies are objects of the type <classname>MysqlndUhConnection</classname>.
   Connection proxy objects are installed by
   <function>mysqlnd_uh_set_connection_proxy</function>.
   If you install the built-in class <classname>MysqlndUhConnection</classname>
   as a proxy, nothing happens. It behaves like a transparent proxy.
  </para>
 <para>
   <example>
    <title>Proxy registration, mysqlnd_uh.enable=1</title>
        <programlisting role="php">
<![CDATA[
<?php
mysqlnd_uh_set_connection_proxy(new MysqlndUhConnection());
$mysqli = new mysqli("localhost", "root", "", "test");
?>
]]>
    </programlisting>
   </example>
  </para>
  <para>
   The <literal>PHP_INI_SYSTEM</literal> configuration setting
   <literal><link linkend="ini.mysqlnd-uh.enable">mysqlnd_uh.enable</link></literal>
   controls whether a proxy may be set. If disabled, the extension
   will throw errors of type <literal>E_WARNING</literal>
  </para>
  <para>
   <example>
    <title>Proxy installation disabled</title>
     <programlisting role="ini">
<![CDATA[
mysqlnd_uh.enable=0
]]>
    </programlisting>
        <programlisting role="php">
<![CDATA[
<?php
mysqlnd_uh_set_connection_proxy(new MysqlndUhConnection());
$mysqli = new mysqli("localhost", "root", "", "test");
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
PHP Warning:  MysqlndUhConnection::__construct(): (Mysqlnd User Handler) The plugin has been disabled by setting the configuration parameter mysqlnd_uh.enabled = false.  You must not use any of the base classes in %s on line %d
PHP Warning:  mysqlnd_uh_set_connection_proxy(): (Mysqlnd User Handler) The plugin has been disabled by setting the configuration parameter mysqlnd_uh.enable = false. The proxy has not been installed  in %s on line %d
]]>
    </screen>
   </example>
  </para>
  <para>
   To monitor <literal>mysqlnd</literal>, you have to write your own
   proxy object subclassing <classname>MysqlndUhConnection</classname>.
   Please, see the function reference for a the list of methods that
   can be subclassed. Alternatively, you can use reflection to inspect
   the built-in <classname>MysqlndUhConnection</classname>.
  </para>
  <para>
    Create a new class <literal>proxy</literal>. Derive it from the
    built-in class <classname>MysqlndUhConnection</classname>.
    Replace the
    <function>MysqlndUhConnection::connect</function>.
    method. Print out the host parameter value passed to the method.
    Make sure that you call the parent implementation of the <literal>connect</literal>
    method. Failing to do so may give unexpected and undesired results, including
    memory leaks and crashes.
  </para>
  <para>
   Register your proxy and open three connections using the PHP MySQL
   extensions <link linkend="ref.mysqli">mysqli</link>,
   <link linkend="ref.mysql">mysql</link>,
   <link linkend="ref.pdo-mysql">PDO_MYSQL</link>. If the extensions have been
   compiled to use the <literal>mysqlnd</literal> library, the
   <literal>proxy::connect</literal> method will be called three times, once
   for each connection opened.
  </para>
  <para>
   <example>
    <title>Connection proxy</title>
        <programlisting role="php">
<![CDATA[
<?php
class proxy extends MysqlndUhConnection {
  public function connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {
   printf("Connection opened to '%s'\n", $host);
   /* Always call the parent implementation! */
   return parent::connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags);
  }
}
mysqlnd_uh_set_connection_proxy(new proxy());

$mysqli = new mysqli("localhost", "root", "", "test");
$mysql = mysql_connect("localhost", "root", "");
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
Connection opened to 'localhost'
Connection opened to 'localhost'
Connection opened to 'localhost'
]]>
    </screen>
   </example>
  </para>
  <para>
   The use of prepared statement proxies follows the same pattern: create a
   proxy object of the type <classname>MysqlndUhPreparedStatement</classname>
   and install the proxy using
   <function>mysqlnd_uh_set_statement_proxy</function>.
  </para>
  <para>
   <example>
    <title>Prepared statement proxy</title>
        <programlisting role="php">
<![CDATA[
<?php
class stmt_proxy extends MysqlndUhPreparedStatement {
 public function prepare($res, $query) {
  printf("%s(%s)\n", __METHOD__, $query);
  return parent::prepare($res, $query);
 }
}
mysqlnd_uh_set_statement_proxy(new stmt_proxy());

$mysqli = new mysqli("localhost", "root", "", "test");
$stmt = $mysqli->prepare("SELECT 'mysqlnd hacking made easy' AS _msg FROM DUAL");
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
stmt_proxy::prepare(SELECT 'mysqlnd hacking made easy' AS _msg FROM DUAL)
]]>
    </screen>
   </example>
  </para>
 </section>

 <section xml:id="mysqlnd-uh.quickstart.query-monitoring">
  <title>Basic query monitoring</title>
  <para>
   Basic monitoring of a query statement is easy with PECL/mysqlnd_uh.
   Combined with <function>debug_print_backtrace</function> it can become a powerful
   tool, for example, to find the origin of certain statement. This may
   be desired when searching for slow queries but also after database
   refactoring to find code still accessing deprecated databases or
   tables. The latter may be a complicated matter to do otherwise, especially if
   the application uses auto-generated queries.
  </para>
  <para>
   <example>
    <title>Basic Monitoring</title>
        <programlisting role="php">
<![CDATA[
<?php
class conn_proxy extends MysqlndUhConnection {
 public function query($res, $query) {
  debug_print_backtrace();
  return parent::query($res, $query);
 }
}
class stmt_proxy extends MysqlndUhPreparedStatement {
 public function prepare($res, $query) {
  debug_print_backtrace();
  return parent::prepare($res, $query);
 }
}
mysqlnd_uh_set_connection_proxy(new conn_proxy());
mysqlnd_uh_set_statement_proxy(new stmt_proxy());

printf("Proxies installed...\n");
$pdo = new PDO("mysql:host=localhost;dbname=test", "root", "");
var_dump($pdo->query("SELECT 1 AS _one FROM DUAL")->fetchAll(PDO::FETCH_ASSOC));

$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->prepare("SELECT 1 AS _two FROM DUAL");
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
#0  conn_proxy->query(Resource id #19, SELECT 1 AS _one FROM DUAL)
#1  PDO->query(SELECT 1 AS _one FROM DUAL) called at [example.php:19]
array(1) {
  [0]=>
  array(1) {
    ["_one"]=>
    string(1) "1"
  }
}
#0  stmt_proxy->prepare(Resource id #753, SELECT 1 AS _two FROM DUAL)
#1  mysqli->prepare(SELECT 1 AS _two FROM DUAL) called at [example.php:22]
]]>
    </screen>
   </example>
  </para>
  <para>
   For basic query monitoring you should install a connection and a prepared statement
   proxy. The connection proxy should subclass <function>MysqlndUhConnection::query</function>.
   All database queries not using native prepared statements will call this method.
   In the example the <literal>query</literal> function is invoked by a PDO call.
   By default, <literal>PDO_MySQL</literal> is using prepared statement emulation.
  </para>
  <para>
   All native prepared statements are prepared with the <literal>prepare</literal>
   method of <literal>mysqlnd</literal> exported through
   <methodname>MysqlndUhPreparedStatement::prepare</methodname>. Subclass
   <classname>MysqlndUhPreparedStatement</classname> and overwrite <literal>prepare</literal>
   for native prepared statement monitoring.
  </para>
 </section>


</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->