Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Replicating servers in MySQL - Why and how

In MySQL, you can store the same data in multiple servers to provide extra bandwidth and robustness. The process known as REPLICATION is used to keep the data between the two (or more) servers in step.

PRINCIPLES

One server is designated as being the MASTER server and all data updates are performed on that server. Updates are logged by the master server, and a running SLAVE server will collect the changes.

These notes are written to cover data stored on two systems, but note that you can increase the number of servers if you wish - either by running multiple slaves off a master, or by setting up a slave to the first master as a master itself, supplying updates to a second generation slave.

The binary log file format used by the master to record changes varies significantly from one release to the next, so that the slave server should always be at the same or a higher version of MySQL than the master.

A replica server provides extra bandwidth and robustness and is also useful in allowing you to take a reference backup without having to stop data entry - simply freeze the slave, do the backup, and have the slave catch up.

SETUP EXAMPLE

for this example ...
 Master server - robinson
 Slave server - friday
 MySQL root password - k645pfb
 internal account name, ids, etc, made up as we go along.


1. Check that the versions of MySQL that you propose to use on the Master and Slave will work together; ideally, use the same version on both.

2. Install the MySQL on both master and slave if not already running; no need to configure the slave as you'll be simply replicating, but do configure the master.

3. Set up an account on the master server that the slave can use to make its connection. You need to grant the account REPLICATION SLAVE privilege.

 GRANT REPLICATION SLAVE ON *.* TO
  'tgif'@'friday' IDENTIFIED BY 'codAndCh1ps';

4. Take a snapshot of the data on the master server.
 
a) Lock all the tables on the master:
 FLUSH TABLES WITH READ LOCK

b) Run
 SHOW MASTER STATUS
and note the File and Position (if blank, don't worry). LEAVE the MySQL client that you used for that running. This will prevent updates while you're copying to your mirror.

c) Take a dump of the data on the Master server:
 mysqldump --master-data -uroot -pk645pfb > mirror.sql
 
5. Check that the Master server has an ID set and is running with the log-bin option. This should be in the my.cnf file - for example
 [mysqld]
 log-bin=mysql-bin
 server-id=1
Server ids (Master and Slave) must all be unique positive integers.

If necessary, stop and restart the server

6. Stop the slave server and configure its my.cnf file - for example
 [mysqld]
 server-id=2
Restart the slave server

7. Load the data onto the slave server; transfer the mirror.sql file created in step 4 (c) across, then run
 mysql -uroot -pk645pfb < mirror.sql
Flush or restart the slave server if you've transferred different account information over!

8. Tell the slave server to act as a slave server:
 CHANGE MASTER TO
  MASTER_HOST='robinson',
  MASTER_USER='tgif',
  MASTER_PASSWORD='codAndCh1ps',
  MASTER_LOG_FILE='[recorded name or empty if none]',
  MASTER_LOG_POS=[recorded number or 4 if none];

9. Start the slave threads
 START SLAVE;

That should be it ... your slave will contact your master for any updates since the backup copy was taken.

OPTIONS ON SETUP

Some alternatives:

a) If you want to use LOAD TABLE FROM MASTER or LOAD DATA FROM MASTER commands later, you need to give your slave SUPER and RELOAD global privileges, and also SELECT privilege over any tables that you want to load.

b) The data can also be transferred, more efficiently, in binary. In summary, you use tar, zip or similar software to squish the data directory in MySQL's data directory (extra steps needed if you're using InnoDb as well) and transfer those files - data files, log files, .frm definition files - and unpack them.
  
c) If you're going to be running different user accounts / passwords etc. on the slave server, or it's a significantly more recent version of MySQL, then do NOT transfer the database called mysql.

d) If you're copying to a slave that has previously functioned as a slave, restart it with the --skip-slave-start option in order to avoid it contacting its old master right away.

e) If you configure your slave server as a master too, then it can take over for data changes in the event of a prolonged failure of the master, and can feed data through to second layer slaves. With a properly configured network with homogeneous systems, the old master can be brought back in later as a slave to automatically catch up.

WHAT HAPPENS WHEN IT'S RUNNING?

The Master server keeps a binary log file of database updates for passing to the slaves as required.

The Slave server keeps data files called master.info and relay-log.info to keep track of how it's doing in processing the master log. It maintains a connection to the Master so that updates are virtually instant.

If a slave server looses connection to the master, it will retry connecting periodically (default 60 seconds, --master-connect-retry option to change), and when it gets back in touch it will catch up.

Master servers do not maintain a list of known slaves, let alone a note of whether or not they're up to date.

You are STRONGLY advised not to edit any of these files directly, but rather to use commands such as CHANGE MASTER TO ..


See also MySQL - our FAQ

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

MySQL - Designing an SQL Database System
  [4426] FileMaker Day to Unix Time conversion - (2015-02-15)
  [3494] Databases - when to treat the rules as guidelines - (2011-10-23)
  [3361] Blowing our own trumpet - MySQL resources - (2011-07-18)
  [3270] SQL - Data v Metadata, and the various stages of data selection - (2011-04-29)
  [2749] Delegate Question - defining MySQL table relationships as you create the tables - (2010-05-02)
  [2204] Images in a database? How big is a database? (MySQL) - (2009-05-28)
  [2085] MySQL - licensing issues, even with using the name - (2009-03-16)
  [2053] What a difference a MySQL Index made - (2009-02-25)
  [1771] More HowTo diagrams - MySQL, Tomcat and Java - (2008-08-24)
  [1575] Database design for a shopping application (MySQL) - (2008-03-15)
  [1423] MySQL - table design and initial testing example - (2007-11-06)
  [945] Code quality counts - (2006-11-26)
  [937] Display an image from a MySQL database in a web page via PHP - (2006-11-22)
  [918] Databases needn't be frightening, hard or expensive - (2006-11-08)
  [666] Database design - get it right from first principles - (2006-04-02)
  [515] MySQL - an FAQ - (2005-12-03)
  [494] MySQL - a score of things to remember - (2005-11-12)
  [375] Oops - I got my initial database design wrong - (2005-07-12)
  [361] Binary Large Objects or bars - (2005-06-27)
  [59] MySQL - Pivot tables - (2004-09-22)

Replication in MySQL
  [2210] MySQL server replication - (2009-06-01)

resource index - MySQL
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/mysql-re ... d-how.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard