MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since...
Transcript of MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since...
![Page 1: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/1.jpg)
{MySQL mysqlnd PHP driver
Serge Frezefond / Twitter : @sfrezefond
![Page 2: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/2.jpg)
{ MySQL Connectors
MySQL Server
Application
libmysql
Co
nn
ec
to
r/
C+
+
Co
nn
ec
to
r/
OD
BC
Co
nn
ec
to
r/
Ja
va
Co
nn
ec
to
r/
PH
P
Co
nn
ec
to
r/
Py
th
on
*
Co
nn
ec
to
r/
.Ne
t
Pe
rl
Ru
by …
LI
SP
![Page 3: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/3.jpg)
{ PHP mysqlnd driver
• MySQL PHP drivers part of the PHP distribution• Part of the php.net project• Three APIs (mysql, mysqli, pdo_mysql) and
one base library (mysqlnd)• http://php.net
![Page 4: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/4.jpg)
{ PHP mysqlnd driver
•No MySQL compile dependency•No MySQL client runtime dependency•Extensible•Available in 5.3, default in 5.4+
![Page 5: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/5.jpg)
{ PHP and mysqlnd
PHP
PHP Memory IO: PHP StreamsInfrastructure
ext/mysql PDO_mysqlmysqli
mysqlnd – MySQL native driver for PHP
MySQL Server
![Page 6: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/6.jpg)
{ libmysql vs. mysqlnd
MySQL Server MySQL Server
mysqlnd libmysql
PHP PHP
PHP Memory
libmysql Memory
PHP Memory
PHP Memory
![Page 7: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/7.jpg)
{ Mysqli embeds new mysqlnd enhancements
Key mysqli extension benefits compare to the mysql extension :
Object-oriented interface Support for Prepared Statements Support for Multiple Statements Support for Transactions Enhanced debugging capabilities Embedded server support
![Page 8: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/8.jpg)
{ <?php$connection = mysqli_connect('localhost', 'root', '', 'test');if (!$connection) { die('Error: ' . mysqli_connect_error());}$result = mysqli_query($connection, 'SELECT first_name, last_name FROM
employees LIMIT 5');if (!$result) { die('Error: ' . mysqli_error());}
echo “<table>\n”;while ($row = mysqli_fetch_assoc($result)) { printf(“<tr><td>%s</td><td>%s</td></tr>\n”, htmlentities($row['first_name']), htmlentities($row['last_name']) );}echo “</table>\n”;mysqli_free_result($result);mysqli_close($connection);?>
<?php$connection = mysqli_connect('localhost', 'root', '', 'test');if (!$connection) { die('Error: ' . mysqli_connect_error());}$result = mysqli_query($connection, 'SELECT first_name, last_name FROM
employees LIMIT 5');if (!$result) { die('Error: ' . mysqli_error());}
echo “<table>\n”;while ($row = mysqli_fetch_assoc($result)) { printf(“<tr><td>%s</td><td>%s</td></tr>\n”, htmlentities($row['first_name']), htmlentities($row['last_name']) );}echo “</table>\n”;mysqli_free_result($result);mysqli_close($connection);?>
Mysqli API PHP Example
![Page 9: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/9.jpg)
{ Mysqlnd install
Building PHP with mysqlnd
• ./configure \
--with-mysql=mysqlnd \
--with-mysqli=msqlnd \
--with-pdo-mysql=mysqlnd
• Default on Windows and some distributions
On linux distrib Incompatibility between pkgs
php-mysqlnd-5.3.10-1.fc16.x86_64.rpm
php-mysql- ...
![Page 10: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/10.jpg)
{ Mysqlnd / few dependencies
$ rpm -ql php-mysqlnd-5.3.13-1.fc16.x86_64
/etc/php.d/mysqlnd.ini mysqlnd_mysqli.ini pdo_mysqlnd.ini
/usr/lib64/php/modules/mysqlnd.so mysqlnd_mysqli.so pdo_mysqlnd.so
$ ldd pdo_mysqlnd.so
linux-vdso.so.1 => (0x00007fff64f96000)
libc.so.6 => /lib64/libc.so.6 (0x00007f28de038000)
/lib64/ld-linux-x86-64.so.2 (0x0000003dae000000)
![Page 11: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/11.jpg)
{ Mysqlnd features level
Special features compare to MySQL Client Library :
» Improved persistent connections
» The special function mysqli_fetch_all()
» Performance statistics calls: mysqli_get_cache_stats(), mysqli_get_client_stats(), mysqli_get_connection_stats()
![Page 12: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/12.jpg)
{ Mysqlnd features level
SSL Support
MySQL Native Driver supports SSL since PHP 5.3.3
Compressed Protocol Support
As of PHP 5.3.2 MySQL Native Driver supports the compressed client server protocol.
Note that PDO_MYSQL does NOT support compression when used together with mysqlnd.
Named Pipes Support
Named pipes support for Windows was added in PHP version 5.4.0.
![Page 13: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/13.jpg)
{ Mysql asynchronous query - only with mysqlnd -
<?php
...
$link1->query("SELECT 'test'", MYSQLI_ASYNC);
...
if (!mysqli_poll($links, $errors, $reject, 1)) {
...
$result = $link->reap_async_query();
...
?>
![Page 14: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/14.jpg)
{ client per-process statistics- only with mysqlnd -
• Around 150 statistic values collected– mysqli_get_client_stats ()
– mysqli_get_connection_stats()
• Check PS Execution Ratio– $stats = mysqli_get_client_stats();
echo $stats['com_execute'] / $stats['com_prepare'];
• Check for unprocessed rows$mysqli->close();
$stats = mysqli_get_client_stats();echo $stats['rows_skipped_normal'];
![Page 15: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/15.jpg)
{ Do more with mysqlnd plugins
Plugin Hook
mysqlnd Query
mysqli::query()mysql_query() PDO::query()
Wire Protocol
Plugin Hook
Network
![Page 16: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/16.jpg)
{ mysqlnd Plugins
• “mysqlnd client proxy”–Load Balancing• Read / Write splitting• Failover
–Monitoring• Query Logging• Query Auditing
–
Performance
– Caching
– throtling
– Sharding
– Memcached
– multiplexing
![Page 17: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/17.jpg)
{ MySQL mysqlnd and PECL :: The PHP Extension Community Library
•mysqlnd_ms– (ms for master/slave) A replication and load
balancing plugin for mysqlnd
•mysqlnd_qc A query cache plugin for mysqlnd•mysqlnd_uh mysqlnd Userland Handler•mysqlnd_mux connection multiplexing plugin•mysqlnd_memcache memcache plugin
![Page 18: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/18.jpg)
{ mysqlnd Query Cache
PHP
mysql / mysqli / PDO_mysql
mysqlnd
Cache Backend
Query Cache
MySQL Server
Local Memory, APC, Memcache, Custom Handler
![Page 19: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/19.jpg)
{ mysqlnd Query CacheKey Properties
• Transparent or hints
– PHP Extension hooking into mysqlnd
• Works with ext/mysql, mysqli, pdo_mysql
• Pluggable storage handler
– By default: local memory, APC, memcache, SQLite
– PHP Userspace
• Invalidation via TTL
– No automatic invalidation by server
– Custom handlers may use custom invalidation logic
![Page 20: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/20.jpg)
{ mysqlnd Query CacheTransparent ?
$mysqli = new mysqli($host, $user, $pw, $db);
$sql = sprintf(“/*%s*/SELECT SLEEP(10) FROM table”,
MYSQLND_QC_ENABLE_SWITCH);
...
Or
...
mysqlnd_qc.cache_by_default = 1
! Usually you should NOT do this!
![Page 21: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/21.jpg)
{ mysql_msLoad balancing!
Writes
Reads
Reads
Replication
![Page 22: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/22.jpg)
{ mysqlnd_ms
mysqlnd_ms
mysqli::query()mysql_query() PDO::query()
Application
Master Slave(s)
![Page 23: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/23.jpg)
{ mysqlnd_ms Configuration
{ "myapp": { "master": { "master_0": { "host": "localhost", "socket": "/tmp/mysql.sock" } }, "slave": { "slave_0": { "host": "192.168.2.27", "port": "3306" } } }}
![Page 24: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/24.jpg)
{ mysqlnd_ms: Connecting
$mysqli = new MySQLi(“myapp”, “user”, “pwd”);
$pdo = new PDO(“mysql:hostname=myapp”, “user”, “pwd”);
$mysql = mysql_connect(“myapp”, “user”, “pwd”);
$mysqli->query(“SELECT * FROM t”);$mysqli->query(“SET @sessionvar='value' ”);$mysqli->query(“SELECT @sessionvar”);
![Page 25: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/25.jpg)
{ Connection-Pooling: Fail-Over
• Default–No failover, error
• Possible–Automatic fail-over
•Warning:State changes!
![Page 26: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/26.jpg)
{ mysqlnd_ms Quality of Service
mysqlnd_ms is a load balancer at the driver level
controlled by :– SQL hints
– mysqlnd_ms_set_qos() defines the quality of service (QoS). tells load balancer how to select database cluster nodes .
Without GTIDs the rules for a MySQL Replication cluster are simple:
– eventual consistency – any slave, session– strong consistency – master only.
![Page 27: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/27.jpg)
{ mysqlnd_ms 1.5 + PHP 5.5.0 + MySQL 5.6 = better load balancing
2 possibilities :
- hint the load balancer about transaction boundaries
- monitor API calls controlling transactions
mysqli_begin_transaction(),
![Page 28: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/28.jpg)
{ Benefit from MySQL 5.6 read only transactions
For 5.6 : very important !
$mysqli->begin_transaction(
– MYSQLI_TRANS_START_READ_ONLY);
![Page 29: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/29.jpg)
{mysql_uh
writing mysqlnd plugin in php !David Soria Parra (lead)
Ulf Wendel
![Page 30: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/30.jpg)
{ myslqnd_uh for what ?
Monitoring
Queries executed by any of the PHP MySQL extensions
Prepared statements executing by any of the PHP MySQL extensions
Auditing
Detection of database usage
SQL injection protection using black and white lists
Assorted
Load Balancing connections
![Page 31: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/31.jpg)
{ myslqnd_uh how ?
Two classes are provided by the extension: MysqlndUhConnection and MysqlndUhPreparedStatement.
MysqlndUhConnection lets you access almost all methods of the mysqlnd internal connection class.
MysqlndUhPreparedStatement exposes some selected methods of the mysqlnd internal statement class
![Page 32: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/32.jpg)
{ mysqlnd_ mux
operate transparently from a user perspective.
Supports all of the PHP MySQL extensions.
Pooling reduces connection establishment latency
Reduces # connections to the MySQL server.
– horizontal scale-out (scale-by-client).
1:n relationship between internal network connection and user connection handles.
Persistent connection.
![Page 33: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/33.jpg)
{ mysqlnd_ memcache
PHP extension for transparently translating SQL into requests for the MySQL InnoDB Memcached Daemon Plugin (5.6 server plugin).
![Page 34: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/34.jpg)
{ Write your own mysqlnd plugin !
A mysqlnd plugin is itself a PHP extension.
A mysqlnd plugin works by intercepting calls made to mysqlnd by extensions that use mysqlnd.
This is achieved by obtaining the mysqlnd function table, backing it up, and replacing it by a custom function table, which calls the functions of the plugin as required.
![Page 35: MySQL mysqlnd PHP driver Serge Frezefond / Twitter ... · MySQL Native Driver supports SSL since PHP 5.3.3 Compressed Protocol Support As of PHP 5.3.2 MySQL Native Driver supports](https://reader033.fdocuments.in/reader033/viewer/2022052805/605a139c1cff87019914ea25/html5/thumbnails/35.jpg)
{ Questions
?Serge Frezefond / SkySQLTwitter : @sfrezefondBlog : http://Serge.frezefond.com