This is the second part of my series on High Availability with mysqlnd_ms. In my first post, “Simple MySQL Master HA with mysqlnd_ms,” I showed a simple HA solution using asynchronous MySQL replication. This time we will see how to leverage an all-primary cluster where you can write to all nodes. In this post I used Percona XtraDB Cluster, but you should also be able to do the same with MySQL NDB Cluster or Tungsten Replicator.
To start with, here is the mysqlnd_ms configuration I used: mysqlnd_ms_mm.ini . All of these files are available from my Github repository. Below, I have three Percona XtraDB Cluster nodes, all defined as masters and no slaves. I’ve configured a roundrobin filter where all connections will happen on the first node, in this case 192.168.56.44 . In case the first node fails, the second node will be used and so forth until no more nodes are available. Another interesting configuration option here is the loop_before_master strategy whereby if connection or a statement to the current server fails, it will be retried silently on the remaining nodes before returning an error to the user, more on this below.
|
1 |
{<br> "primary": {<br> "master": {<br> "master_1": {<br> "host": "192.168.56.44",<br> "port": "3306"<br> },<br> "master_2": {<br> "host": "192.168.56.43",<br> "port": "3306"<br> },<br> "master_3": {<br> "host": "192.168.56.42",<br> "port": "3306"<br> }<br> },<br> "slave": { },<br> "filters": { "roundrobin": [ ] },<br> "failover": { "strategy": "loop_before_master", "remember_failed": true }<br> }<br> } |
Similar to my previous post, I also used a custom INI file for PHP to use, this time aptly named master-master.ini :
|
1 |
mysqlnd_ms.enable = 1<br>mysqlnd_ms.disable_rw_split = 1<br>mysqlnd_ms.multi_master = 1<br>mysqlnd_ms.force_config_usage = 1<br>mysqlnd_ms.config_file = /home/revin/git/demo-me/phpugph201407/mysqlnd_ms_mm.ini |
A new addition to this configuration is mysqlnd_ms.multi_master , when enabled it would allow you to use all nodes or just one and treat the others as passive. The PHP script I used this time is called master-master.php , it is largely similar to master-slave-ng.php with a few differences:
So here is a quick test, first with roundrobin filter, after 4 INSERTs, I shutdown 192.168.56.44 which sends my connection to the next server in the configuration, 192.168.56.43 . When I started back 192.168.56.44 again, the script resumed connections there. Pretty cool right?
|
1 |
[revin@forge phpugph201407]$ php -c master-master.ini master-master.php<br>Last value 3564 from host 192.168.56.44 via TCP/IP and thread id 19<br>Last value 3565 from host 192.168.56.44 via TCP/IP and thread id 20<br>Last value 3566 from host 192.168.56.44 via TCP/IP and thread id 21<br>Last value 3567 from host 192.168.56.44 via TCP/IP and thread id 22<br>Warning: connect_mysql(): MySQL server has gone away in /home/revin/git/demo-me/phpugph201407/master-master.php on line 63<br>Warning: connect_mysql(): Error while reading greeting packet. PID=23464 in /home/revin/git/demo-me/phpugph201407/master-master.php on line 63<br>ERRROR: 192.168.56.43 via TCP/IP [2006] MySQL server has gone away on line 30<br>Last value 0 from host and thread id 0<br>Last value 3568 from host 192.168.56.43 via TCP/IP and thread id 1552<br>Last value 3569 from host 192.168.56.43 via TCP/IP and thread id 1553<br>[...]<br>Last value 3584 from host 192.168.56.43 via TCP/IP and thread id 1568<br>Last value 3585 from host 192.168.56.44 via TCP/IP and thread id 18 |
Here’s another test using the random filter which allows you to write to all nodes, on my mysqlnd_ms_mm.ini above, I just changed roundrobin to random . As you can see, all three nodes were being used, of course in random, at the same time you will also see when I shutdown 192.168.56.44 around where the connect_mysql errors and then the server was used again near the bottom after a started it back up. Still pretty cool right?
|
1 |
[revin@forge phpugph201407]$ php -c master-master.ini master-master.php<br>Last value 3590 from host 192.168.56.42 via TCP/IP and thread id 2060<br>Last value 3591 from host 192.168.56.43 via TCP/IP and thread id 1569<br>Last value 3592 from host 192.168.56.43 via TCP/IP and thread id 1570<br>Warning: connect_mysql(): MySQL server has gone away in /home/revin/git/demo-me/phpugph201407/master-master.php on line 63<br>Warning: connect_mysql(): Error while reading greeting packet. PID=23919 in /home/revin/git/demo-me/phpugph201407/master-master.php on line 63<br>ERRROR: 192.168.56.43 via TCP/IP [2006] MySQL server has gone away on line 30<br>Last value 0 from host and thread id 0<br>Last value 3593 from host 192.168.56.42 via TCP/IP and thread id 2061<br>Last value 3594 from host 192.168.56.42 via TCP/IP and thread id 2062<br>Last value 3595 from host 192.168.56.42 via TCP/IP and thread id 2063<br>Last value 3596 from host 192.168.56.42 via TCP/IP and thread id 2064<br>Last value 3597 from host 192.168.56.43 via TCP/IP and thread id 1576<br>Last value 3598 from host 192.168.56.43 via TCP/IP and thread id 1577<br>Last value 3599 from host 192.168.56.43 via TCP/IP and thread id 1578<br>Last value 3600 from host 192.168.56.43 via TCP/IP and thread id 1579<br>Last value 3601 from host 192.168.56.42 via TCP/IP and thread id 2065<br>Last value 3602 from host 192.168.56.43 via TCP/IP and thread id 1581<br>Last value 3603 from host 192.168.56.43 via TCP/IP and thread id 1582<br>Last value 3604 from host 192.168.56.42 via TCP/IP and thread id 2066<br>Last value 3605 from host 192.168.56.44 via TCP/IP and thread id 19<br>Last value 3606 from host 192.168.56.43 via TCP/IP and thread id 1583<br>Last value 3607 from host 192.168.56.44 via TCP/IP and thread id 21 |
So here are some issues I’ve observed during these tests:
So we’ve seen these two forms of possibilities and they definitely have use cases and advantages. On the other hand because of the issues we have found so far (I’ve reported 4 bugs on the PHP bugs database during the course of these tests including one crashing), I recommend to make sure you test seriously before putting this on production.
Resources
RELATED POSTS