Top Banner
Highly Available PHP/MySQL Applications with mysqlnd Jervin Real PHP-UGPH, June 2014
73

Highly Available MySQL/PHP Applications with mysqlnd

Jan 15, 2015

Download

Software

dotmanila

PHP Users Group Philippines July Meetup
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Highly Available MySQL/PHP Applications with mysqlnd

Highly Available PHP/MySQL Applications with mysqlnd

Jervin Real PHP-UGPH, June 2014

Page 2: Highly Available MySQL/PHP Applications with mysqlnd

Highly Available Applications

• … how much your application is able to serve its end users within a time period

Page 3: Highly Available MySQL/PHP Applications with mysqlnd

Highly Available Applications

• … how much your application is able to serve its end users within a time period

• Usually referred to as 99[..]% uptime

Page 4: Highly Available MySQL/PHP Applications with mysqlnd

Highly Available Applications

• … how much your application is able to serve its end users within a time period

• Usually referred to as 99[..]% uptime

• It actually depends on whose perspective

Page 5: Highly Available MySQL/PHP Applications with mysqlnd

Highly Available Applications

Keep PHP<>MySQL downtime to a minimum

Page 6: Highly Available MySQL/PHP Applications with mysqlnd

WTF are you saying? I’m a “devloper"!

No no no no no no!

Page 7: Highly Available MySQL/PHP Applications with mysqlnd

Lack of knowledge

Page 8: Highly Available MySQL/PHP Applications with mysqlnd

Limited resources

Page 9: Highly Available MySQL/PHP Applications with mysqlnd

How?

Page 10: Highly Available MySQL/PHP Applications with mysqlnd

MySQL

Page 11: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

Page 12: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

• MySQL Native Replication (Multi-Master)

Page 13: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

• MySQL Native Replication (Multi-Master)

• MySQL NDB Cluster

Page 14: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

• MySQL Native Replication (Multi-Master)

• MySQL NDB Cluster

• Galera based

Page 15: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

• MySQL Native Replication (Multi-Master)

• MySQL NDB Cluster

• Galera based • Percona XtraDB Cluster • MariaDB Galera Cluster • MySQL+Galera

Page 16: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

• MySQL Native Replication (Multi-Master)

• MySQL NDB Cluster

• Galera based • Percona XtraDB Cluster • MariaDB Galera Cluster • MySQL+Galera

• Tungsten Replicator

Page 17: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA

• MySQL Native Replication (Single Primary)

• MySQL Native Replication (Multi-Master)

• MySQL NDB Cluster

• Galera based • Percona XtraDB Cluster • MariaDB Galera Cluster • MySQL+Galera

• Tungsten Replicator

• … and many more

Page 18: Highly Available MySQL/PHP Applications with mysqlnd

• MySQL Master HA

MySQL HA (External Support)

Page 19: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA (External Support)

• MySQL Master HA

• Percona Replication Manager (Pacemaker)

Page 20: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA (External Support)

• MySQL Master HA

• Percona Replication Manager (Pacemaker)

• Haproxy

Page 21: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA (External Support)

• MySQL Master HA

• Percona Replication Manager (Pacemaker)

• Haproxy

• Load Balancers

Page 22: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA (External Support)

• MySQL Master HA

• Percona Replication Manager (Pacemaker)

• Haproxy

• Load Balancers

• MySQL Multi Master

Page 23: Highly Available MySQL/PHP Applications with mysqlnd

MySQL HA (External Support)

• MySQL Master HA

• Percona Replication Manager (Pacemaker)

• Haproxy

• Load Balancers

• MySQL Multi Master

Page 24: Highly Available MySQL/PHP Applications with mysqlnd

PHP (Application)

Page 25: Highly Available MySQL/PHP Applications with mysqlnd

HA via Application

• Traditionally complex to implement

• Unnecessary overhead

• No single point of failure

Page 26: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

Page 27: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

Page 28: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

• Written C (as all extensions should be!)

Page 29: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

• Written C (as all extensions should be!)

• MySQL Client Libraries no more

Page 30: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

• Written C (as all extensions should be!)

• MySQL Client Libraries no more

• Smaller memory footprint in many cases

Page 31: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

• Written C (as all extensions should be!)

• MySQL Client Libraries no more

• Smaller memory footprint in many cases

• Performance statistics

Page 32: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

• Written C (as all extensions should be!)

• MySQL Client Libraries no more

• Smaller memory footprint in many cases

• Performance statistics

• Compression

Page 33: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd

• MySQL Native Driver

• Written C (as all extensions should be!)

• MySQL Client Libraries no more

• Smaller memory footprint in many cases

• Performance statistics

• Compression

• Plugins

Page 34: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd Plugins

• mysqlnd_qc - query result cache

Page 35: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd Plugins

• mysqlnd_qc - query result cache

• mysqlnd_mux - connection multiplexing

Page 36: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd Plugins

• mysqlnd_qc - query result cache

• mysqlnd_mux - connection multiplexing

• mysqlnd_uh - user handler

Page 37: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd Plugins

• mysqlnd_qc - query result cache

• mysqlnd_mux - connection multiplexing

• mysqlnd_uh - user handler

• mysqlnd_memcache - Memcache (MySQL interface)

Page 38: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd Plugins

• mysqlnd_qc - query result cache

• mysqlnd_mux - connection multiplexing

• mysqlnd_uh - user handler

• mysqlnd_memcache - Memcache (MySQL interface)

• mysqlnd_ms - replication and load balancing

Page 39: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd_ms

Page 40: Highly Available MySQL/PHP Applications with mysqlnd

mysqlnd_ms Supported Clusters

• All previously mentioned with few limitations

Page 41: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Slave)

• Master is a single point of failure

• Need external help for failover

• . . or, shoot the other node in the foot, this is still HA!

Page 42: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Slave)

Page 43: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Slave)

Page 44: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Slave)

Page 45: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Master)

• Risky, even if you try segregating writes per master you can still lose data

Page 46: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Master)

db1

db1

Page 47: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Master)

db1

db1

Page 48: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Master)

db2

db1

Page 49: Highly Available MySQL/PHP Applications with mysqlnd

Limitations (Master-Master)

db1 + db2

• Not good if sync_binlog = 0 AND/OR innodb_flush_log_at_trx_commit <> 1

• Not really recommended in general, use appropriate technologies instead

Page 50: Highly Available MySQL/PHP Applications with mysqlnd

Some More Limitations

• Lazy connections is on by default

• Each request will create its own connections when needed, persistent connections may be required

• https://bugs.php.net/bug.php?id=67564

• https://bugs.php.net/bug.php?id=67565

• https://bugs.php.net/bug.php?id=67574

• Be careful with multi-statements and read-write splitting, they are likely to be executed on slave!

SELECT  *  FROM  tbl;  DELETE  FROM  tbl;

Page 51: Highly Available MySQL/PHP Applications with mysqlnd

• Single Primary - one writable node

• Multiple Primary - all nodes writable

mysqlnd_ms Supported Clusters

Page 52: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Slave

mysqlnd_ms Supported Clusters

Page 53: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Slave

• Declare 2 clusters

mysqlnd_ms Supported Clusters

Page 54: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Slave

• Declare 2 clusters

• When primary fails, mark it permanently dead and switch to secondary

mysqlnd_ms Supported Clusters

Page 55: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Slave

• Declare 2 clusters

• When primary fails, mark it permanently dead and switch to secondary

{      "primary":  {          "master":  {              "master_1":  {                  "host":  "127.0.0.1",                  "port":  33001              }          },          "slave":  {              "slave_0":  {                  "host":  "127.0.0.1",                  "port":  33002              }          }      },      "standby":  {          "master":  {              "master_1":  {                  "host":  "127.0.0.1",                  "port":  33002              }          },          "slave":  {          }      }  }

mysqlnd_ms Supported Clusters

Page 56: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Slave

• Declare 2 clusters

• When primary fails, mark it permanently dead and switch to secondary

• Works better with master-master!

{      "primary":  {          "master":  {              "master_1":  {                  "host":  "127.0.0.1",                  "port":  33001              }          },          "slave":  {              "slave_0":  {                  "host":  "127.0.0.1",                  "port":  33002              }          }      },      "standby":  {          "master":  {              "master_1":  {                  "host":  "127.0.0.1",                  "port":  33002              }          },          "slave":  {          }      }  }

mysqlnd_ms Supported Clusters

Page 57: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Slave Demo

• https://github.com/dotmanila/demo-me/phpugph201407/

• master-slave.ini

• mysqlnd_ms_ms.ini

• master-slave.php

mysqlnd_ms Supported Clusters

Page 58: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

• Declare all nodes as masters

mysqlnd_ms Supported Clusters

Page 59: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

mysqlnd_ms Supported Clusters

Page 60: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

• Declare all nodes as masters

mysqlnd_ms Supported Clusters

 {        "primary":  {            "master":  {                "master_1":  {                    "host":  "192.168.56.44",                    "port":  "3306"                },                "master_2":  {                    "host":  "192.168.56.43",                    "port":  "3306"                },                "master_3":  {                    "host":  "192.168.56.42",                    "port":  "3306"                }            },            "slave":  {  },            "filters":  {  "random":  [  ]  },            "failover":  {                  "strategy":  "loop_before_master",                  "remember_failed":  true              }        }    }

Page 61: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

mysqlnd_ms Supported Clusters

 {        "primary":  {            "master":  {                "master_1":  {                    "host":  "192.168.56.44",                    "port":  "3306"                },                "master_2":  {                    "host":  "192.168.56.43",                    "port":  "3306"                },                "master_3":  {                    "host":  "192.168.56.42",                    "port":  "3306"                }            },            "slave":  {  },            "filters":  {  "random":  [  ]  },            "failover":  {                  "strategy":  "loop_before_master",                  "remember_failed":  true              }        }    }

Page 62: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

• Declare all nodes as masters

• roundrobin - single node writer (first node in config)

mysqlnd_ms Supported Clusters

 {        "primary":  {            "master":  {                "master_1":  {                    "host":  "192.168.56.44",                    "port":  "3306"                },                "master_2":  {                    "host":  "192.168.56.43",                    "port":  "3306"                },                "master_3":  {                    "host":  "192.168.56.42",                    "port":  "3306"                }            },            "slave":  {  },            "filters":  {  "random":  [  ]  },            "failover":  {                  "strategy":  "loop_before_master",                  "remember_failed":  true              }        }    }

Page 63: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

• Declare all nodes as masters

• roundrobin - single node writer (first node in config)

• random - all nodes

mysqlnd_ms Supported Clusters

 {        "primary":  {            "master":  {                "master_1":  {                    "host":  "192.168.56.44",                    "port":  "3306"                },                "master_2":  {                    "host":  "192.168.56.43",                    "port":  "3306"                },                "master_3":  {                    "host":  "192.168.56.42",                    "port":  "3306"                }            },            "slave":  {  },            "filters":  {  "random":  [  ]  },            "failover":  {                  "strategy":  "loop_before_master",                  "remember_failed":  true              }        }    }

Page 64: Highly Available MySQL/PHP Applications with mysqlnd

• Multi-Masters (NDB, Galera)

• Declare all nodes as masters

• roundrobin - single node writer (first node in config)

• random - all nodes

• remember_failed is not what it says it is

 {        "primary":  {            "master":  {                "master_1":  {                    "host":  "192.168.56.44",                    "port":  "3306"                },                "master_2":  {                    "host":  "192.168.56.43",                    "port":  "3306"                },                "master_3":  {                    "host":  "192.168.56.42",                    "port":  "3306"                }            },            "slave":  {  },            "filters":  {  "random":  [  ]  },            "failover":  {                  "strategy":  "loop_before_master",                  "remember_failed":  true              }        }    }

mysqlnd_ms Supported Clusters

Page 65: Highly Available MySQL/PHP Applications with mysqlnd

• Master-Master Demo

• https://github.com/dotmanila/demo-me/phpugph201407/

• master-master.ini

• mysqlnd_ms_mm.ini

• master-master.php

mysqlnd_ms Supported Clusters

Page 66: Highly Available MySQL/PHP Applications with mysqlnd

Am I Using mysqlnd? (rpm)

[revin@forge  ~]$  rpm  -­‐q  php-­‐mysql.x86_64  -­‐-­‐requires  […]  libmysqlclient_r.so.16()(64bit)  libmysqlclient_r.so.16(libmysqlclient_16)(64bit)  libmysqlclient.so.16()(64bit)  libmysqlclient.so.16(libmysqlclient_16)(64bit)  […]

Page 67: Highly Available MySQL/PHP Applications with mysqlnd

[revin@forge  ~]$  rpm  -­‐q  php-­‐mysqlnd.x86_64  -­‐-­‐requires  php-­‐pdo(x86-­‐64)  =  5.4.30-­‐36.el6.art  rpmlib(VersionedDependencies)  <=  3.0.3-­‐1  rpmlib(FileDigests)  <=  4.6.0-­‐1  rpmlib(PayloadFilesHavePrefix)  <=  4.0-­‐1  rpmlib(CompressedFileNames)  <=  3.0.4-­‐1  libc.so.6()(64bit)  libc.so.6(GLIBC_2.2.5)(64bit)  libc.so.6(GLIBC_2.3.4)(64bit)  libc.so.6(GLIBC_2.4)(64bit)  libpthread.so.0()(64bit)  libpthread.so.0(GLIBC_2.2.5)(64bit)  rtld(GNU_HASH)  rpmlib(PayloadIsXz)  <=  5.2-­‐1

Am I Using mysqlnd? (rpm)

Page 68: Highly Available MySQL/PHP Applications with mysqlnd

[revin@forge  ~]$  php  -­‐i  […]  !mysql  !MySQL  Support  =>  enabled  Active  Persistent  Links  =>  0  Active  Links  =>  0  Client  API  version  =>  5.1.59  MYSQL_MODULE_TYPE  =>  external  MYSQL_SOCKET  =>  /var/lib/mysql/mysql.sock  MYSQL_INCLUDE  =>  -­‐I/usr/include/mysql  MYSQL_LIBS  =>  -­‐L/usr/lib64/mysql  -­‐lmysqlclient

Am I Using mysqlnd? (phpinfo)

Page 69: Highly Available MySQL/PHP Applications with mysqlnd

[revin@forge  ~]$  php  -­‐i  […]  !mysqlnd  !mysqlnd  =>  enabled  Version  =>  mysqlnd  5.0.10  -­‐  20111026  -­‐  $Id:  c85105d7c6f7d70d609bb4c000257868a40840ab  $  Compression  =>  supported  SSL  =>  supported  Command  buffer  size  =>  4096  Read  buffer  size  =>  32768  Read  timeout  =>  31536000  Collecting  statistics  =>  Yes  Collecting  memory  statistics  =>  No  Tracing  =>  n/a  Loaded  plugins  =>  mysqlnd,example,debug_trace,     auth_plugin_mysql_native_password,     auth_plugin_mysql_clear_password  API  Extensions  =>  mysql,mysqli,pdo_mysql

Am I Using mysqlnd? (phpinfo)

Page 70: Highly Available MySQL/PHP Applications with mysqlnd

Conclusion

• Yes, we can achieve HA with mysqlnd_ms

• Not for the faint of heart

• Do not rely on for HA (writes) on critical production systems

• Good for intended use case, rw-splitting

Page 71: Highly Available MySQL/PHP Applications with mysqlnd
Page 72: Highly Available MySQL/PHP Applications with mysqlnd

Q&A

• http://dotmanila.com/blog/ • http://www.mysqlperformanceblog.com/ • @dotmanila

Percona is Hiring!!http://www.percona.com/about-us/careers/open-positions

Page 73: Highly Available MySQL/PHP Applications with mysqlnd

$t  =  true  AND  false;  echo  (int)  $t;