13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Implementing Replication<br />

307<br />

You need to edit your my.ini or my.cnf file on both the master <strong>and</strong> slave servers.<br />

On the master, you need the following settings:<br />

[mysqld]<br />

log-bin<br />

server-id=1<br />

The first setting turns on binary logging (so you should already have this one; if not, add<br />

it in now).The second setting gives your master server a unique ID. Each of the slaves<br />

also needs an ID, so you need to add a similar line to the my.ini/my.cnf files on each of<br />

the slaves. Make sure the numbers are unique! For example, your first slave could have<br />

server-id=2; the next, server-id=3; <strong>and</strong> so on.<br />

Setting Up the Master<br />

On the master, you need to create a user for slaves to connect as.There is a special privilege<br />

level for slaves called replication slave. Depending on how you plan to do the initial<br />

data transfer, you may need to temporarily grant some additional privileges.<br />

In most cases, you will use a database snapshot to transfer the data, <strong>and</strong> in this case,<br />

only the special replication slave privilege is needed. If you decide to use the LOAD DATA<br />

FROM MASTER comm<strong>and</strong> to transfer data (you learn about it in the next section), this user<br />

will also need the RELOAD, SUPER, <strong>and</strong> SELECT privileges, but only for initial setup. As per<br />

the principle of least privilege, discussed in Chapter 9, you should revoke these other<br />

privileges after the system is up <strong>and</strong> running.<br />

Create a user on the master.You can call it anything you like <strong>and</strong> give it any password<br />

you like, but you should make a note of the username <strong>and</strong> password you choose. In our<br />

example, we call this user rep_slave:<br />

grant replication slave<br />

on *.*<br />

to ‘rep_slave’@’%’ identified by ‘password’;<br />

Obviously, you should change the password to something else.<br />

Performing the Initial Data Transfer<br />

You can transfer the data from master to slave in several ways.The simplest is to set up<br />

the slaves (described in the next section) <strong>and</strong> then run a LOAD DATA FROM MASTER statement.The<br />

problem with this approach is that it will lock the tables on the master while<br />

the data is being transferred, <strong>and</strong> this can take some time, so we do not recommend it.<br />

(You can use this option only if you are using MyISAM tables.)<br />

Generally, it is better to take a snapshot of the database at the current time.You can<br />

do this by using the procedures described for taking backups elsewhere in this chapter.<br />

You should first flush the tables with the following statement:<br />

flush tables with read lock;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!