Replication
8
The basic idea behind replication is to reect the contents of one database server (this
can include all databases, only some of them, or even just a few tables) to more than one
instance. Usually, those instances will be running on separate machines, even though this is
not technically necessary.
Traditionally, MySQL replication is based on the surprisingly simple idea of repeating the
execution of all statements issued that can modify data—not SELECT—against a single master
machine on other machines as well. Provided all secondary slave machines had identical data
contents when the replication process began, they should automatically remain in sync. This
is called Statement Based Replication (SBR).
With MySQL 5.1, Row Based Replication (RBR) was added as an alternative method for
replication, targeting some of the deciencies SBR brings with it. While at rst glance it may
seem superior (and more reliable), it is not a silver bullet—the pain points of RBR are simply
different from those of SBR.
Even though there are certain use cases for RBR, all recipes in this chapter will be using
Statement Based Replication.
While MySQL makes replication generally easy to use, it is still important to understand
what happens internally to be able to know the limitations and consequences of the actions
and decisions you will have to make. We assume you already have a basic understanding of
replication in general, but we will still go into a few important details.
Statement Based Replication
SBR is based on a simple but effective principle: if two or more machines have the same set
of data to begin with, they will remain identical if all of them execute the exact same SQL
statements in the same order.
Executing all statements manually on multiple machines would be extremely tedious and
impractical. SBR automates this process. In simple terms, it takes care of sending all the
SQL statements that change data on one server (the master) to any number of additional
instances (the slaves) over the network.
The slaves receiving this stream of modication statements execute them automatically,
thereby effectively reproducing the changes the master machine made to its data originally.
That way they will keep their local data les in sync with the master's.
One thing worth noting here is that the network connection between the master and its
slave(s) need not be permanent. In case the link between a slave and its master fails, the
slave will remember up to which point it had read the data last time and will continue from
there once the network becomes available again.
Download at Wow! eBook
WWW.WOWEBOOK.COM