Archive for February, 2008

How to sync tables in master-master MySQL replication

Suppose you have a master-master replication setup, and you know one of the tables has the wrong data. How do you re-sync it with the other server?

Warning: don’t just use any tool for this job! You may destroy your good copy of the data.

If your table is large, you’ll probably want to use a tool that can smartly find the differences in a very large dataset, and fix only the rows that need to be fixed. There are several tools that are either able to do this, or claim to be able to do this. However, most of them are not replication-aware, and are likely to either break replication or destroy data.

To see why this is, let’s look at a typical scenario. You have server1 and server2 set up as co-masters. On server1, your copy of sakila.film has correct data. On server2, somehow you are missing a row in that table. A hypothetical sync tool will compare the two copies of the data and find the missing row, then insert it on server2. This INSERT statement will flow through replication to server1, where it will cause a duplicate key error and stop replication.

You can probably think of many other scenarios with lots of bad side effects, so I won’t list any more. I’ll leave it at this: when you are synchronizing data on a slave (even if it is also a master), you must not change data on the slave. Changing data on the slave can cause so much trouble in so many different ways! The correct way to do this is to make the changes on the master, and let them flow through replication to the slave.

As far as I know, there is only one tool that is capable of doing this. It is mk-table-sync, which is part of Maatkit. However, even this tool will let you point the gun at your foot and pull the trigger, if you don’t use it correctly.

The correct way to sync a master-master setup with mk-table-sync is with the --synctomaster option, which tells it to make changes on the master:

mk-table-sync --synctomaster h=server2,D=sakila,t=film

Notice that I’m connecting to the slave, but instructing it to make changes on the master. (Yes, it is able to find the master by inspecting the slave).

If you do the following, you’ll probably cause problems:

mk-table-sync h=server1,D=sakila,t=film h=server2

I’ve just updated the documentation to point out the subtleties with master-master replication. However, you should always keep in mind: it’s not just master-master replication. Any replication configuration is best synchronized by making the changes on the master, and you should always avoid changing data on a slave — even to “fix” the slave. I might also add a feature to mk-table-sync to warn you when it detects that you are trying to change data on a slave.

Technorati Tags:, , ,

You might also like:

  1. Progress on Maatkit bounty, part 3
  2. Maatkit version 1579 released
  3. How to know if a MySQL slave is identical to its master
  4. How to avoid an extra index scan in MySQL
  5. Why MySQL says the server is not configured as a slave

Remember to sign up for MySQL Conference and Expo!

You have only a few more days to sign up for the MySQL Conference and Expo before the early-bird discount goes away. Check out the schedule of speakers and tutorials, and sign up soon! And just in case you didn’t get one from any of the other people blogging about it, you can email me for a code that’s good for a 20% discount.

I’m presenting two sessions: one on the query cache, and one on EXPLAIN. Both are manageable for an hour-or-so talk. I’m not trying to boil the ocean, but rather to help you understand these important topics in ways you’ll remember after leaving the conference.

I was also on the voting committee for the proposals, so I’ve read them all. I really believe this event is worth every penny. (Of course, as a speaker, it doesn’t cost me… but I digress).

While you’re there you should also plan to get certified, also at a significant savings. This is a great career move, and there are sessions that will help you prepare. There’s a critical shortage of people who really know how to use MySQL. I must admit, I’m not even certified! But I’ll be taking the certification exams too.

Technorati Tags:, , , , ,

You might also like:

  1. Like it or not, it is the MySQL Conference and Expo
  2. My presentations at the 2008 MySQL Conference and Expo
  3. Send your employees to the MySQL Conference
  4. How to get your session accepted to MySQL Conference 2008
  5. MySQL Conference and Expo 2008, Day Two

Henceforth, I dub thee GLAMP

I’ve decided to start replacing L with GL in acronyms where L supposedly stands for Linux.

I’m not a big user of acronyms, because I think they are exclusionist and they obscure, rather than revealing. (This wouldn’t matter if I wrote for people who already knew what I meant and agreed with me, but that’s a waste of time). However, LAMP is one that I’ve probably used a few times, without thinking that it is supposed to stand for Linux, Apache, MySQL, and PHP/Perl/Python. In fact, it doesn’t refer to Linux, it refers to GNU/Linux. Therefore, it should be GLAMP.

Why does this matter? I try not to say Linux, unless I’m referring to a kernel, because a kernel is not an operating system. I try to be pretty careful about saying GNU/Linux when I’m talking about an operating system. An exception is a recruiting event yesterday at the University of Virginia, where I compromised my principles because of the noise. Trying to explain myself at that decibel level was just beyond my willingness, so I said we use Linux. If the potential recruits hire on with us, they’ll get to hear me say GNU/Linux. And if they don’t, maybe they’ll attend Richard Stallman’s upcoming talk at the engineering school there on March 27th or 28th (sorry, it’s not listed online, so I can’t link to it).

And you’ll see GNU/Linux used conscientiously if you read the book I’m helping to write, too.

GNU matters. A lot. You may not think so, but if it ceased to exist, you’d find out. That applies equally even if you don’t think you are a Free Software user. You have no idea how much you rely on Free Software in your daily life. And the GNU project has been and continues to be a keystone in that arch of freedom.

Thanks to MySQL’s Brian Aker for snapping me out of my LAMP carelessness.

Technorati Tags:, , , , ,

You might also like:

  1. How to use Linux’s CONFIG_IKCONFIG_PROC feature
  2. Why I write Free Software
  3. Announcement: Xaprb scripts are re-licensed

Maatkit version 1753 released

Download Maatkit

This release contains minor bug fixes and new features. Besides the little bug fixes, there’s a fun new feature in mk-heartbeat: it can auto-discover slaves recursively, and show the replication delay on all of them, to wit:

baron@keywest ~ $ mk-heartbeat --check --host master -D rkdb --recurse 10
master 0
slave1 1
slave2 1
slave3 4

(Not actual results. Your mileage may vary. Closed course, professional driver. Do not attempt).

Nothing else in this release is very exciting. I just wanted to get the bug fixes out there.

Changelog for mk-heartbeat:

2008-02-10: version 1.0.7

   * Added --recurse option to check slaves to any depth.
   * Made mk-heartbeat explicitly close DB connection when done.

Changelog for mk-parallel-dump:

2008-02-10: version 1.0.6

   * Added the --losslessfp option.
   * Fixed child process exit status on Solaris (bug #1886444).

Changelog for mk-parallel-restore:

2008-02-10: version 1.0.5

   * Fixed forking issues with File::Find on Solaris (bug #1887102).
   * Fixed child process exit status on Solaris (bug #1886444).
   * The --defaults-file option caused a mysql error (bug #1886866).

Changelog for mk-show-grants:

2008-02-10: version 1.0.8

   * Added --timestamp option.

Changelog for mk-table-checksum:

2008-02-10: version 1.1.25

   * The --lock option did not work correctly (bug #1884712).

Changelog for mk-table-sync:

2008-02-10: version 1.0.5

   * The Stream algorithm wasn't chosen when a table had no key.
   * Numeric strings beginning with 0 weren't quoted (bug #1883019).
Technorati Tags:, ,

You might also like:

  1. Maatkit version 1709 released
  2. Maatkit version 1674 released
  3. Maatkit version 1877 released
  4. Maatkit version 1314 released
  5. Maatkit version 1579 released