Intro to PECL/mysqlnd_ms (4/7/2011)

11,177 views

Published on

I gave this presentation about the totally awesome mysqlnd_ms PHP extension at the Minnesota PHP User Group.

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

No Downloads
Views
Total views
11,177
On SlideShare
0
From Embeds
0
Number of Embeds
1,988
Actions
Shares
0
Downloads
65
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Intro to PECL/mysqlnd_ms (4/7/2011)

    1. 1. Intro to PECL/mysqlnd_msChris BarberMinnesota PHP User GroupApril 7, 2011
    2. 2. About MeChris BarberLead Software Engineer @ AppceleratorCEO/Technology Consultant @ CB1, INC.JavaScript, PHP, C++http://www.cb1inc.com@cb1kenobi on Twitter, Slideshare
    3. 3. What is mysqlnd?
    4. 4. What is mysqlnd?MySQL Native Driver for PHPPHP 5.3+Replaces libmysqlUsed by ext/mysql, ext/mysqli, PDO_MYSQLUse configure options: --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlndNot compiled into Ubuntu’s PHP 5.3 package :(
    5. 5. What is mysqlnd_ms?
    6. 6. What is mysqlnd_ms?A PHP Extension that provides transparent MySQLload balancing across master and slave servers
    7. 7. How it worksCreate a mysqlnd_ms_config.ini with your settingsMake MySQL calls from your PHP codeINSERT, UPDATE, & DELETE statements go to masterSELECT statements go to slaves
    8. 8. Getting started
    9. 9. php.iniextension=mysqlnd_ms.somysqlnd_ms.enable=1mysqlnd_ms.ini_file=mysqlnd_ms_config.ini
    10. 10. mysqlnd_ms_config.ini[myapp]master[]=192.168.1.120slave[]=192.168.1.121slave[]=192.168.1.122
    11. 11. PHP Usage<?php$db = mysqli(“myapp”, “user”, “password”);$results = $db->query(“SELECT ...”);$db->query(“INSERT ...”);?>
    12. 12. Which server to connect to?
    13. 13. Choosing a serverAvailable load balancing algorithms: random round-robin user defined (custom PHP function) random_once (sticky)Specify algorithm in the mysqlnd_ms_config.ini file
    14. 14. Choosing a server [myapp] master[]=192.168.1.120 slave[]=192.168.1.121 slave[]=192.168.1.122 pick[]=user pick[]=random<?phpfunction pick_server($connected_host, $query, $master, $slaves, $last_used_connection) { if (stristr($query, “FROM table_a”)) return “hostname_of_slave_for_table_a”; return NULL; // resort to random/random-once/round-robin}mysqlnd_ms_set_user_pick_server(“pick_server”);?>
    15. 15. Issues...
    16. 16. IssuesINSERT and immediate SELECT Replication lagTransactions Need server stickinessFailover
    17. 17. Hints Prefix query with a comment!<?php$db = mysqli(“myapp”, “user”, “password”);$results = $db->query(“/* HINT_GOES_HERE */SELECT ...”);?>
    18. 18. Built-in Hints MYSQLND_MS_MASTER_SWITCH MYSQLND_MS_SLAVE_SWITCH MYSQLND_MS_LAST_USED_SWITCH MYSQLND_MS_ALL_SERVER_SWITCH MYSQLND_MS_QUERY_USE_MASTER MYSQLND_MS_QUERY_USE_SLAVE MYSQLND_MS_QUERY_USE_LAST_USED
    19. 19. Hints<?php$db->query(sprintf(“/*%s*/SET @a=1”, MYSQL_MS_SLAVE_SWITCH));$db->query(sprintf(“/*%s*/SET @a=@a+1”, MYSQL_MS_LAST_USED_SWITCH));$db->query(sprintf(“/*%s*/SELECT @a as _a”, MYSQL_MS_LAST_USED_SWITCH));?>
    20. 20. Custom Hints<?phpfunction pick_server($connected_host, $query, $master, $slaves, $last_used_connection) { if (preg_match("@^/*.+*/@ismU", $query, $matches)) { $json = json_decode($matches[1]) if ($json && $json->something_enabled) { return “some_special_hostname”; } } return NULL; // resort to random/random-once/round-robin}mysqlnd_ms_set_user_pick_server(“pick_server”);$db->query(“/* {“something_enabled”:true} */SELECT ...”);?>
    21. 21. Server Picking Ideas Geo-load balancing Routing around server maintenance Sharding Dynamically add/override master/slave hostnames
    22. 22. Failover mysqlnd_ms does not do failover Do it in your application! Retry query N number of times before falling on sword Have pick_server() “disable” dead servers for a minute Perhaps use APC to cache which servers are dead
    23. 23. Getting mysqlnd_mssvn co http://svn.php.net/repository/pecl/mysqlnd_ms/trunk mysqlnd_mscd mysqlnd_msphpizeconfiguremakesudo make install
    24. 24. More InfoAuthors: Andrey Hristov, Ulf Wendel, & JohannesSchlueterhttp://blog.ulf-wendel.dehttp://svn.php.net/viewvc/pecl/mysqlnd_ms
    25. 25. ThanksQuestions?

    ×