Setting up replication with XtraBackup


I attended Vadim Tkachenko’s talk on XtraBackup during MySQL conference in Santa Clara last week. Backups are obviously very important, but the use case I had in mind is this:
Replicating a database that has Innodb tables in it, while keeping both master and slave on line if possible.

Tangent: by the way, I love the native backup utility that was once promised in MySQL 6.0, similar to SQL Server’s way of backup. It was like running “BACKUP myDb to DISK = ‘/backupDirectory/myDb.bak’” under mysql client, but I digress…

I have used mysqldump to accomplish this in the past, but I wondered how XtraBackup would fare in this task, especially after hearing Vadim’s talk and reading news on Percona’s development effort. To cut to the chase, this is my conclusion. Reproducing steps are listed immediately afterwards.
1. innobackupex provides a consisten database backup, spitting out log file and log positions in stdout, which is nice and useful for slave initiation;
2. It works with both MyISAM and innodb tables;
3. If MyISAM tables are all you have, just run innobackupex –prepare /directoryWhereBackupIs, and then move the database directory from under /directoryWhereBackupIs to under your slave’s datadir, then make the necessary group and owner change to said directory and its content files, and you are ready to run the “change master” command and start slave;
4. If the database has innodb tables, then in addition to step 3, you will also need to stop mysql on slave, move the ibdata1 file to datadir, then restart mysql, and run “change master…” and “start slave” commands. It does not matter if you are using innodb_file_per_table or not.

It will be nice if I can keep the slave up and running during this step when the database has innodb tables in it. Did I do anything wrong? Is there a better way? What if the slave has a database that has innodb tables and thus uses ibdata1 to begin with? What do you do then? Should I play with Tungsten’s replication? What are the compelling reasons to use Tungsten’s replication?

In any case, from my limited testing, I think I will use innobackupex for future replication creation tasks, if I can afford a mysqld restart. Overall, it feels a bit easier than mysqldump approach that I’ve been using in the past.

Here are the steps needed to reproduce:

1. Fire up 2 Rackspace CentOS 5.5 servers. Rackspace cloud servers beat Amazon EC2 servers hands down, in my view, for developing/sandboxing purposes;
2. Install the required mysql client, server, and XtraBackup on both servers;
3. Make /etc/my.cnf by cloning the sample cnf files under /usr/share/my-small.cnf. 3 minimum changes were necessary: log-bin=mysql-bin, server-id=a unique number, datadir=/var/lib/mysql. The first 2 are necessary for replication, the last is needed for innobackupex

Well, while you are at it, on slave, add in read-only and skip-slave-start if appropriate. That’s best practice for read only slave.

4. Add master server’s public key to authorized_keys on slave, to facilitate easy ssh connection.
5. On master, run this command:
[sourcecode language=”text”]
innobackupex –databases=test –stream=tar /tmp/ –slave-info | ssh root@slave “tar xfi – -C /root”
When it finishes, you should see something like this:
110419 18:54:21 innobackupex: completed OK!
tar: Read 6656 bytes from –
[/sourcecode]

Take note of 3 lines immediately above it, where it states the binlog file and log position, like this:
[sourcecode language=”text”]
innobackupex: MySQL binlog position: filename ‘mysql-bin.000002′, position 2515
[/sourcecode]
6. On slave, run this command:
[sourcecode language=”text”]
innobackupex –apply-log /locationWhereBackupIs
[/sourcecode]

then, assuming the database name is test, run the 2 commands below to change the group and owner to mysql:
[sourcecode language=”text”]
chgrp -R mysql test
chown -R mysql test
[/sourcecode]

move the directory under mysqld’s datadir:
[sourcecode language=”text”]
mv test/ /mysql/datadir
[/sourcecode]

If test database has innodb tables in it, stop mysql on slave, then copy ibdata1 to datadir, restart mysql.

7. On master, open up port 3306 if it is not already open, then create the replication account:
[sourcecode language=”text”]
grant replication slave, replication client on *.* to repl@’50.56.121.%’ identified by ‘p@ssw0rd’;
[/sourcecode]

8. On slave, run:
[sourcecode language=”text”]
change master to master_host=’50.56.121.96′, master_user=’repl’, master_password=’p@ssw0rd’, master_log_file=’see output from innobackupex backup command on master’, master_log_pos=numFrominnobackupexOutputOnMaster;

start slave;

show slave status\G
[/sourcecode]

, ,

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.