Setting up mysql replication |
With mysql replication, a slave mysql server continuously accesses the binary log on a master mysql server to update its own databases. This is a simple way to provide a "live backup" that is useful e.g. when the master server crashes.
The following outlines (for mysql 4.1.1, but it should also work for later releases) the steps to take, based on my own (limited) experience. For more information, see e.g. the mysql documentation.
We assume that master.vub.ac.be contains the databases dba, dbb, dbc of which we want to mirror only dbb and dbc to slave.vub.ac.be. The exact file locations (e.g. of my.cnf) below may be gentoo-specific.
[mysqld] .. server-id = 1 log-binThe binary log files will be in the mysql data directory (/var/lib/mysql on gentoo). To look at a binary log file, use mysqlbinlog, e.g.
mysqlbinlog /var/lib/mysql/master-bin.000005
bind-address = 134.210.10.99
mysql -u root -pxyz <<EOF GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicant'@'slave.vub.ac.be' IDENTIFIED BY 'some_password'; EOF
mysqldump --user=root --password=xyz \ --extended-insert --master-data --lock-tables \ --databases dbb dbc >/tmp/master.dumpThanks to the --master-data option, the master.dump file will contain a line such as the following
CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000011', MASTER_LOG_POS=524;which, when executed on the slave, will tell it from which point in the master's binary logs it should start mirroring. The master.dump file also contains database creation statements:
CREATE DATABASE dbb; CREATE DATABASE dbc;so one does not have to create these explicitly on the slave.
[mysqld] .. server-id = 2
[mysqld] .. character-set-server = latin1 default-character-set = latin1
[mysqld] .. server-id = 2 replicate-ignore-db=dba replicate-wild-do-table=dbb.% replicate-wild-do-table=dbc.%
mysqladmin -u root -pxyz -f drop dbb mysqladmin -u root -pxyz -f drop dbc
scp master.vub.ac.be:/tmp/master.sql /tmp/master.sql mysql -u root -pxyz </tmp/master.sql
mysql -u root -pxyz <<EOF STOP SLAVE; CHANGE MASTER TO MASTER_HOST='master.vub.ac.be', MASTER_USER='replicant', MASTER_PASSWORD='some_password'; START SLAVE;