SlideShare a Scribd company logo
Intro to PECL/mysqlnd_ms
Chris Barber
Minnesota PHP User Group
April 7, 2011
About Me

Chris Barber
Lead Software Engineer @ Appcelerator
CEO/Technology Consultant @ CB1, INC.
JavaScript, PHP, C++
http://www.cb1inc.com
@cb1kenobi on Twitter, Slideshare
What is mysqlnd?
What is mysqlnd?

MySQL Native Driver for PHP
PHP 5.3+
Replaces libmysql
Used by ext/mysql, ext/mysqli, PDO_MYSQL
Use configure options: --with-mysql=mysqlnd --with-
mysqli=mysqlnd --with-pdo-mysql=mysqlnd
Not compiled into Ubuntu’s PHP 5.3 package :(
What is mysqlnd_ms?
What is mysqlnd_ms?



A PHP Extension that provides transparent MySQL
load balancing across master and slave servers
How it works


Create a mysqlnd_ms_config.ini with your settings
Make MySQL calls from your PHP code
INSERT, UPDATE, & DELETE statements go to master
SELECT statements go to slaves
Getting started
php.ini

extension=mysqlnd_ms.so
mysqlnd_ms.enable=1
mysqlnd_ms.ini_file=mysqlnd_ms_config.ini
mysqlnd_ms_config.ini

[myapp]
master[]=192.168.1.120
slave[]=192.168.1.121
slave[]=192.168.1.122
PHP Usage
<?php

$db = mysqli(“myapp”, “user”, “password”);

$results = $db->query(“SELECT ...”);

$db->query(“INSERT ...”);

?>
Which server to connect to?
Choosing a server

Available load balancing algorithms:
  random
  round-robin
  user defined (custom PHP function)
  random_once (sticky)
Specify algorithm in the mysqlnd_ms_config.ini file
Choosing a server
       [myapp]
       master[]=192.168.1.120
       slave[]=192.168.1.121
       slave[]=192.168.1.122
       pick[]=user
       pick[]=random




<?php

function 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”);

?>
Issues...
Issues

INSERT and immediate SELECT
  Replication lag
Transactions
  Need server stickiness
Failover
Hints

  Prefix query with a comment!
<?php

$db = mysqli(“myapp”, “user”, “password”);
$results = $db->query(“/* HINT_GOES_HERE */SELECT ...”);

?>
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
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));

?>
Custom Hints

<?php

function 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 ...”);

?>
Server Picking Ideas


 Geo-load balancing
 Routing around server maintenance
 Sharding
 Dynamically add/override master/slave hostnames
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
Getting mysqlnd_ms
svn co http://svn.php.net/repository/pecl/mysqlnd_ms/
trunk mysqlnd_ms
cd mysqlnd_ms
phpize
configure
make
sudo make install
More Info


Authors: Andrey Hristov, Ulf Wendel, & Johannes
Schlueter
http://blog.ulf-wendel.de
http://svn.php.net/viewvc/pecl/mysqlnd_ms
Thanks
Questions?

More Related Content

What's hot (20)

ODP
Award-winning technology: Oxid loves the query cache
Ulf Wendel
 
ODP
NoSQL in MySQL
Ulf Wendel
 
ODP
MySQL 5.7 Fabric: Introduction to High Availability and Sharding
Ulf Wendel
 
ODP
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
Ulf Wendel
 
ODP
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
Ulf Wendel
 
ODP
PoC: Using a Group Communication System to improve MySQL Replication HA
Ulf Wendel
 
PDF
DIY: A distributed database cluster, or: MySQL Cluster
Ulf Wendel
 
ODP
MySQL Group Replication
Ulf Wendel
 
ODP
MySQL 5.7 clustering: The developer perspective
Ulf Wendel
 
PDF
HTTP Plugin for MySQL!
Ulf Wendel
 
PDF
Highly Available MySQL/PHP Applications with mysqlnd
Jervin Real
 
PDF
MySQL Group Replication
Bogdan Kecman
 
PDF
MySQL Group Replication - an Overview
Matt Lord
 
PDF
MySQL High Availability: Managing Farms of Distributed Servers (MySQL Fabric)
Alfranio Júnior
 
PPTX
MySQL Multi Master Replication
Moshe Kaplan
 
PDF
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
PDF
MySQL Group Replication
Manish Kumar
 
PDF
MySQL Database Architectures - 2020-10
Kenny Gryp
 
DOCX
Mater,slave on mysql
Vasudeva Rao
 
PDF
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
Serge Frezefond
 
Award-winning technology: Oxid loves the query cache
Ulf Wendel
 
NoSQL in MySQL
Ulf Wendel
 
MySQL 5.7 Fabric: Introduction to High Availability and Sharding
Ulf Wendel
 
MySQL native driver for PHP (mysqlnd) - Introduction and overview, Edition 2011
Ulf Wendel
 
MySQL 5.6 Global Transaction IDs - Use case: (session) consistency
Ulf Wendel
 
PoC: Using a Group Communication System to improve MySQL Replication HA
Ulf Wendel
 
DIY: A distributed database cluster, or: MySQL Cluster
Ulf Wendel
 
MySQL Group Replication
Ulf Wendel
 
MySQL 5.7 clustering: The developer perspective
Ulf Wendel
 
HTTP Plugin for MySQL!
Ulf Wendel
 
Highly Available MySQL/PHP Applications with mysqlnd
Jervin Real
 
MySQL Group Replication
Bogdan Kecman
 
MySQL Group Replication - an Overview
Matt Lord
 
MySQL High Availability: Managing Farms of Distributed Servers (MySQL Fabric)
Alfranio Júnior
 
MySQL Multi Master Replication
Moshe Kaplan
 
Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication
Kenny Gryp
 
MySQL Group Replication
Manish Kumar
 
MySQL Database Architectures - 2020-10
Kenny Gryp
 
Mater,slave on mysql
Vasudeva Rao
 
MySQL PHP native driver : Advanced Functions / PHP forum Paris 2013
Serge Frezefond
 

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

KEY
Mysqlnd uh
natmchugh
 
PDF
Ipc mysql php
Anis Berejeb
 
PPTX
20141011 mastering mysqlnd
do_aki
 
PDF
MySQL NoSQL APIs
Morgan Tocker
 
PDF
Barcelona mysqlnd qc
Anis Berejeb
 
PDF
Mysqlnd Query Cache
Anis Berejeb
 
PDF
Mysqlnd, an unknown powerful PHP extension
julien pauli
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
Php and database functionality
Sayed Ahmed
 
PPTX
PHP and database functionality
Sayed Ahmed
 
PPTX
3-Chapter-Edit.pptx debre tabour university
alemunuruhak9
 
PDF
Get mysql clusterrunning-windows
JoeSg
 
PDF
Mysql tracing
Anis Berejeb
 
PDF
Mysql tracing
Anis Berejeb
 
ODP
MySQL HA
Kris Buytaert
 
PPT
MySQL Cluster Basics
Wagner Bianchi
 
PDF
Mysql wp cluster_quickstart_windows
Rogério Rocha
 
PDF
OSMC 2008 | Monitoring MySQL by Geert Vanderkelen
NETWAYS
 
PPT
Php classes in mumbai
aadi Surve
 
PDF
MySQL's NoSQL -- SCaLE 13x Feb. 20, 2015
Dave Stokes
 
Mysqlnd uh
natmchugh
 
Ipc mysql php
Anis Berejeb
 
20141011 mastering mysqlnd
do_aki
 
MySQL NoSQL APIs
Morgan Tocker
 
Barcelona mysqlnd qc
Anis Berejeb
 
Mysqlnd Query Cache
Anis Berejeb
 
Mysqlnd, an unknown powerful PHP extension
julien pauli
 
Php and database functionality
Sayed Ahmed
 
Php and database functionality
Sayed Ahmed
 
PHP and database functionality
Sayed Ahmed
 
3-Chapter-Edit.pptx debre tabour university
alemunuruhak9
 
Get mysql clusterrunning-windows
JoeSg
 
Mysql tracing
Anis Berejeb
 
Mysql tracing
Anis Berejeb
 
MySQL HA
Kris Buytaert
 
MySQL Cluster Basics
Wagner Bianchi
 
Mysql wp cluster_quickstart_windows
Rogério Rocha
 
OSMC 2008 | Monitoring MySQL by Geert Vanderkelen
NETWAYS
 
Php classes in mumbai
aadi Surve
 
MySQL's NoSQL -- SCaLE 13x Feb. 20, 2015
Dave Stokes
 
Ad

More from Chris Barber (11)

PPTX
Remote IP Power Switches
Chris Barber
 
PPTX
Node.js/io.js Native C++ Addons
Chris Barber
 
PPTX
Titanium 3.2 CLI - TiAppCamp2 - 11/2/2013
Chris Barber
 
PPTX
Exploring the Titanium CLI - Codestrong 2012
Chris Barber
 
PDF
Cassandra - Say Goodbye to the Relational Database (5-6-2010)
Chris Barber
 
PDF
Debugging Dojo Applications (2/10/2010)
Chris Barber
 
PDF
Titanium Powered Desktop & Mobile Apps (11/21/2009)
Chris Barber
 
PDF
Dojo - Javascript's Swiss Army Knife (7/15/2009)
Chris Barber
 
PDF
High Availability With DRBD & Heartbeat
Chris Barber
 
PDF
2008 MySQL Conference Recap
Chris Barber
 
PDF
Memcached And MySQL
Chris Barber
 
Remote IP Power Switches
Chris Barber
 
Node.js/io.js Native C++ Addons
Chris Barber
 
Titanium 3.2 CLI - TiAppCamp2 - 11/2/2013
Chris Barber
 
Exploring the Titanium CLI - Codestrong 2012
Chris Barber
 
Cassandra - Say Goodbye to the Relational Database (5-6-2010)
Chris Barber
 
Debugging Dojo Applications (2/10/2010)
Chris Barber
 
Titanium Powered Desktop & Mobile Apps (11/21/2009)
Chris Barber
 
Dojo - Javascript's Swiss Army Knife (7/15/2009)
Chris Barber
 
High Availability With DRBD & Heartbeat
Chris Barber
 
2008 MySQL Conference Recap
Chris Barber
 
Memcached And MySQL
Chris Barber
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Future-Proof or Fall Behind? 10 Tech Trends You Can’t Afford to Ignore in 2025
DIGITALCONFEX
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 

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

Editor's Notes