SlideShare a Scribd company logo
by Rob Kaufman [email_address] MySQL S&M
Slaves and Masters What is Master Slave Replication One database instance (the Master) that does both reads and writes This guy is in charge Should do mostly writing One or more database instances that only do reads (the Slaves) Only know what the Master tells them Never except writes
Master Slave Diagram Master Slave 1 Slave 2 Slave 3 App Server App Server Key: Arrow represents data flow direction
Master Slave: Why What will a Master Slave setup do for you? Redundancy Hot spares of data Any slave can become the master if the master fails Efficiency If you data is read bound Many reads to a few writes
Master Slave: Why Not What draw backs does a Master Slave setup have Not a backup! Your backing up your data... right? If not, go do it now Yes I really mean it... get up and go NOW Won't help the write bound Many web apps are about creating data, not just consuming it
Master Slave: How Create slave user GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mydomain.com' IDENTIFIED BY 'slavepass'; Configure Master Configure Slave Copy The Data Use Innodb
Be the Master Master Configuration my.cnf: [mysqld] log-bin=mysql-bin server-id=1 Watch out for the skip-networking option Make a data backup mysqldump --all-databases --lock-all-tables --master-data >dbdump.db
Be the Slave Slave Setup Don't have to do log-bin, but you should my.cnf: [mysqld] log-bin=mysql-bin server-id=2 master-host = host_ip_address master-user = replication master-password = slavepass master-port = 3306
Whip that Slave Import the data Start slave with --skip-slave option mysql < dbdump.db Start slave with mysql command &quot;start slave;&quot; check with &quot;show slave status;&quot; and &quot;show master status;&quot;
Slaves in Rails How do I do this in Rails Masochism Hey, I didn't pick the name ;-) Plugin by Rick Olsen piston import http://ar-code.svn.engineyard.com/plugins/masochism/ vendor/plugins add master_database entry into your database.yml set production entry to point at slave add to production.rb config.after_initialize do  ActiveReload::ConnectionProxy.setup!  end
Slaves in Rails See Robby Russell for more info http://www.robbyonrails.com/articles/2007/11/15/master-slave-databases-with-ruby-on-rails
Other Slave in Rails Options Active Delegate Multiple Database Connections in Ruby on Rails From Robby Russell (http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails) Likely abandoned magic_multi_connections From Dr Nic (http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/) This gem is also able to handle master/slave setups but also could be used to do data partitioning across multiple databases. Admittedly has issues
Other Slave in Rails Options ActsAsReadonlyable From the Revolution Health team (http://revolutiononrails.blogspot.com/2007/04/plugin-release-actsasreadonlyable.html) DynamicDatabaseChanger Rails Database Loadbalancing http://rubyforge.org/projects/ddcplugin/ May work for multi-master set ups too (http://scriptserver.blogspot.com/2007/06/rails-database-loadbalancing-with.html) Does not do failover if instance goes down
Master Master Replication What is Master Master Replication All Master database instances can both read and write They are set up in a &quot;circle&quot; that passes data from one instance to another Every master is one machines slave Every slave is another machines master Can be mixed with Master Slave Each Master has multiple slaves One slave of each master is the next master in the chain
Master Master Diagram Master 2 Master 1 Master 3 App Server App Server Key: Arrow represents data flow direction App Server
Master Master: Why What will a Master Master setup do for you? Redundancy Hot spares of data If a master fails it can be removed from the loop its app servers can be redirected elsewhere data will stop moving around the circle till it is fixed or removed Efficiency If you are either write or read bound Can scale cleanly up to 10 instances w/ fast connection between servers
Master Master: Why Not What drawbacks does a Master Master setup have Not a backup! Can propagate DROP and DELETE just as fast as it can UPDATE and INSERT Can get behind It is possible that the data might not all be propagated to every instance at a given moment If a db instance goes down If network latency is really high If traffic is higher than the db instance can handle
Master Master: How Create slave user GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mydomain.com' IDENTIFIED BY 'slavepass'; Make a data backup of master1 mysqldump --all-databases --lock-all-tables --master-data >dbdump.db
Be the Master Master 1 configuration my.cnf: [mysqld] log-bin=mysql-bin log-slave-updates replicate-same-server-id  = 0 server-id=1 master-host = master2_ip_address master-user = replication master-password = slavepass master-port = 3306 auto_increment_increment = 10 auto_increment_offset = 1
Be the Other Master Master 2 configuration my.cnf: [mysqld] log-bin=mysql-bin log-slave-updates replicate-same-server-id  = 0 server-id=2 master-host = master1_ip_address master-user = replication master-password = slavepass master-port = 3306 auto_increment_increment = 10 auto_increment_offset = 2
Whip that... Master? Import the data into master2 Start master2 with --skip-slave option mysql < dbdump.db Start master2 as slave with mysql command &quot;start slave;&quot; Get master2 position with mysql command &quot;show master status&quot; Write down the info under File and Position, use for FILE and POSITION below Back on master1 Start master1 with --skip-slave option Set the master position mysql command CHANGE MASTER TO MASTER_LOG_FILE='FILE',  MASTER_LOG_POS=POSITION Start master1 as slave with mysql command &quot;start slave;&quot;
Master Master in Rails Step 1: Setup Master Master Replication for your production enviroment Step 2: Point different clusters of application servers at each different master Step 3: There IS NO STEP 3!
What you want load balancing? We can do load balancing for either Master Slave or Master Master SQLRelay http://sqlrelay.sourceforge.net/documentation.html Stable and well tested solution If one db instance goes down, will server requests to other instances MysqlRelay http://forge.mysql.com/wiki/MySQL_Proxy Kind of the new kid on the block MySQL specific Has other functions besides load balance and failover support
Know When Something is Wrong You need to monitor the replication &quot;show slave status&quot; and &quot;show master status&quot; Munin http://munin.projects.linpro.no/wiki/plugin-mysql_slave_status MMM http://code.google.com/p/mysql-master-master/ PDI
Fix An Error Now what Most of the time what went wrong was the bin log got off by position.  This  happens sometimes when a db instance crashes or powerfails If so you can use mysql command &quot;STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;&quot; Can be greater than 1 if you want, but watch for danger Sometimes (rarely) it will get ahead Only ever seen this on a disk full Can go back to an old position on the relay log &quot;CHANGE MASTER TO RELAY_LOG_FILE='slave-bin.006',  RELAY_LOG_POS=4025;
Thanks Thanks to Robby Russell and Dr Nic They have both done quite a bit of blogging in the Master Slave area Special thanks to RailsMagnet for having links to all the current master-slave plugins for Rails http://railsmagnet.com/2007/12/using-multiple-databases-rails-application MySQL www.mysql.com for its cool Master Master setup Munin  http://munin.projects.linpro.no/ for watching my back

More Related Content

PPTX
201904 websocket
PDF
WordPress Performance & Scalability
PPTX
Word press workflows and gulp
ODP
High Performance Web Sites
PPT
Oreilly Webcast Jan 09, 2009
PDF
Care and feeding notes
PDF
Cloud Automation with Opscode Chef
KEY
Performance Tuning - MuraCon 2012
201904 websocket
WordPress Performance & Scalability
Word press workflows and gulp
High Performance Web Sites
Oreilly Webcast Jan 09, 2009
Care and feeding notes
Cloud Automation with Opscode Chef
Performance Tuning - MuraCon 2012

What's hot (20)

ODP
Choosing a Web Architecture for Perl
PPT
Gearman - Job Queue
PDF
KOWAZA for mackerel
PDF
Faster PHP apps using Queues and Workers
PDF
Queue your work
PDF
Gearman and Perl
PDF
Секретный доклад о React Router - OdessaJS 2014
PDF
Intro to Rack
PPTX
Distributed Applications with Perl & Gearman
PDF
The MetaCPAN VM for Dummies Part One (Installation)
PDF
Scalable talk notes
PDF
Building Scalable Websites with Perl
PDF
How to Upgrade Your Database Plan on Heroku and Rails Setup?
KEY
Enterprise Hosting
PDF
Asynchronous Processing with Ruby on Rails (RailsConf 2008)
PDF
Automated shutdown
PDF
Distributed Queue System using Gearman
PDF
CouchDB: A NoSQL database
PDF
Caching for Cash: Caching
PPTX
Drupal development environment
Choosing a Web Architecture for Perl
Gearman - Job Queue
KOWAZA for mackerel
Faster PHP apps using Queues and Workers
Queue your work
Gearman and Perl
Секретный доклад о React Router - OdessaJS 2014
Intro to Rack
Distributed Applications with Perl & Gearman
The MetaCPAN VM for Dummies Part One (Installation)
Scalable talk notes
Building Scalable Websites with Perl
How to Upgrade Your Database Plan on Heroku and Rails Setup?
Enterprise Hosting
Asynchronous Processing with Ruby on Rails (RailsConf 2008)
Automated shutdown
Distributed Queue System using Gearman
CouchDB: A NoSQL database
Caching for Cash: Caching
Drupal development environment
Ad

Viewers also liked (6)

PPTX
Discovery informaticsstanton
PPTX
Manual r commander By Juan Guarangaa
PPTX
Chapter9 r studio2
PPTX
R-Studio Vs. Rcmdr
PPTX
Installing R and R-Studio
PPTX
Why R? A Brief Introduction to the Open Source Statistics Platform
Discovery informaticsstanton
Manual r commander By Juan Guarangaa
Chapter9 r studio2
R-Studio Vs. Rcmdr
Installing R and R-Studio
Why R? A Brief Introduction to the Open Source Statistics Platform
Ad

Similar to Mysql S&M (20)

PPT
Architecting cloud
PPT
Intro to MySQL Master Slave Replication
ODP
Database Replication
PDF
DB Replication With Rails
ZIP
My sql replication advanced techniques presentation
DOCX
Mater,slave on mysql
PPT
MySQL 5.1 Replication
PDF
Mysql Replication Excerpt 5.1 En
DOCX
Master master vs master-slave database
ODP
Mysql
ODP
MySQL 101 PHPTek 2017
PPT
Download presentation
PDF
High Availability with MySQL
PPT
Basic Knowledge on MySql Replication
PDF
Has MySQL grown up?
PDF
MySQL database replication
PDF
My S Q L Replication Getting The Most From Slaves
PDF
Replication tutorial presentation
PDF
Advanced MySQL Replication Architectures - Luis Soares
PPTX
MySqL Failover by Weatherly Cloud Computing USA
Architecting cloud
Intro to MySQL Master Slave Replication
Database Replication
DB Replication With Rails
My sql replication advanced techniques presentation
Mater,slave on mysql
MySQL 5.1 Replication
Mysql Replication Excerpt 5.1 En
Master master vs master-slave database
Mysql
MySQL 101 PHPTek 2017
Download presentation
High Availability with MySQL
Basic Knowledge on MySql Replication
Has MySQL grown up?
MySQL database replication
My S Q L Replication Getting The Most From Slaves
Replication tutorial presentation
Advanced MySQL Replication Architectures - Luis Soares
MySqL Failover by Weatherly Cloud Computing USA

More from Rob Kaufman (9)

PDF
How to Get Your Idea Built
PDF
The Art of Negotiation
ODP
Ruby Debug
PDF
Developer Flow
ODP
Learned from Woodworking
ODP
Testing Philosphies
ODP
Tanning Bed
ODP
Ruby 1.9 Or Bust Presentation
ODP
Action Mailer In Action
How to Get Your Idea Built
The Art of Negotiation
Ruby Debug
Developer Flow
Learned from Woodworking
Testing Philosphies
Tanning Bed
Ruby 1.9 Or Bust Presentation
Action Mailer In Action

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
KodekX | Application Modernization Development
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Cloud computing and distributed systems.
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Agricultural_Statistics_at_a_Glance_2022_0.pdf
20250228 LYD VKU AI Blended-Learning.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
KodekX | Application Modernization Development
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Cloud computing and distributed systems.
Understanding_Digital_Forensics_Presentation.pptx
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
“AI and Expert System Decision Support & Business Intelligence Systems”
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Reach Out and Touch Someone: Haptics and Empathic Computing

Mysql S&M

  • 1. by Rob Kaufman [email_address] MySQL S&M
  • 2. Slaves and Masters What is Master Slave Replication One database instance (the Master) that does both reads and writes This guy is in charge Should do mostly writing One or more database instances that only do reads (the Slaves) Only know what the Master tells them Never except writes
  • 3. Master Slave Diagram Master Slave 1 Slave 2 Slave 3 App Server App Server Key: Arrow represents data flow direction
  • 4. Master Slave: Why What will a Master Slave setup do for you? Redundancy Hot spares of data Any slave can become the master if the master fails Efficiency If you data is read bound Many reads to a few writes
  • 5. Master Slave: Why Not What draw backs does a Master Slave setup have Not a backup! Your backing up your data... right? If not, go do it now Yes I really mean it... get up and go NOW Won't help the write bound Many web apps are about creating data, not just consuming it
  • 6. Master Slave: How Create slave user GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mydomain.com' IDENTIFIED BY 'slavepass'; Configure Master Configure Slave Copy The Data Use Innodb
  • 7. Be the Master Master Configuration my.cnf: [mysqld] log-bin=mysql-bin server-id=1 Watch out for the skip-networking option Make a data backup mysqldump --all-databases --lock-all-tables --master-data >dbdump.db
  • 8. Be the Slave Slave Setup Don't have to do log-bin, but you should my.cnf: [mysqld] log-bin=mysql-bin server-id=2 master-host = host_ip_address master-user = replication master-password = slavepass master-port = 3306
  • 9. Whip that Slave Import the data Start slave with --skip-slave option mysql < dbdump.db Start slave with mysql command &quot;start slave;&quot; check with &quot;show slave status;&quot; and &quot;show master status;&quot;
  • 10. Slaves in Rails How do I do this in Rails Masochism Hey, I didn't pick the name ;-) Plugin by Rick Olsen piston import http://ar-code.svn.engineyard.com/plugins/masochism/ vendor/plugins add master_database entry into your database.yml set production entry to point at slave add to production.rb config.after_initialize do ActiveReload::ConnectionProxy.setup! end
  • 11. Slaves in Rails See Robby Russell for more info http://www.robbyonrails.com/articles/2007/11/15/master-slave-databases-with-ruby-on-rails
  • 12. Other Slave in Rails Options Active Delegate Multiple Database Connections in Ruby on Rails From Robby Russell (http://www.robbyonrails.com/articles/2007/10/05/multiple-database-connections-in-ruby-on-rails) Likely abandoned magic_multi_connections From Dr Nic (http://drnicwilliams.com/2007/04/12/magic-multi-connections-a-facility-in-rails-to-talk-to-more-than-one-database-at-a-time/) This gem is also able to handle master/slave setups but also could be used to do data partitioning across multiple databases. Admittedly has issues
  • 13. Other Slave in Rails Options ActsAsReadonlyable From the Revolution Health team (http://revolutiononrails.blogspot.com/2007/04/plugin-release-actsasreadonlyable.html) DynamicDatabaseChanger Rails Database Loadbalancing http://rubyforge.org/projects/ddcplugin/ May work for multi-master set ups too (http://scriptserver.blogspot.com/2007/06/rails-database-loadbalancing-with.html) Does not do failover if instance goes down
  • 14. Master Master Replication What is Master Master Replication All Master database instances can both read and write They are set up in a &quot;circle&quot; that passes data from one instance to another Every master is one machines slave Every slave is another machines master Can be mixed with Master Slave Each Master has multiple slaves One slave of each master is the next master in the chain
  • 15. Master Master Diagram Master 2 Master 1 Master 3 App Server App Server Key: Arrow represents data flow direction App Server
  • 16. Master Master: Why What will a Master Master setup do for you? Redundancy Hot spares of data If a master fails it can be removed from the loop its app servers can be redirected elsewhere data will stop moving around the circle till it is fixed or removed Efficiency If you are either write or read bound Can scale cleanly up to 10 instances w/ fast connection between servers
  • 17. Master Master: Why Not What drawbacks does a Master Master setup have Not a backup! Can propagate DROP and DELETE just as fast as it can UPDATE and INSERT Can get behind It is possible that the data might not all be propagated to every instance at a given moment If a db instance goes down If network latency is really high If traffic is higher than the db instance can handle
  • 18. Master Master: How Create slave user GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%.mydomain.com' IDENTIFIED BY 'slavepass'; Make a data backup of master1 mysqldump --all-databases --lock-all-tables --master-data >dbdump.db
  • 19. Be the Master Master 1 configuration my.cnf: [mysqld] log-bin=mysql-bin log-slave-updates replicate-same-server-id = 0 server-id=1 master-host = master2_ip_address master-user = replication master-password = slavepass master-port = 3306 auto_increment_increment = 10 auto_increment_offset = 1
  • 20. Be the Other Master Master 2 configuration my.cnf: [mysqld] log-bin=mysql-bin log-slave-updates replicate-same-server-id = 0 server-id=2 master-host = master1_ip_address master-user = replication master-password = slavepass master-port = 3306 auto_increment_increment = 10 auto_increment_offset = 2
  • 21. Whip that... Master? Import the data into master2 Start master2 with --skip-slave option mysql < dbdump.db Start master2 as slave with mysql command &quot;start slave;&quot; Get master2 position with mysql command &quot;show master status&quot; Write down the info under File and Position, use for FILE and POSITION below Back on master1 Start master1 with --skip-slave option Set the master position mysql command CHANGE MASTER TO MASTER_LOG_FILE='FILE', MASTER_LOG_POS=POSITION Start master1 as slave with mysql command &quot;start slave;&quot;
  • 22. Master Master in Rails Step 1: Setup Master Master Replication for your production enviroment Step 2: Point different clusters of application servers at each different master Step 3: There IS NO STEP 3!
  • 23. What you want load balancing? We can do load balancing for either Master Slave or Master Master SQLRelay http://sqlrelay.sourceforge.net/documentation.html Stable and well tested solution If one db instance goes down, will server requests to other instances MysqlRelay http://forge.mysql.com/wiki/MySQL_Proxy Kind of the new kid on the block MySQL specific Has other functions besides load balance and failover support
  • 24. Know When Something is Wrong You need to monitor the replication &quot;show slave status&quot; and &quot;show master status&quot; Munin http://munin.projects.linpro.no/wiki/plugin-mysql_slave_status MMM http://code.google.com/p/mysql-master-master/ PDI
  • 25. Fix An Error Now what Most of the time what went wrong was the bin log got off by position. This happens sometimes when a db instance crashes or powerfails If so you can use mysql command &quot;STOP SLAVE; SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;&quot; Can be greater than 1 if you want, but watch for danger Sometimes (rarely) it will get ahead Only ever seen this on a disk full Can go back to an old position on the relay log &quot;CHANGE MASTER TO RELAY_LOG_FILE='slave-bin.006', RELAY_LOG_POS=4025;
  • 26. Thanks Thanks to Robby Russell and Dr Nic They have both done quite a bit of blogging in the Master Slave area Special thanks to RailsMagnet for having links to all the current master-slave plugins for Rails http://railsmagnet.com/2007/12/using-multiple-databases-rails-application MySQL www.mysql.com for its cool Master Master setup Munin http://munin.projects.linpro.no/ for watching my back