2. About Me
Chris Barber
Lead Software Engineer @ Appcelerator
CEO/Technology Consultant @ CB1, INC.
JavaScript, PHP, C++
http://www.cb1inc.com
@cb1kenobi on Twitter, Slideshare
4. 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 :(
6. What is mysqlnd_ms?
A PHP Extension that provides transparent MySQL
load balancing across master and slave servers
7. 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
13. 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
14. 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”);
?>
21. Server Picking Ideas
Geo-load balancing
Routing around server maintenance
Sharding
Dynamically add/override master/slave hostnames
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. 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
24. More Info
Authors: Andrey Hristov, Ulf Wendel, & Johannes
Schlueter
http://blog.ulf-wendel.de
http://svn.php.net/viewvc/pecl/mysqlnd_ms