Mysql – How does thesqldump take place when there are more than one database

backupMySQLmysqldump

I'm writing a shell script wherein I'm doing a mysqldump of all databases.

My questions are:

  1. Are the tables of all databases in the server locked during the mysqldump?
  2. If I have to do an incremental backup using mysqlbinlog from the last full backup, should I take the time when mysqldump started or when it ended?
  3. What happens when one is using the database when mysqldump is running?
    How can I take the start time for mysqlbinlog? What happens when mysqldump is executed?

Best Answer

Let me address each of your three Questions

QUESTION #1

Are the tables of all databases in the server locked during the mysqldump?

ANSWER TO QUESTION #1

Depends on the default settings you allow and what setting you use to override. The --opt parameter is enabled by default. That enables --add-drop-table, --add-locks, --create-options, --quick, --extended-insert, --lock-tables, --set-charset, and --disable-keys.

If you have a database with all InnoDB tables or a mix of InnoDB tables and read-only MyISAM tables, you can just use --single-transaction. This will create a clean point-in-time snapshot of all tables involved in the mysqldump. Any INSERT/UPDATE/DELETE queries against an InnoDB table will not interfere with the point-in-time consistency of the mysqldump.

By the way, --single-transaction is mutually exclusive to --lock-all-tables. Thus, using --single-transaction will disable --lock-all-tables.

QUESTION #2

If I have to do an incremental backup using mysqlbinlog from the last full backup, should I take the time when mysqldump started or when it ended?

ANSWER TO QUESTION #2

You could take the time if you do mysqlbinlog at different times of the day. You could save yourself some off that housekeeping information by simply running FLUSH LOGS; or FLUSH BINARY LOGS; the same time every night. For example, you could script a cronjob to run FLUSH LOGS;at 11:59 PM like this

#!/bin/sh
sleep 55
mysql -uroot -p... -e"FLUSH LOGS;"

That way you can logically group together all binary logs that have the same date when formulating any needed incremental backup.

You do not have record times or positions in this respect: If you include the option --master-data=2 in the mysqldump, the log file and position at recorded as a comment on line 22 of the mysqldump output.

QUESTION #3

What happens when one is using the database when mysqldump is running? How can I take the start time for mysqlbinlog? What happens when mysqldump is executed?

ANSWER TO QUESTION #3

Here is where you have to be very careful. As I mentioned earlier, if you have a database with all InnoDB tables or a mix of InnoDB tables and read-only MyISAM tables, you can use --single-transaction and every table involved in the mysqldump will exist in the same point-in-time.

What Can Go Wrong #1

If you are performing INSERT/UPDATE/DELETE queries against a MyISAM table during the mysqldump, then the MyISAM tables will not be point-in-time consistent.

What Can Go Wrong #2

If you are performing ALTER TABLE, DROP TABLE, RENAME TABLE, or TRUNCATE TABLE against any table (MyISAM or InnoDB), consistent mysqldumps are no longer isolated. The transaction you issued with --single-transaction is released (made null and void) and all tables afterward can no longer the point-in-time consistent.

If one of these occur, recording start times or position is rendered useless.