Mysql
Replication
Replication
enables data from one MySQL database server (the master) to be replicated to
one or more MySQL database servers (the slaves). Replication is asynchronous
- slaves need not be connected permanently to receive updates from the
master. This means that updates can occur over long-distance connections and
even over temporary or intermittent connections such as a dial-up service.
Depending on the configuration, you can replicate all databases, selected
databases, or even selected tables within a database.
Uses for
replication in MySQL include:
|
Setting
the Replication Master Configuration:
On a replication master, you must enable binary
logging and establish a unique server ID. Binary logging must be
enabled on the master because the binary log is the basis for sending data changes
from the master to its slaves. If binary logging is not enabled, replication
will not be possible. Each server
within a replication group must be configured with a unique server ID. This
ID is used to identify individual servers within the group, and must be a
positive integer between 1 and (232)–1.
To configure the binary log and server
ID options, you will need to shut down your MySQL server and edit themy.cnf or my.ini file.
Add the following options to the configuration file within the [mysqld] section.
For example, to enable binary logging
using a log file name prefix of mysql-bin, and configure a server
ID of 1, use these lines:
After making the changes, restart the
server.
|
Setting the Replication Slave
Configuration:
On a
replication slave, you must establish a unique server ID.
After making
the changes, restart the server. If you are setting up multiple slaves, each
one must have a unique server-id value
that differs from that of the master and from each of the other slaves.
|
Creating a User for Replication:
Each slave
must connect to the master using a MySQL user name and password, so there
must be a user account on the master that the slave can use to connect. Any
account can be used for this operation, providing it has been granted the REPLICATION
SLAVE privilege. You may wish to create a different account for
each slave, or connect to the master using the same account for each slave.
|
Obtaining the Replication Master Binary
Log Coordinates:
To configure
replication on the slave you must determine the master's current coordinates
within its binary log. You will need this information so that when the slave
starts the replication process, it is able to start processing events from
the binary log at the correct point. If
you have existing data on your master that you want to synchronize on your
slaves before starting the replication process, you must stop processing
statements on the master, and then obtain its current binary log coordinates
and dump its data, before permitting the master to continue executing
statements. If you do not stop the execution of statements, the data dump and
the master status information that you use will not match and you will end up
with inconsistent or corrupted databases on the slaves.
To obtain the
master binary log coordinates, follow these steps:
1. Start a session on the master by connecting to it with the command-line
client, and flush all tables and block write statements by executing the FLUSH TABLES WITH READ LOCK statement:
2. In a different session on the master, use the SHOW MASTER STATUS statement
to determine the current binary log file name and position:
The File column shows the name of
the log file and Position shows the position within the file. You need them later when you are
setting up the slave. They represent the replication coordinates at which the
slave should begin processing new updates from the master. If the master has
been running previously without binary logging enabled, the log file name and
position values displayed by SHOW MASTER STATUS or mysqldump
--master-data will be empty. In that case, the values that you
need to use later when specifying the slave's log file and position are the
empty string ('') and 4.
|
Creating a Data Snapshot Using mysqldump:
One way to create a snapshot of the data in an
existing master database is to use the mysqldump tool.
Once the data dump has been completed, you then import this data into the
slave before starting the replication process.
To obtain a snapshot of the data using mysqldump:
1.
If you have not already locked the tables on the server to prevent
statements that update data from executing:
Start a
session on the server by connecting to it with the command-line client, and
flush all tables and block write statements by executing the FLUSH TABLES WITH READ LOCK statement:
Remember to use SHOW MASTER STATUS and
record the binary log details for use when starting up the slave. The point
in time of your snapshot and the binary log position must match.
2.
In another session, use mysqldump to create a dump either of all the databases you want to
replicate, or of selected individual databases. For example:
An alternative to using a bare dump, is to use the --master-data option,
which automatically appends theCHANGE MASTER TO statement
required on the slave to start the replication process.
shell> mysqldump --all-databases --master-data >dbdump.db
3.
In the client where you acquired the read lock, release the lock:
Alternatively, exit the first session to release
the read lock.
|
Setting Up Replication with New Master
and Slaves:
The easiest
and most straightforward method for setting up replication is to use new
master and slave servers.
To set up
replication between a new master and slave:
1. Configure the MySQL
master with the necessary configuration properties.
2. Start up the MySQL
master.
3. Set up a user.
4. Obtain the master
status information.
5. On the master,
release the read lock:
mysql>
UNLOCK TABLES;
6. On the slave, edit
the MySQL configuration.
7. Start up the MySQL
slave.
Perform the
slave setup steps on each slave. Because there is no data to load or exchange
on a new server configuration you do not need to copy or import any
information.
If you are setting up
a new replication environment using the data from a different existing
database server, you will now need to run the dump file generated from that
server on the new master. The database updates will automatically be
propagated to the slaves:
shell> mysql -h
master < fulldb.dump
|
Setting Up Replication with Existing
Data:
When setting up
replication with existing data, you will need to decide how best to get the
data from the master to the slave before starting the replication service.
The basic
process for setting up replication with existing data is as follows:
1. With the MySQL master
running, create a user to be used by the slave when connecting to the master
during replication.
2. If you have not
already configured the server-id and
enabled binary logging on the master server, you will need to shut it down to
configure these options.
If you have to shut down your master server, this
is a good opportunity to take a snapshot of its databases. You should obtain
the master status before taking down the master, updating the configuration
and taking a snapshot.
3. If your master server
is already correctly configured, obtain its status and then use mysqldump to
take a snapshot or take a raw snapshot of the live server.
4. Update the
configuration of the slave.
5. The next step depends
on how you created the snapshot of data on the master.
If you used mysqldump:
b.
Import the dump file:
shell> mysql < fulldb.dump
If you
created a snapshot using the raw data files:
shell> tar xvf dbdump.tar
You may need to set permissions and ownership on
the files so that the slave server can access and modify them.
6. Configure the slave
with the replication coordinates from the master. This tells the slave the
binary log file and position within the file where replication needs to
start. Also, configure the slave with the login credentials and host name of
the master.
7. Start the slave
threads:
mysql> START
SLAVE;
After you
have performed this procedure, the slave should connect to the master and
catch up on any updates that have occurred since the snapshot was taken.
|
Introducing Additional Slaves to
an Existing Replication Environment:
To add
another slave to an existing replication configuration, you can do so without
stopping the master. Instead, set up the new slave by making a copy of an
existing slave, except that you configure the new slave with a differentserver-id value.
To duplicate an
existing slave:
1.
Shut down the existing slave:
shell>
mysqladmin shutdown
2.
Copy the data directory from the existing slave to the new slave. You
can do this by creating an archive using taror WinZip,
or by performing a direct copy using a tool such as cp or rsync.
Ensure that you also copy the log files and relay log files.
3.
Copy the master.info and relay-log.info files from
the existing slave to the new slave if they were not located in the data
directory. These files hold the current log coordinates for the master's
binary log and the slave's relay log.
4.
Start the existing slave.
5.
On the new slave, edit the configuration and give the new slave a
unique server-id not
used by the master or any of the existing slaves.
6.
Start the new slave. The slave will use the information in its master.info file
to start the replication process.
|
Setting the Master Configuration on the
Slave:
To set up the
slave to communicate with the master for replication, you must tell the slave
the necessary connection information. To do this, execute the following
statement on the slave, replacing the option values with the actual values
relevant to your system:
|
Checking Replication Status:
The most
common task when managing a replication process is to ensure that replication
is taking place and that there have been no errors between the slave and the
master. The primary statement for this is SHOW SLAVE STATUS,
which you must execute on each slave:
The key
fields from the status report to examine are:
1. Slave_IO_State: The current status
of the slave.
2. Slave_IO_Running: Whether the I/O
thread for reading the master's binary log is running. Normally, you want
this to be Yes unless you have not yet started replication
or have explicitly stopped it with STOP SLAVE.
3. Slave_SQL_Running: Whether the SQL
thread for executing events in the relay log is running. As with the I/O
thread, this should normally be Yes.
4. Last_Error: The last error registered when processing the
relay log. Ideally this should be blank, indicating no error.
5. Seconds_Behind_Master: The number of
seconds that the slave SQL thread is behind processing the master binary log.
A high number (or an increasing one) can indicate that the slave is unable to
handle events from the master in a timely fashion.
6. Master_Log_file, Read_Master_Log_Pos:
Coordinates in the master binary log indicating how far the slave I/O thread
has read events from that log.
7. Relay_Master_Log_File, Exec_Master_Log_Pos:
Coordinates in the master binary log indicating how far the slave SQL thread
has executed events received from that log.
8. Relay_Log_File, Relay_Log_Pos:
Coordinates in the slave relay log indicating how far the slave SQL thread
has executed the relay log. These correspond to the preceding coordinates,
but are expressed in slave relay log coordinates rather than master binary
log coordinates.
On the
master, you can check the status of connected slaves using SHOW PROCESSLIST to
examine the list of running processes. Slave connections have Binlog
Dump in the Command field:
Because it is the
slave that drives the replication process, very little information is
available in this report.
The SHOW SLAVE HOSTS statement
on the master shows basic information about the slaves. The output includes
the ID of the slave serve Pausing Replication on the Slave:
You can stop
and start the replication of statements on the slave using the STOP SLAVE and START SLAVEstatements.
To stop
processing of the binary log from the master, use STOP
SLAVE:
mysql>
When
replication is stopped, the slave I/O thread stops reading events from the
master binary log and writing them to the relay log, and the SQL thread stops
reading events from the relay log and executing them. You can pause the I/O
or SQL thread individually by specifying the thread type:
mysql> mysql>
To start
execution again, use the START SLAVE statement:
mysql>
To start a particular thread, specify the thread type:
mysql> mysql>
r, the value
of the --report-host option,
the connecting port, and master ID:
|
Pausing Replication on the Slave:
You can stop
and start the replication of statements on the slave using the STOP SLAVE and START SLAVEstatements.
To stop
processing of the binary log from the master, use STOP
SLAVE:
mysql>
When
replication is stopped, the slave I/O thread stops reading events from the
master binary log and writing them to the relay log, and the SQL thread stops
reading events from the relay log and executing them. You can pause the I/O
or SQL thread individually by specifying the thread type:
mysql> mysql>
To start
execution again, use the START SLAVE statement:
mysql>
To start a particular thread, specify the thread type:
mysql> mysql> |
No comments:
Post a Comment